pub struct Signal<T> { /* private fields */ }Expand description
Mutable source node in the reactive graph.
Reading a signal from inside an observer (an effect, thunk, or memo computation) records a dependency; writing to it marks those observers stale — thunks and memos recompute on their next read, and effects are queued for the next microtask flush. Clones share the same underlying node, so a signal can be captured by any number of closures. The node lives as long as any clone does.
Writes through set are suppressed when the new value equals the old one,
which is what allows convergent feedback (e.g. an effect clamping a value it reads) to
settle. replace and update always mark dependents
stale.
Implementations§
Source§impl<T: 'static> Signal<T>
impl<T: 'static> Signal<T>
Sourcepub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
Runs f with a shared reference to the current value, recording a dependency for the
currently running observer.
§Panics
A shared borrow is held while f runs: writing this same signal (set, replace, or
update) from inside f panics with a borrow error.
Sourcepub fn id(&self) -> NodeId
pub fn id(&self) -> NodeId
Returns this signal’s node id, for use with the graph queries on
Reactor — observer_count,
dependencies_of, node_origin and
friends. Ids are unique within one reactor, so aggregate on
(Reactor::id, id).
Sourcepub fn replace(&self, value: T) -> T
pub fn replace(&self, value: T) -> T
Replaces the current value and marks dependents stale, even when the new value equals
the old one (compare set).
§Panics
The value is swapped through a mutable borrow, so writing this same signal from inside a
closure that already holds one — with, with_peek
or update on this signal — panics with a borrow error. The panic is
std’s bare RefCell already borrowed, but #[track_caller] attributes it to this call
site, so the offending write is named by line in every build.
Sourcepub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
pub fn update<R>(&self, f: impl FnOnce(&mut T) -> R) -> R
Mutates the current value in place and marks dependents stale, regardless of whether
f actually changed anything (compare set).
§Panics
f runs while the value is mutably borrowed: any read or write of the same signal
from inside f (get, with, peek, set, replace, or a nested update) panics
with a borrow error.
Source§impl<T: PartialEq + 'static> Signal<T>
impl<T: PartialEq + 'static> Signal<T>
Sourcepub fn set(&self, value: T) -> Option<T>
pub fn set(&self, value: T) -> Option<T>
Sets the signal to value, suppressing unchanged writes.
Returns the previous value if the signal changed, or None when the new value was equal to
the old one.
The equality check runs untracked, so a PartialEq implementation that reads reactive
state records no dependencies for the currently running observer.
§Panics
A write borrows the value twice over: the equality check takes a shared borrow, and a write
that survives it takes a mutable one. So writing this same signal from inside a closure that
already holds a borrow — with, with_peek or
update on this signal — panics with a borrow error, as does a
PartialEq implementation that writes the very signal it was asked to compare. The panic is
std’s bare RefCell already borrowed, but #[track_caller] attributes it to this call
site, so the offending write is named by line in every build.
Trait Implementations§
Source§impl<T: 'static> Observable for Signal<T>
impl<T: 'static> Observable for Signal<T>
Source§fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
f with a shared reference to the current value, recording a dependency for the
currently running observer.Source§fn with_peek<R>(&self, f: impl FnOnce(&T) -> R) -> R
fn with_peek<R>(&self, f: impl FnOnce(&T) -> R) -> R
f with a shared reference to the current value without recording a dependency.