diff --git a/packages/core/src/runtime.rs b/packages/core/src/runtime.rs index 1373736c7..80f5fbe54 100644 --- a/packages/core/src/runtime.rs +++ b/packages/core/src/runtime.rs @@ -62,10 +62,27 @@ impl Runtime { contexts[id.0] = Some(context); } - pub(crate) fn remove_context(&self, id: ScopeId) { - if let Some(_scope) = self.scope_contexts.borrow_mut()[id.0].take() { - // todo: some cleanup work + pub(crate) fn remove_context(self: &Rc, id: ScopeId) { + { + 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 @@ -75,7 +92,7 @@ impl Runtime { /// 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(&self, id: ScopeId, f: impl FnOnce() -> O) -> O { self.scope_stack.borrow_mut().push(id); let o = f(); diff --git a/packages/core/src/scope_context.rs b/packages/core/src/scope_context.rs index 441abc056..ea3a63134 100644 --- a/packages/core/src/scope_context.rs +++ b/packages/core/src/scope_context.rs @@ -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 { /// Get the current scope id pub fn current_scope_id(self) -> Option { diff --git a/packages/core/tests/miri_simple.rs b/packages/core/tests/miri_simple.rs index 96b7ce32a..4224c14ee 100644 --- a/packages/core/tests/miri_simple.rs +++ b/packages/core/tests/miri_simple.rs @@ -41,6 +41,7 @@ fn contexts_drop() { } } + #[allow(non_snake_case)] fn ChildComp() -> Element { let el = consume_context::();