From cd3ff6d3644347163c249da8293a76db02044d03 Mon Sep 17 00:00:00 2001 From: James Parker Date: Tue, 8 Jul 2025 10:07:51 -0400 Subject: [PATCH] Add `spawn_in_scope` --- packages/core/src/global_context.rs | 9 ++++++++- packages/core/src/lib.rs | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/core/src/global_context.rs b/packages/core/src/global_context.rs index 94f60ac38d..af67f74d31 100644 --- a/packages/core/src/global_context.rs +++ b/packages/core/src/global_context.rs @@ -204,7 +204,14 @@ pub fn queue_effect(f: impl FnOnce() + 'static) { /// #[doc = include_str!("../docs/common_spawn_errors.md")] pub fn spawn_forever(fut: impl Future + 'static) -> Option { - Runtime::with_scope(ScopeId::ROOT, |cx| cx.spawn(fut)).ok() + spawn_in_scope(ScopeId::ROOT, fut) +} + +/// Spawns a future in the provided scope. This task will automatically be canceled when the component's scope is dropped. +/// +#[doc = include_str!("../docs/common_spawn_errors.md")] +pub fn spawn_in_scope(scope: ScopeId, fut: impl Future + 'static) -> Option { + Runtime::with_scope(scope, |cx| cx.spawn(fut)).ok() } /// Informs the scheduler that this task is no longer needed and should be removed. diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index 97c5b0d947..956087d82e 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -95,7 +95,7 @@ pub mod prelude { consume_context, consume_context_from_scope, current_owner, current_scope_id, fc_to_builder, generation, has_context, needs_update, needs_update_any, parent_scope, provide_context, provide_error_boundary, provide_root_context, queue_effect, remove_future, - schedule_update, schedule_update_any, spawn, spawn_forever, spawn_isomorphic, suspend, + schedule_update, schedule_update_any, spawn, spawn_forever, spawn_in_scope, spawn_isomorphic, suspend, throw_error, try_consume_context, use_after_render, use_before_render, use_drop, use_hook, use_hook_with_cleanup, with_owner, AnyValue, Attribute, Callback, Component, ComponentFunction, Context, Element, ErrorBoundary, ErrorContext, Event, EventHandler,