浏览代码

Merge pull request #772 from Demonthos/fix-future-on-dropped-scope

Jon Kelley 2 年之前
父节点
当前提交
7f4e45de49
共有 2 个文件被更改,包括 10 次插入1 次删除
  1. 5 0
      packages/core/src/arena.rs
  2. 5 1
      packages/core/src/scopes.rs

+ 5 - 0
packages/core/src/arena.rs

@@ -116,6 +116,11 @@ impl VirtualDom {
         for hook in scope.hook_list.get_mut().drain(..) {
             drop(unsafe { BumpBox::from_raw(hook) });
         }
+
+        // Drop all the futures once the hooks are dropped
+        for task_id in scope.spawned_tasks.borrow_mut().drain() {
+            scope.tasks.remove(task_id);
+        }
     }
 
     fn drop_scope_inner(&mut self, node: &VNode) {

+ 5 - 1
packages/core/src/scopes.rs

@@ -319,7 +319,9 @@ impl<'src> ScopeState {
 
     /// Pushes the future onto the poll queue to be polled after the component renders.
     pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
-        self.tasks.spawn(self.id, fut)
+        let id = self.tasks.spawn(self.id, fut);
+        self.spawned_tasks.borrow_mut().insert(id);
+        id
     }
 
     /// Spawns the future but does not return the [`TaskId`]
@@ -340,6 +342,8 @@ impl<'src> ScopeState {
             .unbounded_send(SchedulerMsg::TaskNotified(id))
             .expect("Scheduler should exist");
 
+        self.spawned_tasks.borrow_mut().insert(id);
+
         id
     }