Avoid duplicate notifications for the same entity in the same effect cycle

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld
2021-09-30 14:06:09 -07:00
co-authored by Nathan Sobo
parent fef0bdf6c3
commit ad92bfe79f
+16 -7
View File
@@ -312,6 +312,7 @@ impl App {
let mut state = self.0.borrow_mut(); let mut state = self.0.borrow_mut();
state.pending_flushes += 1; state.pending_flushes += 1;
let result = callback(&mut *state); let result = callback(&mut *state);
state.pending_notifications.clear();
state.flush_effects(); state.flush_effects();
result result
} }
@@ -668,6 +669,7 @@ pub struct MutableAppContext {
debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>, debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
foreground: Rc<executor::Foreground>, foreground: Rc<executor::Foreground>,
pending_effects: VecDeque<Effect>, pending_effects: VecDeque<Effect>,
pending_notifications: HashSet<usize>,
pending_flushes: usize, pending_flushes: usize,
flushing_effects: bool, flushing_effects: bool,
next_cursor_style_handle_id: Arc<AtomicUsize>, next_cursor_style_handle_id: Arc<AtomicUsize>,
@@ -708,6 +710,7 @@ impl MutableAppContext {
debug_elements_callbacks: HashMap::new(), debug_elements_callbacks: HashMap::new(),
foreground, foreground,
pending_effects: VecDeque::new(), pending_effects: VecDeque::new(),
pending_notifications: HashSet::new(),
pending_flushes: 0, pending_flushes: 0,
flushing_effects: false, flushing_effects: false,
next_cursor_style_handle_id: Default::default(), next_cursor_style_handle_id: Default::default(),
@@ -1015,10 +1018,18 @@ impl MutableAppContext {
observations: Some(Arc::downgrade(&self.observations)), observations: Some(Arc::downgrade(&self.observations)),
} }
} }
pub(crate) fn notify_model(&mut self, model_id: usize) {
if self.pending_notifications.insert(model_id) {
self.pending_effects
.push_back(Effect::ModelNotification { model_id });
}
}
pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) { pub(crate) fn notify_view(&mut self, window_id: usize, view_id: usize) {
self.pending_effects if self.pending_notifications.insert(view_id) {
.push_back(Effect::ViewNotification { window_id, view_id }); self.pending_effects
.push_back(Effect::ViewNotification { window_id, view_id });
}
} }
pub fn dispatch_action<A: Action>( pub fn dispatch_action<A: Action>(
@@ -1400,6 +1411,7 @@ impl MutableAppContext {
refreshing = true; refreshing = true;
} }
} }
self.pending_notifications.clear();
self.remove_dropped_entities(); self.remove_dropped_entities();
} else { } else {
self.remove_dropped_entities(); self.remove_dropped_entities();
@@ -1411,6 +1423,7 @@ impl MutableAppContext {
if self.pending_effects.is_empty() { if self.pending_effects.is_empty() {
self.flushing_effects = false; self.flushing_effects = false;
self.pending_notifications.clear();
break; break;
} else { } else {
refreshing = false; refreshing = false;
@@ -1983,11 +1996,7 @@ impl<'a, T: Entity> ModelContext<'a, T> {
} }
pub fn notify(&mut self) { pub fn notify(&mut self) {
self.app self.app.notify_model(self.model_id);
.pending_effects
.push_back(Effect::ModelNotification {
model_id: self.model_id,
});
} }
pub fn subscribe<S: Entity, F>( pub fn subscribe<S: Entity, F>(