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 ancestry(&self) -> Vec<ScopeId, Global>
pub fn ancestry(&self) -> Vec<ScopeId, Global>
Returns the chain of scope IDs beginning with this one, going to its parent, grandparents, etc.
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: &str,
out_of_order_resolver: impl FnOnce() -> String + 'static,
in_order_resolver: impl FnOnce() -> Vec<StreamChunk, Global> + 'static
)
pub fn register_suspense( &self, context: SuspenseContext, key: &str, out_of_order_resolver: impl FnOnce() -> String + 'static, in_order_resolver: impl FnOnce() -> Vec<StreamChunk, Global> + '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, (Pin<Box<dyn Future<Output = String> + 'static, Global>>, Pin<Box<dyn Future<Output = Vec<StreamChunk, Global>> + 'static, Global>>), RandomState>
pub fn pending_fragments( &self ) -> HashMap<String, (Pin<Box<dyn Future<Output = String> + 'static, Global>>, Pin<Box<dyn Future<Output = Vec<StreamChunk, Global>> + 'static, Global>>), RandomState>
The set of all HTML fragments currently pending.
The keys are hydration IDs. Values are tuples of two pinned
Future
s that return content for out-of-order and in-order streaming, respectively.
sourcepub fn take_pending_fragment(
&self,
id: &str
) -> Option<(Pin<Box<dyn Future<Output = String> + 'static, Global>>, Pin<Box<dyn Future<Output = Vec<StreamChunk, Global>> + 'static, Global>>)>
pub fn take_pending_fragment( &self, id: &str ) -> Option<(Pin<Box<dyn Future<Output = String> + 'static, Global>>, Pin<Box<dyn Future<Output = Vec<StreamChunk, Global>> + 'static, Global>>)>
Takes the pending HTML for a single <Suspense/>
node.
Returns a tuple of two pinned Future
s that return content for out-of-order
and in-order streaming, respectively.
sourcepub fn batch<T>(&self, f: impl FnOnce() -> T) -> T
pub fn batch<T>(&self, f: impl FnOnce() -> T) -> T
Batches any reactive updates, preventing effects from running until the whole function has run. This allows you to prevent rerunning effects if multiple signal updates might cause the same effect to run.
Panics
Panics if the runtime this scope belongs to has already been disposed.
Trait Implementations§
source§impl PartialEq<Scope> for Scope
impl PartialEq<Scope> for Scope
impl Copy for Scope
impl Eq for Scope
impl StructuralEq for Scope
impl StructuralPartialEq for Scope
Auto Trait Implementations§
impl RefUnwindSafe for Scope
impl Send for Scope
impl Sync for Scope
impl Unpin for Scope
impl UnwindSafe for Scope
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.