Skip to main content

ReactiveScope

Struct ReactiveScope 

Source
pub struct ReactiveScope { /* private fields */ }
Expand description

A scope that owns reactive effects and cleanup callbacks.

When a scope is dropped, all effects it owns are disposed and all cleanup callbacks are run in reverse registration order. Child scopes are dropped before their parent’s cleanups run.

§Examples

let mut scope = ReactiveScope::new();
let count = Signal::new(0);

scope.create_effect({
    let count = count.clone();
    move || println!("count = {}", count.get())
});

// Effect runs on creation and when count changes.
count.set(1);

drop(scope); // Effect is disposed, cleanup runs.
count.set(2); // Effect does NOT run.

Implementations§

Source§

impl ReactiveScope

Source

pub fn new() -> Self

Create a new empty reactive scope.

Source

pub fn create_signal<T>(&self, value: T) -> Signal<T>

Create a signal. Convenience method — signals are not owned by the scope since they are shared handles.

Source

pub fn create_computed<T: Clone + 'static>( &self, f: impl Fn() -> T + 'static, ) -> Computed<T>

Create a computed value. Convenience method — computed values are shared handles and not owned by the scope.

Source

pub fn create_effect(&mut self, f: impl FnMut() + 'static) -> Effect

Create an effect owned by this scope.

The effect will be automatically disposed when the scope is dropped.

Source

pub fn on_cleanup(&mut self, f: impl FnOnce() + 'static)

Register a cleanup callback to run when this scope is dropped.

Callbacks are run in reverse registration order.

Source

pub fn child(&mut self) -> &mut ReactiveScope

Create a nested child scope.

The child scope will be dropped before the parent’s cleanups run.

Source

pub fn effect_count(&self) -> usize

Get the number of effects owned by this scope.

Source

pub fn child_count(&self) -> usize

Get the number of child scopes.

Trait Implementations§

Source§

impl Default for ReactiveScope

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for ReactiveScope

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.