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§
Sourcefn set_value(&self, value: T)
fn set_value(&self, value: T)
Set the value of the signal.
NOTE: This does not notify listeners, use [set] instead.
Sourcefn listen(self, listener: Box<dyn Fn(Ref<'_, T>)>) -> Selfwhere
Self: Sized,
fn listen(self, listener: Box<dyn Fn(Ref<'_, T>)>) -> Selfwhere
Self: Sized,
Add a listener to the signal, which will be called when the inner value changes and returns the signal.
Provided Methods§
Sourcefn maybe(&self) -> MaybeSignal<T>where
Self: Sized,
fn maybe(&self) -> MaybeSignal<T>where
Self: Sized,
Converts the signal into a MaybeSignal.
Sourcefn map<U: 'static>(
&self,
map: impl Fn(Ref<'_, T>) -> Ref<'_, U> + 'static,
) -> MapSignal<T, U>where
Self: Sized,
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.
Sourcefn hook(self, context: &AppContext) -> Selfwhere
Self: Sized,
fn hook(self, context: &AppContext) -> Selfwhere
Self: Sized,
Hooks the signal into the given AppContext.
Required for the signal to become reactive with the app lifecycle.