1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 21:32:13 +03:00

change mux empty poll to mux empty event

As part of reducing the amount of regularly scheduled stuff wezterm
does in the background, this commit restructures how an empty mux
is detected; now when the mux prunes dead windows it will emit
an Empty event.

The Activity type will now schedule a prune when it is dropped,
which will clean up and trigger the Empty event.

refs: https://github.com/wez/wezterm/issues/770
This commit is contained in:
Wez Furlong 2021-05-08 10:25:49 -07:00
parent 7cd9f32494
commit 1cbaf55640
4 changed files with 24 additions and 11 deletions

View File

@ -1,4 +1,5 @@
//! Keeps track of the number of user-initiated activities //! Keeps track of the number of user-initiated activities
use crate::Mux;
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::atomic::{AtomicUsize, Ordering};
static COUNT: AtomicUsize = AtomicUsize::new(0); static COUNT: AtomicUsize = AtomicUsize::new(0);
@ -25,5 +26,11 @@ impl Activity {
impl Drop for Activity { impl Drop for Activity {
fn drop(&mut self) { fn drop(&mut self) {
COUNT.fetch_sub(1, Ordering::SeqCst); COUNT.fetch_sub(1, Ordering::SeqCst);
promise::spawn::spawn_into_main_thread(async move {
let mux = Mux::get().unwrap();
mux.prune_dead_windows();
})
.detach();
} }
} }

View File

@ -41,6 +41,7 @@ pub enum MuxNotification {
pane_id: PaneId, pane_id: PaneId,
alert: wezterm_term::Alert, alert: wezterm_term::Alert,
}, },
Empty,
} }
static SUB_ID: AtomicUsize = AtomicUsize::new(0); static SUB_ID: AtomicUsize = AtomicUsize::new(0);
@ -184,6 +185,11 @@ fn read_from_pane_pty(pane_id: PaneId, banner: Option<String>, mut reader: Box<d
// We don't know if we can unilaterally close // We don't know if we can unilaterally close
// this pane right now, so don't! // this pane right now, so don't!
state.write(b"\n[Process completed]"); state.write(b"\n[Process completed]");
promise::spawn::spawn_into_main_thread(async move {
let mux = Mux::get().unwrap();
mux.prune_dead_windows();
})
.detach();
} }
ExitBehavior::Close => { ExitBehavior::Close => {
promise::spawn::spawn_into_main_thread(async move { promise::spawn::spawn_into_main_thread(async move {
@ -454,6 +460,10 @@ impl Mux {
log::trace!("window {} is dead", window_id); log::trace!("window {} is dead", window_id);
self.remove_window_internal(window_id); self.remove_window_internal(window_id);
} }
if self.is_empty() {
self.notify(MuxNotification::Empty);
}
} }
pub fn kill_window(&self, window_id: WindowId) { pub fn kill_window(&self, window_id: WindowId) {

View File

@ -66,6 +66,12 @@ impl GuiFrontEnd {
pane_id: _, pane_id: _,
alert: Alert::TitleMaybeChanged, alert: Alert::TitleMaybeChanged,
} => {} } => {}
MuxNotification::Empty => {
if mux::activity::Activity::count() == 0 {
log::trace!("Mux is now empty, terminate gui");
Connection::get().unwrap().terminate_message_loop();
}
}
} }
true true
} else { } else {
@ -76,17 +82,6 @@ impl GuiFrontEnd {
} }
pub fn run_forever(&self) -> anyhow::Result<()> { pub fn run_forever(&self) -> anyhow::Result<()> {
self.connection
.schedule_timer(std::time::Duration::from_millis(200), move || {
if mux::activity::Activity::count() == 0 {
let mux = Mux::get().unwrap();
mux.prune_dead_windows();
if mux.is_empty() {
Connection::get().unwrap().terminate_message_loop();
}
}
});
self.connection.run_message_loop() self.connection.run_message_loop()
} }
} }

View File

@ -86,6 +86,7 @@ where
handler.schedule_pane_push(pane_id); handler.schedule_pane_push(pane_id);
} }
Ok(Item::Notif(MuxNotification::WindowCreated(_window_id))) => {} Ok(Item::Notif(MuxNotification::WindowCreated(_window_id))) => {}
Ok(Item::Notif(MuxNotification::Empty)) => {}
Err(err) => { Err(err) => {
log::error!("process_async Err {}", err); log::error!("process_async Err {}", err);
return Ok(()); return Ok(());