Remove allocation in ScopeDisposer

This commit is contained in:
novacrazy 2023-04-05 21:21:22 -05:00
parent 164dcd1b97
commit b51da35a9a
2 changed files with 6 additions and 4 deletions

View file

@ -418,7 +418,7 @@ impl RuntimeId {
with_runtime(self, |runtime| {
let id = { runtime.scopes.borrow_mut().insert(Default::default()) };
let scope = Scope { runtime: self, id };
let disposer = ScopeDisposer(Box::new(move || scope.dispose()));
let disposer = ScopeDisposer(scope);
(scope, disposer)
})
.expect(
@ -439,7 +439,7 @@ impl RuntimeId {
}
let scope = Scope { runtime: self, id };
let val = f(scope);
let disposer = ScopeDisposer(Box::new(move || scope.dispose()));
let disposer = ScopeDisposer(scope);
(val, id, disposer)
})
.expect("tried to run scope in a runtime that has been disposed")

View file

@ -336,7 +336,8 @@ pub(crate) enum ScopeProperty {
/// 1. dispose of all child `Scope`s
/// 2. run all cleanup functions defined for this scope by [on_cleanup](crate::on_cleanup).
/// 3. dispose of all signals, effects, and resources owned by this `Scope`.
pub struct ScopeDisposer(pub(crate) Box<dyn FnOnce()>);
#[repr(transparent)]
pub struct ScopeDisposer(pub(crate) Scope);
impl ScopeDisposer {
/// Disposes of a reactive [Scope](crate::Scope).
@ -345,8 +346,9 @@ impl ScopeDisposer {
/// 1. dispose of all child `Scope`s
/// 2. run all cleanup functions defined for this scope by [on_cleanup](crate::on_cleanup).
/// 3. dispose of all signals, effects, and resources owned by this `Scope`.
#[inline(always)]
pub fn dispose(self) {
(self.0)()
self.0.dispose()
}
}