Skip to main content

Effect

Struct Effect 

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

A side effect that runs when its dependencies change

Effects automatically track which signals they read and re-run when any of those signals change. This is the core of the reactive system.

§Example

let count = signal(0);

// This effect automatically tracks `count` as a dependency
let _effect = Effect::new(move || {
    println!("Count: {}", count.get());
});

count.set(1); // Effect re-runs, prints "Count: 1"
count.set(2); // Effect re-runs, prints "Count: 2"

§Thread Safety

Effect closures must be Send + Sync since Signals are now thread-safe. This is automatically satisfied when capturing Signals.

Implementations§

Source§

impl Effect

Source

pub fn new(f: impl Fn() + Send + Sync + 'static) -> Self

Create a new effect that runs immediately with automatic dependency tracking

Source

pub fn lazy(f: impl Fn() + Send + Sync + 'static) -> Self

Create an effect without running immediately (lazy initialization)

Source

pub fn run(&self)

Run the effect if active (manual run, also tracks dependencies)

Source

pub fn stop(&self)

Stop the effect from running and clear its dependencies

Source

pub fn resume(&self)

Resume the effect (will need to be run manually to re-establish dependencies)

Source

pub fn is_active(&self) -> bool

Check if effect is active

Source

pub fn id(&self) -> SubscriberId

Get the effect’s unique ID

Trait Implementations§

Source§

impl Drop for Effect

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.