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 implementFn()
. The closure is executed immediately upon creation and tracked for reactive updates.
Implementations§
Source§impl<F> Effect<F>where
F: Fn(),
impl<F> Effect<F>where
F: Fn(),
Sourcepub fn new(f: F) -> Rc<dyn IEffect>where
F: 'static,
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§
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> 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
Mutably borrows from an owned value. Read more