run hook destructors inside of the scope they were created in

This commit is contained in:
Evan Almloff committed 2024-01-22 09:32:53 -06:00
1 parent e5a11aa2a5
commit cdf25f0c14
3 files changed
+22 -15

No files matched your search

+21 -4
View File
@@ -62,10 +62,27 @@ impl Runtime {
contexts[id.0] = Some(context); contexts[id.0] = Some(context);
} }
pub(crate) fn remove_context(&self, id: ScopeId) { pub(crate) fn remove_context(self: &Rc<Self>, id: ScopeId) {
if let Some(_scope) = self.scope_contexts.borrow_mut()[id.0].take() { {
// todo: some cleanup work let borrow = self.scope_contexts.borrow();
if let Some(scope) = &borrow[id.0] {
let _runtime_guard = RuntimeGuard::new(self.clone());
// Manually drop tasks, hooks, and contexts inside of the runtime
self.on_scope(id, || {
// Drop all spawned tasks
for id in scope.spawned_tasks.take() {
self.remove_task(id);
}
// Drop all hooks
scope.hooks.take();
// Drop all contexts
scope.shared_contexts.take();
});
}
} }
self.scope_contexts.borrow_mut()[id.0].take();
} }
/// Get the current scope id /// Get the current scope id
@@ -75,7 +92,7 @@ impl Runtime {
/// Call this function with the current scope set to the given scope /// Call this function with the current scope set to the given scope
/// ///
/// Useful in a limited number of scenarios, not public. /// Useful in a limited number of scenarios
pub fn on_scope<O>(&self, id: ScopeId, f: impl FnOnce() -> O) -> O { pub fn on_scope<O>(&self, id: ScopeId, f: impl FnOnce() -> O) -> O {
self.scope_stack.borrow_mut().push(id); self.scope_stack.borrow_mut().push(id);
let o = f(); let o = f();
-11
View File
@@ -300,17 +300,6 @@ impl ScopeContext {
} }
} }
impl Drop for ScopeContext {
fn drop(&mut self) {
// Drop all spawned tasks
_ = Runtime::with(|rt| {
for id in self.spawned_tasks.borrow().iter() {
rt.remove_task(*id);
}
})
}
}
impl ScopeId { impl ScopeId {
/// Get the current scope id /// Get the current scope id
pub fn current_scope_id(self) -> Option<ScopeId> { pub fn current_scope_id(self) -> Option<ScopeId> {
+1
View File
@@ -41,6 +41,7 @@ fn contexts_drop() {
} }
} }
#[allow(non_snake_case)]
fn ChildComp() -> Element { fn ChildComp() -> Element {
let el = consume_context::<String>(); let el = consume_context::<String>();