Small refactor

This commit is contained in:
JMS55 2023-12-07 12:30:33 -08:00
parent 8cf201d648
commit 281b93273a
3 changed files with 30 additions and 26 deletions

View file

@ -7,6 +7,12 @@ use bevy::{
};
use std::sync::Arc;
#[derive(Resource, Default)]
pub struct DeferredSystemRegistry {
pub ref_counts: HashMap<SystemId, Arc<()>>,
pub run_queue: Box<Vec<SystemId>>,
}
#[derive(Clone, Copy)]
pub struct DeferredSystem {
id: SystemId,
@ -14,25 +20,6 @@ pub struct DeferredSystem {
}
impl DeferredSystem {
pub(crate) fn new<S>(system: S, world: &mut World) -> (Self, Arc<()>)
where
S: IntoSystem<(), (), ()> + 'static,
{
let id = world.register_system(system);
let ref_count = Arc::new(());
let mut system_registry = world.resource_mut::<DeferredSystemRegistry>();
system_registry
.ref_counts
.insert(id, Arc::clone(&ref_count));
let deferred_system = Self {
id,
run_queue: Box::as_mut(&mut system_registry.run_queue),
};
(deferred_system, ref_count)
}
pub fn schedule(&self) {
unsafe { &mut *self.run_queue }.push(self.id);
}
@ -41,8 +28,22 @@ impl DeferredSystem {
unsafe impl Send for DeferredSystem {}
unsafe impl Sync for DeferredSystem {}
#[derive(Resource, Default)]
pub struct DeferredSystemRegistry {
pub ref_counts: HashMap<SystemId, Arc<()>>,
pub run_queue: Box<Vec<SystemId>>,
pub fn new_deferred_system<S>(system: S, world: &mut World) -> (DeferredSystem, Arc<()>)
where
S: IntoSystem<(), (), ()> + 'static,
{
let id = world.register_system(system);
let ref_count = Arc::new(());
let mut system_registry = world.resource_mut::<DeferredSystemRegistry>();
system_registry
.ref_counts
.insert(id, Arc::clone(&ref_count));
let deferred_system = DeferredSystem {
id,
run_queue: Box::as_mut(&mut system_registry.run_queue),
};
(deferred_system, ref_count)
}

View file

@ -1,4 +1,7 @@
use crate::{deferred_system::DeferredSystem, tick::EcsContext};
use crate::{
deferred_system::{new_deferred_system, DeferredSystem},
tick::EcsContext,
};
use bevy::ecs::{
system::{IntoSystem, Resource},
world::World,
@ -26,7 +29,7 @@ impl DioxusUiHooks for ScopeState {
where
S: IntoSystem<(), (), ()> + 'static,
{
self.use_hook(|| DeferredSystem::new(system, EcsContext::get_world(self)))
self.use_hook(|| new_deferred_system(system, EcsContext::get_world(self)))
.0
}
}

View file

@ -16,7 +16,7 @@ use bevy::{
};
use dioxus_core::ElementId;
pub use self::hooks::DioxusUiHooks;
pub use self::{deferred_system::DeferredSystem, hooks::DioxusUiHooks};
pub use dioxus_core::{Element, Scope};
pub struct DioxusUiPlugin;