Make use_system() hook return a closure

This commit is contained in:
JMS55 2023-12-12 18:24:48 -08:00
parent 035311036d
commit 8dcaae99e1
3 changed files with 12 additions and 10 deletions

View File

@ -9,18 +9,19 @@ use std::sync::Arc;
#[derive(Resource, Default)]
pub struct DeferredSystemRegistry {
// TODO: Replace ref_counts with Box<Vec<SystemId>> and insert SystemId into it on unmount
pub ref_counts: HashMap<SystemId, Arc<()>>,
pub run_queue: Box<Vec<SystemId>>,
}
#[derive(Clone, Copy)]
pub struct DeferredSystem {
struct DeferredSystem {
id: SystemId,
run_queue: *mut Vec<SystemId>,
}
impl DeferredSystem {
pub fn schedule(&self) {
fn schedule(&self) {
unsafe { &mut *self.run_queue }.push(self.id);
}
}
@ -28,9 +29,13 @@ impl DeferredSystem {
unsafe impl Send for DeferredSystem {}
unsafe impl Sync for DeferredSystem {}
pub fn new_deferred_system<S, M>(system: S, world: &mut World) -> (DeferredSystem, Arc<()>)
pub fn new_deferred_system<S, M>(
system: S,
world: &mut World,
) -> (impl Fn() + Send + Sync + Copy, Arc<()>)
where
S: IntoSystem<(), (), M> + 'static,
M: 'static,
{
let id = world.register_system(system);
let ref_count = Arc::new(());
@ -45,5 +50,5 @@ where
run_queue: Box::as_mut(&mut system_registry.run_queue),
};
(deferred_system, ref_count)
(move || deferred_system.schedule(), ref_count)
}

View File

@ -1,7 +1,4 @@
use crate::{
deferred_system::{new_deferred_system, DeferredSystem},
tick::EcsContext,
};
use crate::{deferred_system::new_deferred_system, tick::EcsContext};
use bevy::{
ecs::{
component::ComponentId,
@ -93,9 +90,10 @@ where
}
}
pub fn use_system<S, M>(cx: &ScopeState, system: S) -> DeferredSystem
pub fn use_system<S, M>(cx: &ScopeState, system: S) -> impl Fn() + Send + Sync + Copy
where
S: IntoSystem<(), (), M> + 'static,
M: 'static,
{
cx.use_hook(|| new_deferred_system(system, EcsContext::get_world(cx)))
.0

View File

@ -19,7 +19,6 @@ use bevy::{
};
use dioxus::core::{Element, ElementId, Scope};
pub use self::deferred_system::DeferredSystem;
pub use bevy_mod_picking;
pub use dioxus;