fix and test spawn_forever (#2216)

This commit is contained in:
Evan Almloff authored and GitHub committed 2024-04-02 10:52:00 -07:00
1 parent 947b23bda5
commit 5df333fca2
3 files changed
+44 -9

No files matched your search

+3 -1
View File
@@ -93,8 +93,10 @@ pub fn spawn(fut: impl Future<Output = ()> + 'static) -> Task {
/// Spawn a future that Dioxus won't clean up when this component is unmounted
///
/// This is good for tasks that need to be run after the component has been dropped.
///
/// **This will run the task in the root scope. Any calls to global methods inside the future (including `context`) will be run in the root scope.**
pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<Task> {
Runtime::with_current_scope(|cx| cx.spawn_forever(fut))
Runtime::with_scope(ScopeId::ROOT, |cx| cx.spawn(fut))
}
/// Informs the scheduler that this task is no longer needed and should be removed.
-8
View File
@@ -265,14 +265,6 @@ impl Scope {
id
}
/// Spawn a future that Dioxus won't clean up when this component is unmounted
///
/// This is good for tasks that need to be run after the component has been dropped.
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> Task {
// The root scope will never be unmounted so we can just add the task at the top of the app
Runtime::with(|rt| rt.spawn(self.id, fut)).expect("Runtime to exist")
}
/// Mark this component as suspended on a specific task and then return None
pub fn suspend(&self, task: Task) -> Option<Element> {
self.last_suspendable_task.set(Some(task));