pub struct Signal<T: ?Sized + 'static>(/* private fields */);
Expand description
Similar to Rc<dyn Fn() -> &T>
, but with added functionality to observe changes in the result.
Use the following methods to create an instance of Signal
.
- Methods of
Signal
- Methods of
SignalBuilder
ToSignal::to_signal
Implementations§
Source§impl<T: ?Sized + 'static> Signal<T>
impl<T: ?Sized + 'static> Signal<T>
Sourcepub fn new(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Selfwhere
T: Sized,
pub fn new(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Selfwhere
T: Sized,
Create a new Signal
from a function to get a value.
The signal created by this function also sends a notification when f
returns the same value as before.
new_dedup
must be used to avoid sending a notification when f
returns the same value as before.
Sourcepub fn new_dedup(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Self
pub fn new_dedup(f: impl Fn(&mut SignalContext<'_>) -> T + 'static) -> Self
Creates a new Signal
from a function to get a value, with deduplication.
The signal created by this function does not send a notification when f
returns the same value as before.
Even if the value is not changed, a “value may have changed” notification is sent to the dependants, so the overhead cannot be zero.
Sourcepub fn from_value(value: T) -> Selfwhere
T: Sized,
pub fn from_value(value: T) -> Selfwhere
T: Sized,
Create a new Signal
that does not change from its value.
Sourcepub fn from_value_map<U>(value: U, f: impl Fn(&U) -> &T + 'static) -> Selfwhere
U: 'static,
pub fn from_value_map<U>(value: U, f: impl Fn(&U) -> &T + 'static) -> Selfwhere
U: 'static,
Create a new Signal
that does not change from its value, with a mapping function.
Sourcepub fn from_owned(owned: impl Borrow<T> + 'static) -> Self
pub fn from_owned(owned: impl Borrow<T> + 'static) -> Self
Creates a new Signal
from an owned value.
Sourcepub fn from_node(node: Rc<impl SignalNode<Value = T>>) -> Self
pub fn from_node(node: Rc<impl SignalNode<Value = T>>) -> Self
Create a new Signal
by specifying the internal implementation.
Sourcepub fn from_borrow<U>(
this: U,
borrow: impl for<'s, 'a> Fn(&'a U, &mut SignalContext<'s>, &'a &'s ()) -> StateRef<'a, T> + 'static,
) -> Selfwhere
U: 'static,
pub fn from_borrow<U>(
this: U,
borrow: impl for<'s, 'a> Fn(&'a U, &mut SignalContext<'s>, &'a &'s ()) -> StateRef<'a, T> + 'static,
) -> Selfwhere
U: 'static,
Create a new Signal
from a function to get StateRef
.
Sourcepub fn from_static_ref(value: &'static T) -> Self
pub fn from_static_ref(value: &'static T) -> Self
Create a new Signal
that does not change from a static reference.
Sourcepub fn from_future(future: impl Future<Output = T> + 'static) -> Signal<Poll<T>>where
T: Sized,
pub fn from_future(future: impl Future<Output = T> + 'static) -> Signal<Poll<T>>where
T: Sized,
Create a new Signal
from a Future
.
Sourcepub fn from_stream(stream: impl Stream<Item = T> + 'static) -> Signal<Poll<T>>where
T: Sized,
pub fn from_stream(stream: impl Stream<Item = T> + 'static) -> Signal<Poll<T>>where
T: Sized,
Create a new Signal
from a Stream
.
Sourcepub fn from_async<Fut>(
f: impl Fn(AsyncSignalContext) -> Fut + 'static,
) -> Signal<Poll<T>>
pub fn from_async<Fut>( f: impl Fn(AsyncSignalContext) -> Fut + 'static, ) -> Signal<Poll<T>>
Create a Signal
from an asynchronous function to get a value.
Sourcepub fn borrow<'a, 's: 'a>(
&'a self,
sc: &mut SignalContext<'s>,
) -> StateRef<'a, T>
pub fn borrow<'a, 's: 'a>( &'a self, sc: &mut SignalContext<'s>, ) -> StateRef<'a, T>
Obtains a reference to the current value and adds a dependency on this Signal
to the specified SignalContext
.
If the current value has not yet been calculated, it will be calculated.
Sourcepub fn get(&self, sc: &mut SignalContext<'_>) -> <T as ToOwned>::Ownedwhere
T: ToOwned,
pub fn get(&self, sc: &mut SignalContext<'_>) -> <T as ToOwned>::Ownedwhere
T: ToOwned,
Gets the current value and adds a dependency on this Signal
to the specified SignalContext
.
If the current value has not yet been calculated, it will be calculated.
Sourcepub fn map<U: ?Sized>(&self, f: impl Fn(&T) -> &U + 'static) -> Signal<U>
pub fn map<U: ?Sized>(&self, f: impl Fn(&T) -> &U + 'static) -> Signal<U>
Creates a new Signal
whose references are transformed by the specified function.
Using SignalBuilder::map
, you can create similar Signal
more efficiently.
Sourcepub fn dedup(&self) -> Signal<T>
pub fn dedup(&self) -> Signal<T>
Create a Signal
that does not send notifications to the dependants if the value does not change.
Even if the value is not changed, a “value may have changed” notification is sent to the dependants, so the overhead cannot be zero.
Using SignalBuilder::dedup
, you can create similar Signal
more efficiently.
Sourcepub fn keep(&self) -> Signal<T>
pub fn keep(&self) -> Signal<T>
Create a Signal
that keeps a cache even if the subscriber does not exist.
Normally, Signal
discards the cache at the time Runtime::run_discards
is called if there are no subscribers.
Signals created by this method do not discards the cache even if there are no subscribers.
Using SignalBuilder::keep, you can create similar Signal more efficiently.
Sourcepub fn effect(&self, f: impl FnMut(&T) + 'static) -> Subscription
pub fn effect(&self, f: impl FnMut(&T) + 'static) -> Subscription
Subscribe to the value of this signal.
First, call the function with the current value, then call the function each time the value changes.
The function is called when Runtime::run_tasks
is called with None
or Some(TaskKind::default())
.
When the Subscription
returned by this function is dropped, the subscription is canceled.
Sourcepub fn effect_with(
&self,
f: impl FnMut(&T) + 'static,
kind: TaskKind,
) -> Subscription
pub fn effect_with( &self, f: impl FnMut(&T) + 'static, kind: TaskKind, ) -> Subscription
Subscribe to the value of this signal with specifying TaskKind
.
First, call the function with the current value, then call the function each time the value changes.
The function is called when Runtime::run_tasks
is called with None
or Some(kind)
.
When the Subscription
returned by this function is dropped, the subscription is canceled.
Sourcepub fn to_stream(&self) -> impl Stream<Item = T::Owned> + Unpin + 'staticwhere
T: ToOwned,
pub fn to_stream(&self) -> impl Stream<Item = T::Owned> + Unpin + 'staticwhere
T: ToOwned,
Create a Stream
to subscribe to the value of this signal.