Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-12 14:58:31 +02:00
parent 1f84cdb88c
commit 564a8bdc19
3 changed files with 22 additions and 11 deletions

View file

@ -13,7 +13,7 @@ use crate::{
TextStyleRefinement, TextSystem, Window, WindowContext, WindowHandle, WindowId, TextStyleRefinement, TextSystem, Window, WindowContext, WindowHandle, WindowId,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use collections::{HashMap, VecDeque}; use collections::{HashMap, HashSet, VecDeque};
use futures::Future; use futures::Future;
use parking_lot::Mutex; use parking_lot::Mutex;
use slotmap::SlotMap; use slotmap::SlotMap;
@ -64,6 +64,7 @@ impl App {
unit_entity, unit_entity,
entities, entities,
windows: SlotMap::with_key(), windows: SlotMap::with_key(),
pending_notifications: Default::default(),
pending_effects: Default::default(), pending_effects: Default::default(),
observers: SubscriberSet::new(), observers: SubscriberSet::new(),
event_handlers: SubscriberSet::new(), event_handlers: SubscriberSet::new(),
@ -106,7 +107,8 @@ pub struct AppContext {
pub(crate) unit_entity: Handle<()>, pub(crate) unit_entity: Handle<()>,
pub(crate) entities: EntityMap, pub(crate) entities: EntityMap,
pub(crate) windows: SlotMap<WindowId, Option<Window>>, pub(crate) windows: SlotMap<WindowId, Option<Window>>,
pub(crate) pending_effects: VecDeque<Effect>, pub(crate) pending_notifications: HashSet<EntityId>,
pending_effects: VecDeque<Effect>,
pub(crate) observers: SubscriberSet<EntityId, Handler>, pub(crate) observers: SubscriberSet<EntityId, Handler>,
pub(crate) event_handlers: SubscriberSet<EntityId, EventHandler>, pub(crate) event_handlers: SubscriberSet<EntityId, EventHandler>,
pub(crate) release_handlers: SubscriberSet<EntityId, ReleaseHandler>, pub(crate) release_handlers: SubscriberSet<EntityId, ReleaseHandler>,
@ -148,6 +150,17 @@ impl AppContext {
}) })
} }
pub(crate) fn push_effect(&mut self, effect: Effect) {
match &effect {
Effect::Notify { emitter } => {
if self.pending_notifications.insert(*emitter) {
self.pending_effects.push_back(effect);
}
}
Effect::Emit { .. } => self.pending_effects.push_back(effect),
}
}
fn flush_effects(&mut self) { fn flush_effects(&mut self) {
loop { loop {
self.release_dropped_entities(); self.release_dropped_entities();
@ -197,6 +210,7 @@ impl AppContext {
} }
fn apply_notify_effect(&mut self, emitter: EntityId) { fn apply_notify_effect(&mut self, emitter: EntityId) {
self.pending_notifications.remove(&emitter);
self.observers self.observers
.clone() .clone()
.retain(&emitter, |handler| handler(self)); .retain(&emitter, |handler| handler(self));

View file

@ -116,7 +116,7 @@ impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
} }
pub fn notify(&mut self) { pub fn notify(&mut self) {
self.app.pending_effects.push_back(Effect::Notify { self.app.push_effect(Effect::Notify {
emitter: self.entity_id, emitter: self.entity_id,
}); });
} }
@ -124,7 +124,7 @@ impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
impl<'a, T: EventEmitter + Send + Sync + 'static> ModelContext<'a, T> { impl<'a, T: EventEmitter + Send + Sync + 'static> ModelContext<'a, T> {
pub fn emit(&mut self, event: T::Event) { pub fn emit(&mut self, event: T::Event) {
self.app.pending_effects.push_back(Effect::Emit { self.app.push_effect(Effect::Emit {
emitter: self.entity_id, emitter: self.entity_id,
event: Box::new(event), event: Box::new(event),
}); });

View file

@ -997,12 +997,9 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
pub fn notify(&mut self) { pub fn notify(&mut self) {
self.window_cx.notify(); self.window_cx.notify();
self.window_cx self.window_cx.app.push_effect(Effect::Notify {
.app emitter: self.entity_id,
.pending_effects });
.push_back(Effect::Notify {
emitter: self.entity_id,
});
} }
pub fn run_on_main<R>( pub fn run_on_main<R>(
@ -1052,7 +1049,7 @@ impl<'a, 'w, S: Send + Sync + 'static> ViewContext<'a, 'w, S> {
impl<'a, 'w, S: EventEmitter + Send + Sync + 'static> ViewContext<'a, 'w, S> { impl<'a, 'w, S: EventEmitter + Send + Sync + 'static> ViewContext<'a, 'w, S> {
pub fn emit(&mut self, event: S::Event) { pub fn emit(&mut self, event: S::Event) {
self.window_cx.app.pending_effects.push_back(Effect::Emit { self.window_cx.app.push_effect(Effect::Emit {
emitter: self.entity_id, emitter: self.entity_id,
event: Box::new(event), event: Box::new(event),
}); });