Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Add test for notifying and dropping subscriptions in an update cycle
Max Brunsfeld4708f5d88f85
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rsindex 3299fbb746e..4a5b6f54be4 100644--- a/crates/gpui/src/app.rs+++ b/crates/gpui/src/app.rs@@ -6097,6 +6097,31 @@ mod tests {assert_eq!(view.read(cx).events, ["before notify"]);}+ #[crate::test(self)]+ fn test_notify_and_drop_observe_subscription_in_same_update_cycle(cx: &mut MutableAppContext) {+ struct Model;+ impl Entity for Model {+ type Event = ();+ }++ let model = cx.add_model(|_| Model);+ let (_, view) = cx.add_window(Default::default(), |_| TestView::default());++ view.update(cx, |_, cx| {+ model.update(cx, |_, cx| cx.notify());+ drop(cx.observe(&model, move |this, _, _| {+ this.events.push("model notified".into());+ }));+ model.update(cx, |_, cx| cx.notify());+ });++ for _ in 0..3 {+ model.update(cx, |_, cx| cx.notify());+ }++ assert_eq!(view.read(cx).events, Vec::<String>::new());+ }+#[crate::test(self)]fn test_dropping_observers(cx: &mut MutableAppContext) {struct Model;diff --git a/crates/gpui/src/app/callback_collection.rs b/crates/gpui/src/app/callback_collection.rsindex 5bed9f7a295..43f4f3f62ef 100644--- a/crates/gpui/src/app/callback_collection.rs+++ b/crates/gpui/src/app/callback_collection.rs@@ -58,14 +58,6 @@ impl<K: Clone + Hash + Eq + Copy, F> CallbackCollection<K, F> {}}- pub fn count(&mut self, key: K) -> usize {- self.internal- .lock()- .callbacks- .get(&key)- .map_or(0, |callbacks| callbacks.len())- }-pub fn add_callback(&mut self, key: K, subscription_id: usize, callback: F) {let mut this = self.internal.lock();if !this.dropped_subscriptions.contains(&(key, subscription_id)) {@@ -77,7 +69,9 @@ impl<K: Clone + Hash + Eq + Copy, F> CallbackCollection<K, F> {}pub fn remove(&mut self, key: K) {- self.internal.lock().callbacks.remove(&key);+ let callbacks = self.internal.lock().callbacks.remove(&key);+ // drop these after releasing the lock+ drop(callbacks);}pub fn emit<C: FnMut(&mut F, &mut MutableAppContext) -> bool>(