WIP fix to event reader reactivity

This commit is contained in:
JMS55 2023-12-27 23:04:45 -08:00
parent 350c00c52a
commit 5272be399b
2 changed files with 13 additions and 6 deletions

View File

@ -13,10 +13,12 @@ use dioxus::{
core::{ScopeId, ScopeState},
hooks::use_on_destroy,
};
use std::any::TypeId;
#[derive(Default)]
pub(crate) struct EcsSubscriptions {
pub resources: Box<HashMap<ComponentId, HashSet<ScopeId>>>,
pub events: Box<HashMap<TypeId, (Box<dyn Fn(&World) -> bool>, HashSet<ScopeId>)>>,
pub world_and_queries: Box<HashSet<ScopeId>>,
}
@ -116,15 +118,12 @@ where
}
}
// TODO: Don't think this actually works. EcsSubscriptions needs to handle this.
pub fn use_event_reader<'a, E: Event>(cx: &'a ScopeState) -> EventIterator<'a, E> {
// TODO: Register the subscription
let event_reader = cx.use_hook(|| ManualEventReader::default());
let events = EcsContext::get_world(cx).resource::<Events<E>>();
let new_events = event_reader.read(events);
if new_events.len() != 0 {
(cx.schedule_update())();
}
new_events
event_reader.read(events)
}
pub struct UseQuery<'a, Q: ReadOnlyWorldQuery, F: ReadOnlyWorldQuery> {

View File

@ -93,6 +93,14 @@ fn schedule_ui_renders_from_ecs_subscriptions(ui_root: &mut UiRoot, world: &Worl
}
}
}
for (new_events_exist, scope_ids) in ecs_subscriptions.events.values() {
if new_events_exist(world) {
for scope_id in scope_ids {
ui_root.virtual_dom.mark_dirty(*scope_id);
}
}
}
}
fn render_ui(root_entity: Entity, ui_root: &mut UiRoot, world: &mut World) {