pub struct Scope { /* private fields */ }
Expand description
A Each scope can have child scopes, and may in turn have a parent.
Scopes manage memory within the reactive system. When a scope is disposed, its cleanup functions run and the signals, effects, memos, resources, and contexts associated with it no longer exist and should no longer be accessed.
You generally won’t need to create your own scopes when writing application code. However, they’re very useful for managing control flow within an application or library. For example, if you are writing a keyed list component, you will want to create a child scope for each row in the list so that you can dispose of its associated signals, etc. when it is removed from the list.
Every other function in this crate takes a Scope
as its first argument. Since Scope
is Copy and 'static
this does not add much overhead or lifetime complexity.
Implementations§
source§impl Scope
impl Scope
sourcepub fn child_scope(self, f: impl FnOnce(Scope)) -> ScopeDisposer
pub fn child_scope(self, f: impl FnOnce(Scope)) -> ScopeDisposer
Creates a child scope and runs the given function within it, returning a handle to dispose of it.
The child scope has its own lifetime and disposer, but will be disposed when the parent is disposed, if it has not been already.
This is useful for applications like a list or a router, which may want to create child scopes and dispose of them when they are no longer needed (e.g., a list item has been destroyed or the user has navigated away from the route.)
sourcepub fn run_child_scope<T>(self, f: impl FnOnce(Scope) -> T) -> (T, ScopeDisposer)
pub fn run_child_scope<T>(self, f: impl FnOnce(Scope) -> T) -> (T, ScopeDisposer)
Creates a child scope and runs the given function within it, returning the function’s return type and a handle to dispose of it.
The child scope has its own lifetime and disposer, but will be disposed when the parent is disposed, if it has not been already.
This is useful for applications like a list or a router, which may want to create child scopes and dispose of them when they are no longer needed (e.g., a list item has been destroyed or the user has navigated away from the route.)
sourcepub fn untrack<T>(&self, f: impl FnOnce() -> T) -> T
pub fn untrack<T>(&self, f: impl FnOnce() -> T) -> T
Suspends reactive tracking while running the given function.
This can be used to isolate parts of the reactive graph from one another.
let (a, set_a) = create_signal(cx, 0);
let (b, set_b) = create_signal(cx, 0);
let c = create_memo(cx, move |_| {
// this memo will *only* update when `a` changes
a() + cx.untrack(move || b())
});
assert_eq!(c(), 0);
set_a(1);
assert_eq!(c(), 1);
set_b(1);
// hasn't updated, because we untracked before reading b
assert_eq!(c(), 1);
set_a(2);
assert_eq!(c(), 3);
source§impl Scope
impl Scope
sourcepub fn dispose(self)
pub fn dispose(self)
Disposes of this reactive scope.
This will
- dispose of all child
Scope
s - run all cleanup functions defined for this scope by on_cleanup.
- dispose of all signals, effects, and resources owned by this
Scope
.
source§impl Scope
impl Scope
sourcepub fn all_resources(&self) -> Vec<ResourceId, Global> ⓘ
pub fn all_resources(&self) -> Vec<ResourceId, Global> ⓘ
Returns IDs for all Resources found on any scope.
sourcepub fn pending_resources(&self) -> Vec<ResourceId, Global> ⓘ
pub fn pending_resources(&self) -> Vec<ResourceId, Global> ⓘ
Returns IDs for all Resources found on any scope that are pending from the server.
sourcepub fn serialization_resolvers(
&self
) -> FuturesUnordered<Pin<Box<dyn Future<Output = (ResourceId, String)> + 'static, Global>>>
pub fn serialization_resolvers(
&self
) -> FuturesUnordered<Pin<Box<dyn Future<Output = (ResourceId, String)> + 'static, Global>>>
Returns IDs for all Resources found on any scope.
sourcepub fn register_suspense(
&self,
context: SuspenseContext,
key_before_suspense: &str,
key: &str,
resolver: impl FnOnce() -> String + 'static
)
pub fn register_suspense(
&self,
context: SuspenseContext,
key_before_suspense: &str,
key: &str,
resolver: impl FnOnce() -> String + 'static
)
Registers the given SuspenseContext with the current scope,
calling the resolver
when its resources are all resolved.
sourcepub fn pending_fragments(
&self
) -> HashMap<String, (String, Pin<Box<dyn Future<Output = String> + 'static, Global>>), RandomState>
pub fn pending_fragments(
&self
) -> HashMap<String, (String, Pin<Box<dyn Future<Output = String> + 'static, Global>>), RandomState>
The set of all HTML fragments currently pending.
Returns a tuple of the hydration ID of the previous element, and a pinned Future
that will yield the
<Suspense/>
HTML when all resources are resolved.