diff --git a/Cargo.lock b/Cargo.lock index 946d99e547..08c8b667b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5368,6 +5368,7 @@ dependencies = [ "settings", "shellexpand", "smallvec", + "smol", "theme", "thiserror", "util", diff --git a/crates/terminal/Cargo.toml b/crates/terminal/Cargo.toml index 836555e963..4099cfd50a 100644 --- a/crates/terminal/Cargo.toml +++ b/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" diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index e873e7b48e..ed5271854b 100644 --- a/crates/terminal/src/terminal.rs +++ b/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 { + pub fn subscribe(mut self, cx: &mut ModelContext) -> 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; } + } - future::ready(()) - }) - .await; + 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; + } + + Some(()) }) .detach();