diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 5c323e1..571634b 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -47,7 +47,11 @@ type Job = Box) + Send>; pub struct Later(UnboundedSender); impl Later { - /// Run `job` on GPUI's thread soon; `false` when the window is gone. + /// Run `job` on GPUI's thread soon. `false` once a job has found the + /// window gone (the entity released): the loop that runs the jobs notices + /// that only when a job reaches it, ends and drops the channel, so the + /// first job sent after the window went answers `true` and is never run. + /// `true` is a promise of nothing more than that the job was queued. pub fn run( &self, job: impl FnOnce(&mut AppState, &mut Context) + Send + 'static, @@ -371,6 +375,21 @@ mod tests { assert_eq!(counters.later_jobs.get(), 1); } + #[gpui::test] + fn a_job_sent_after_the_window_went_is_dropped_and_the_next_one_is_refused( + cx: &mut TestAppContext, + ) { + let (app, _tx, counters) = entity(cx); + let later = app.read_with(cx, |app, _| app.later()); + drop(app); + cx.update(|_| {}); + cx.run_until_parked(); + assert!(later.run(|_, _| {}), "queued: nothing has found the entity gone yet"); + cx.run_until_parked(); + assert_eq!(counters.later_jobs.get(), 0, "never run"); + assert!(!later.run(|_, _| {}), "the first job found the entity gone"); + } + /// What closing the window hands the runtime's shutdown: the number of /// sessions the engine was told to close, which `run` gives time to act /// on (the Slint front end does the same). The engine ignores a close for