Signal

Trait Signal 

Source
pub trait Signal<T: 'static>: 'static {
    // Required methods
    fn get(&self) -> Ref<'_, T>;
    fn set_value(&self, value: T);
    fn listen(self, listener: Box<dyn Fn(Ref<'_, T>)>) -> Self
       where Self: Sized;
    fn notify(&self);
    fn dyn_clone(&self) -> Box<dyn Signal<T>>;

    // Provided methods
    fn set(&self, value: T) { ... }
    fn maybe(&self) -> MaybeSignal<T>
       where Self: Sized { ... }
    fn map<U: 'static>(
        &self,
        map: impl Fn(Ref<'_, T>) -> Ref<'_, U> + 'static,
    ) -> MapSignal<T, U>
       where Self: Sized { ... }
    fn hook(self, context: &AppContext) -> Self
       where Self: Sized { ... }
}
Expand description

Base signal trait.

Signals store values of type T and notify listeners when they change.

NOTE: By default, signals don’t have any listeners. To “hook” a signal into the application cycle, call use_signal.

§Avoiding Borrowing Errors

Be careful not to write something like signal.set(*signal.get());, as many signals that use Rc or RefCell might panic, due to the value already being borrowed (by the signal.get(); call). Write let value = *signal.get(); or let value = signal.clone() and then signal.set(value); instead.

Required Methods§

Source

fn get(&self) -> Ref<'_, T>

Get a reference to the current value of the signal.

Source

fn set_value(&self, value: T)

Set the value of the signal.

NOTE: This does not notify listeners, use [set] instead.

Source

fn listen(self, listener: Box<dyn Fn(Ref<'_, T>)>) -> Self
where Self: Sized,

Add a listener to the signal, which will be called when the inner value changes and returns the signal.

Source

fn notify(&self)

Notify listeners that the inner value has changed. May also be called manually to update listeners.

Source

fn dyn_clone(&self) -> Box<dyn Signal<T>>

Clones the signal into a Box<dyn Signal<T>>.

This is an object-safe alternative to simply making the signal Clone-dependent.

Provided Methods§

Source

fn set(&self, value: T)

Set the value of the signal and notify listeners.

Source

fn maybe(&self) -> MaybeSignal<T>
where Self: Sized,

Converts the signal into a MaybeSignal.

Source

fn map<U: 'static>( &self, map: impl Fn(Ref<'_, T>) -> Ref<'_, U> + 'static, ) -> MapSignal<T, U>
where Self: Sized,

Converts this signal into a MapSignal and applies the given mapping function.

Source

fn hook(self, context: &AppContext) -> Self
where Self: Sized,

Hooks the signal into the given AppContext.

Required for the signal to become reactive with the app lifecycle.

Implementors§

Source§

impl<T: 'static> Signal<T> for EvalSignal<T>

Source§

impl<T: 'static> Signal<T> for FixedSignal<T>

Source§

impl<T: 'static> Signal<T> for MemoizedSignal<T>

Source§

impl<T: 'static> Signal<T> for StateSignal<T>

Source§

impl<T: 'static, U: 'static> Signal<U> for MapSignal<T, U>