Struct Effect

Source
pub struct Effect<F>
where F: Fn(),
{ /* private fields */ }
Expand description

A reactive effect that runs a closure whenever its dependencies change.

Effect<F> behaves similarly to an “event listener” or a callback, but it is automatically tied to any signals or memos it reads during execution. When those dependencies change, the effect will re-run.

Note: The closure runs immediately upon creation via Effect::wrap, so the effect is always initialized with an up-to-date value.

In short:

  • Like a callback: wraps a closure of type F and runs it.
  • Adds tracking: automatically re-runs when dependent signals change.
  • Runs once immediately at creation.

§Type Parameters

  • F: The closure type wrapped by this effect. Must implement Fn(). The closure is executed immediately upon creation and tracked for reactive updates.

Implementations§

Source§

impl<F> Effect<F>
where F: Fn(),

Source

pub fn new(f: F) -> Rc<dyn IEffect>
where F: 'static,

Creates a new Effect, wrapping the provided closure and running it immediately for dependency tracking.

Returns an Rc<dyn IEffect> so the effect can be stored and shared as a non-generic trait object.

§Examples
use std::{cell::Cell, rc::Rc};
use reactive_cache::Effect;

let counter = Rc::new(Cell::new(0));
let c_clone = counter.clone();

let effect = Effect::new(move || {
    // This closure runs immediately
    c_clone.set(c_clone.get() + 1);
});

assert_eq!(counter.get(), 1);

Trait Implementations§

Source§

impl<F> IEffect for Effect<F>
where F: Fn(),

Source§

fn run(&self)

Runs the effect closure. Read more

Auto Trait Implementations§

§

impl<F> Freeze for Effect<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for Effect<F>
where F: RefUnwindSafe,

§

impl<F> Send for Effect<F>
where F: Send,

§

impl<F> Sync for Effect<F>
where F: Sync,

§

impl<F> Unpin for Effect<F>
where F: Unpin,

§

impl<F> UnwindSafe for Effect<F>
where F: UnwindSafe,

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.