pub trait SignalUpdate<T> {
    // Required methods
    fn update(&self, f: impl FnOnce(&mut T));
    fn try_update<O>(&self, f: impl FnOnce(&mut T) -> O) -> Option<O>;

    // Provided method
    fn update_returning<O>(&self, f: impl FnOnce(&mut T) -> O) -> Option<O> { ... }
}
Expand description

This trait allows updating the inner value of a signal.

Required Methods§

source

fn update(&self, f: impl FnOnce(&mut T))

Applies a function to the current value to mutate it in place and notifies subscribers that the signal has changed.

Note: update() does not auto-memoize, i.e., it will notify subscribers even if the value has not actually changed.

source

fn try_update<O>(&self, f: impl FnOnce(&mut T) -> O) -> Option<O>

Applies a function to the current value to mutate it in place and notifies subscribers that the signal has changed. Returns [Some(O)] if the signal is still valid, None otherwise.

Note: update() does not auto-memoize, i.e., it will notify subscribers even if the value has not actually changed.

Provided Methods§

source

fn update_returning<O>(&self, f: impl FnOnce(&mut T) -> O) -> Option<O>

👎Deprecated: Please use try_update instead. This method will be removed in a future version of this crate

Applies a function to the current value to mutate it in place and notifies subscribers that the signal has changed. Returns [Some(O)] if the signal is still valid, None otherwise.

Note: update() does not auto-memoize, i.e., it will notify subscribers even if the value has not actually changed.

Implementors§

source§

impl<T> SignalUpdate<T> for RwSignal<T>

Examples

let count = create_rw_signal(cx, 0);

// notifies subscribers
count.update(|n| *n = 1); // it's easier just to call set_count(1), though!
assert_eq!(count(), 1);

// you can include arbitrary logic in this update function
// also notifies subscribers, even though the value hasn't changed
count.update(|n| {
    if *n > 3 {
        *n += 1
    }
});
assert_eq!(count(), 1);
source§

impl<T> SignalUpdate<T> for WriteSignal<T>

Examples

let (count, set_count) = create_signal(cx, 0);

// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count(1), though!
assert_eq!(count(), 1);

// you can include arbitrary logic in this update function
// also notifies subscribers, even though the value hasn't changed
set_count.update(|n| if *n > 3 { *n += 1 });
assert_eq!(count(), 1);