Improve DeferredSystem ergonomics
This commit is contained in:
parent
c8d1e1d402
commit
4ff1563941
|
@ -2,16 +2,10 @@ use bevy::ecs::{
|
||||||
system::{IntoSystem, Resource, SystemId},
|
system::{IntoSystem, Resource, SystemId},
|
||||||
world::World,
|
world::World,
|
||||||
};
|
};
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
// TODO: Can probably make the value stored in the hook the only one that has a Drop unregistering the system,
|
#[derive(Clone, Copy)]
|
||||||
// and then make the type that lets you schedule the system Copy
|
pub struct DeferredSystem {
|
||||||
|
id: SystemId,
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DeferredSystem(pub(crate) Arc<DeferredSystemInner>);
|
|
||||||
|
|
||||||
pub(crate) struct DeferredSystemInner {
|
|
||||||
pub id: SystemId,
|
|
||||||
world: *mut World,
|
world: *mut World,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,28 +14,32 @@ impl DeferredSystem {
|
||||||
where
|
where
|
||||||
S: IntoSystem<(), (), ()> + 'static,
|
S: IntoSystem<(), (), ()> + 'static,
|
||||||
{
|
{
|
||||||
Self(Arc::new(DeferredSystemInner {
|
Self {
|
||||||
id: world.register_system(system),
|
id: world.register_system(system),
|
||||||
world,
|
world,
|
||||||
}))
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schedule(&self) {
|
pub fn schedule(&self) {
|
||||||
unsafe { &mut *self.0.world }
|
unsafe { &mut *self.world }
|
||||||
.resource_mut::<DeferredSystemRunQueue>()
|
.resource_mut::<DeferredSystemRunQueue>()
|
||||||
.0
|
.0
|
||||||
.push(self.clone());
|
.push(self.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for DeferredSystemInner {
|
unsafe impl Send for DeferredSystem {}
|
||||||
|
unsafe impl Sync for DeferredSystem {}
|
||||||
|
|
||||||
|
pub struct OnDropUnregisterDeferredSystem(pub DeferredSystem);
|
||||||
|
|
||||||
|
impl Drop for OnDropUnregisterDeferredSystem {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe { &mut *self.world }.remove_system(self.id).unwrap();
|
unsafe { &mut *self.0.world }
|
||||||
|
.remove_system(self.0.id)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for DeferredSystemInner {}
|
|
||||||
unsafe impl Sync for DeferredSystemInner {}
|
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
pub struct DeferredSystemRunQueue(pub Vec<DeferredSystem>);
|
pub struct DeferredSystemRunQueue(pub Vec<SystemId>);
|
||||||
|
|
11
src/hooks.rs
11
src/hooks.rs
|
@ -1,4 +1,7 @@
|
||||||
use crate::{deferred_system::DeferredSystem, tick::EcsContext};
|
use crate::{
|
||||||
|
deferred_system::{DeferredSystem, OnDropUnregisterDeferredSystem},
|
||||||
|
tick::EcsContext,
|
||||||
|
};
|
||||||
use bevy::ecs::{
|
use bevy::ecs::{
|
||||||
system::{IntoSystem, Resource},
|
system::{IntoSystem, Resource},
|
||||||
world::World,
|
world::World,
|
||||||
|
@ -26,8 +29,10 @@ impl DioxusUiHooks for ScopeState {
|
||||||
where
|
where
|
||||||
S: IntoSystem<(), (), ()> + 'static,
|
S: IntoSystem<(), (), ()> + 'static,
|
||||||
{
|
{
|
||||||
self.use_hook(|| DeferredSystem::new(system, EcsContext::get_world(self)))
|
self.use_hook(|| {
|
||||||
.clone()
|
OnDropUnregisterDeferredSystem(DeferredSystem::new(system, EcsContext::get_world(self)))
|
||||||
|
})
|
||||||
|
.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,8 +44,8 @@ pub fn tick_dioxus_ui(world: &mut World) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for system in mem::take(&mut world.resource_mut::<DeferredSystemRunQueue>().0) {
|
for system_id in mem::take(&mut world.resource_mut::<DeferredSystemRunQueue>().0) {
|
||||||
world.run_system(system.0.id).unwrap();
|
world.run_system(system_id).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue