From 5272be399b809b9f8a82ffce35b06d2e37b84343 Mon Sep 17 00:00:00 2001 From: JMS55 <47158642+JMS55@users.noreply.github.com> Date: Wed, 27 Dec 2023 23:04:45 -0800 Subject: [PATCH] WIP fix to event reader reactivity --- src/ecs_hooks.rs | 11 +++++------ src/tick.rs | 8 ++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/ecs_hooks.rs b/src/ecs_hooks.rs index c31e411..744bd40 100644 --- a/src/ecs_hooks.rs +++ b/src/ecs_hooks.rs @@ -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>>, + pub events: Box bool>, HashSet)>>, pub world_and_queries: Box>, } @@ -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::>(); - 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> { diff --git a/src/tick.rs b/src/tick.rs index 31d5ddb..568f20e 100644 --- a/src/tick.rs +++ b/src/tick.rs @@ -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) {