Pause and buffer

Mikayla Maki created

Change summary

Cargo.lock                      |  1 
crates/terminal/Cargo.toml      |  1 
crates/terminal/src/terminal.rs | 38 +++++++++++++++++++---------------
3 files changed, 23 insertions(+), 17 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -5368,6 +5368,7 @@ dependencies = [
  "settings",
  "shellexpand",
  "smallvec",
+ "smol",
  "theme",
  "thiserror",
  "util",

crates/terminal/Cargo.toml 🔗

@@ -17,6 +17,7 @@ settings = { path = "../settings" }
 workspace = { path = "../workspace" }
 project = { path = "../project" }
 smallvec = { version = "1.6", features = ["union"] }
+smol = "1.2.5"
 mio-extras = "2.0.6"
 futures = "0.3"
 ordered-float = "2.1.1"

crates/terminal/src/terminal.rs 🔗

@@ -19,10 +19,7 @@ use alacritty_terminal::{
 };
 use anyhow::{bail, Result};
 
-use futures::{
-    channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
-    future,
-};
+use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
 
 use modal::deploy_modal;
 use settings::{Settings, Shell};
@@ -350,25 +347,32 @@ impl TerminalBuilder {
         })
     }
 
-    pub fn subscribe(self, cx: &mut ModelContext<Terminal>) -> Terminal {
+    pub fn subscribe(mut self, cx: &mut ModelContext<Terminal>) -> Terminal {
         //Event loop
         cx.spawn_weak(|this, mut cx| async move {
             use futures::StreamExt;
 
-            self.events_rx
-                .for_each(|event| {
-                    match this.upgrade(&cx) {
-                        Some(this) => {
-                            this.update(&mut cx, |this, cx| {
-                                this.process_event(&event, cx);
-                            });
-                        }
-                        None => {}
+            let mut events = Vec::new();
+            while let Some(event) = self.events_rx.next().await {
+                events.push(event);
+                while let Ok(Some(event)) = self.events_rx.try_next() {
+                    events.push(event);
+                    if events.len() > 1000 {
+                        break;
+                    }
+                }
+
+                let this = this.upgrade(&cx)?;
+                this.update(&mut cx, |this, cx| {
+                    for event in events.drain(..) {
+                        this.process_event(&event, cx);
                     }
+                });
+
+                smol::future::yield_now().await;
+            }
 
-                    future::ready(())
-                })
-                .await;
+            Some(())
         })
         .detach();