pub trait SignalUpdate {
    type Value;

    // Required methods
    fn update(&self, f: impl FnOnce(&mut Self::Value));
    fn try_update<O>(&self, f: impl FnOnce(&mut Self::Value) -> O) -> Option<O>;
}
Expand description

This trait allows updating the inner value of a signal.

Required Associated Types§

source

type Value

The value held by the signal.

Required Methods§

source

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

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 Self::Value) -> 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.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl SignalUpdate for Trigger

§

type Value = ()

source§

impl<S, T> SignalUpdate for Resource<S, T>

§

type Value = Option<T>

source§

impl<T> SignalUpdate for RwSignal<T>

§Examples

let count = create_rw_signal(0);

// notifies subscribers
count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 1);
§

type Value = T

source§

impl<T> SignalUpdate for WriteSignal<T>

§Examples

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

// notifies subscribers
set_count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 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.get(), 1);
§

type Value = T