From e416e31bc733999f20c39dc303ecf74263e540c3 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sun, 27 Feb 2022 17:22:21 -0500 Subject: [PATCH] feat: new spawn_forever for tasks that never die --- packages/core/src/scopes.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs index 7d50fd2a5..7e415801b 100644 --- a/packages/core/src/scopes.rs +++ b/packages/core/src/scopes.rs @@ -773,6 +773,20 @@ impl ScopeState { self.push_future(fut); } + /// Spawn a future that Dioxus will never clean up + /// + /// This is good for tasks that need to be run after the component has been dropped. + pub fn spawn_forever(&self, fut: impl Future + 'static) -> TaskId { + // wake up the scheduler if it is sleeping + self.tasks + .sender + .unbounded_send(SchedulerMsg::NewTask(self.our_arena_idx)) + .unwrap(); + + // The root scope will never be unmounted so we can just add the task at the top of the app + self.tasks.spawn(ScopeId(0), fut) + } + /// Informs the scheduler that this task is no longer needed and should be removed /// on next poll. pub fn remove_future(&self, id: TaskId) {