Struct leptos::Scope

source ·
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

source

pub fn id(&self) -> ScopeId

The unique identifier for this scope.

source

pub fn ancestry(&self) -> Vec<ScopeId, Global>

Returns the chain of scope IDs beginning with this one, going to its parent, grandparents, etc.

source

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.)

source

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.)

source

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

source

pub fn dispose(self)

Disposes of this reactive scope.

This will

  1. dispose of all child Scopes
  2. run all cleanup functions defined for this scope by on_cleanup.
  3. dispose of all signals, effects, and resources owned by this Scope.
source

pub fn parent(&self) -> Option<Scope>

Returns the the parent Scope, if any.

source§

impl Scope

source

pub fn all_resources(&self) -> Vec<ResourceId, Global>

Returns IDs for all Resources found on any scope.

source

pub fn pending_resources(&self) -> Vec<ResourceId, Global>

Returns IDs for all Resources found on any scope that are pending from the server.

source

pub fn serialization_resolvers( &self ) -> FuturesUnordered<Pin<Box<dyn Future<Output = (ResourceId, String)> + 'static, Global>>>

Returns IDs for all Resources found on any scope.

source

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.

source

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 Futures that return content for out-of-order and in-order streaming, respectively.

source

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 Futures that return content for out-of-order and in-order streaming, respectively.

source

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 Clone for Scope

source§

fn clone(&self) -> Scope

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Scope

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl PartialEq<Scope> for Scope

source§

fn eq(&self, other: &Scope) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Scope

source§

impl Eq for Scope

source§

impl StructuralEq for Scope

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<El> ElementDescriptorBounds for Elwhere El: Debug,