pub struct ImmediateEffect { /* private fields */ }Expand description
Effects run a certain chunk of code whenever the signals they depend on change.
The effect runs on creation and again as soon as any tracked signal changes.
NOTE: you probably want use Effect instead.
This is for the few cases where it’s important to execute effects immediately and in order.
ImmediateEffects stop running when dropped.
NOTE: since effects are executed immediately, they might recurse. Under recursion or parallelism only the last run to start is tracked.
§Example
let a = RwSignal::new(0);
let b = RwSignal::new(0);
// ✅ use effects to interact between reactive state and the outside world
let _drop_guard = ImmediateEffect::new(move || {
// on the next “tick” prints "Value: 0" and subscribes to `a`
println!("Value: {}", a.get());
});
// The effect runs immediately and subscribes to `a`, in the process it prints "Value: 0"
a.set(1);
// ✅ because it's subscribed to `a`, the effect reruns and prints "Value: 1"§Notes
- Scheduling: Effects run immediately, as soon as any tracked signal changes.
- By default, effects do not run unless the
effectsfeature is enabled. If you are using this with a web framework, this generally means that effects do not run on the server. and you can call browser-specific APIs within the effect function without causing issues. If you need an effect to run on the server, useImmediateEffect::new_isomorphic.
Implementations§
Source§impl ImmediateEffect
impl ImmediateEffect
Sourcepub fn new(fun: impl Fn() + Send + Sync + 'static) -> ImmediateEffect
pub fn new(fun: impl Fn() + Send + Sync + 'static) -> ImmediateEffect
Creates a new effect which runs immediately, then again as soon as any tracked signal changes. (Unless batch is used.)
NOTE: this requires a Fn function because it might recurse.
Use Self::new_mut to pass a FnMut function, it’ll panic on recursion.
Sourcepub fn new_scoped(fun: impl Fn() + Send + Sync + 'static)
pub fn new_scoped(fun: impl Fn() + Send + Sync + 'static)
Creates a new effect which runs immediately, then again as soon as any tracked signal changes. (Unless batch is used.)
NOTE: this requires a Fn function because it might recurse.
Use Self::new_mut_scoped to pass a FnMut function, it’ll panic on recursion.
NOTE: this effect is automatically cleaned up when the current owner is cleared or disposed.
Sourcepub fn new_mut_scoped(fun: impl FnMut() + Send + Sync + 'static)
pub fn new_mut_scoped(fun: impl FnMut() + Send + Sync + 'static)
Creates a new effect which runs immediately, then again as soon as any tracked signal changes. (Unless batch is used.)
NOTE: this effect is automatically cleaned up when the current owner is cleared or disposed.
§Panics
Panics on recursion or if triggered in parallel. Also see Self::new_scoped
Sourcepub fn new_isomorphic(fun: impl Fn() + Send + Sync + 'static) -> ImmediateEffect
pub fn new_isomorphic(fun: impl Fn() + Send + Sync + 'static) -> ImmediateEffect
Creates a new effect which runs immediately, then again as soon as any tracked signal changes.
This will run whether the effects feature is enabled or not.
Trait Implementations§
Source§impl Clone for ImmediateEffect
impl Clone for ImmediateEffect
Source§fn clone(&self) -> ImmediateEffect
fn clone(&self) -> ImmediateEffect
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ImmediateEffect
impl Debug for ImmediateEffect
Source§impl DefinedAt for ImmediateEffect
impl DefinedAt for ImmediateEffect
Source§fn defined_at(&self) -> Option<&'static Location<'static>>
fn defined_at(&self) -> Option<&'static Location<'static>>
None in
release mode.Source§impl Dispose for ImmediateEffect
impl Dispose for ImmediateEffect
Source§impl ToAnySubscriber for ImmediateEffect
impl ToAnySubscriber for ImmediateEffect
Source§fn to_any_subscriber(&self) -> AnySubscriber
fn to_any_subscriber(&self) -> AnySubscriber
Auto Trait Implementations§
impl Freeze for ImmediateEffect
impl RefUnwindSafe for ImmediateEffect
impl Send for ImmediateEffect
impl Sync for ImmediateEffect
impl Unpin for ImmediateEffect
impl UnwindSafe for ImmediateEffect
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more