pub struct Memo<T> { /* private fields */ }Expand description
Equality/comparator-aware computed node.
Like Thunk, but a recomputation that produces an unchanged value (under PartialEq for
memo, or a custom comparator for memo_by) does not invalidate downstream observers.
Effects depending only on unchanged memos skip their re-runs entirely.
Clones share the same underlying node.
If the compute closure panics, the panic propagates to the reader and the node stays dirty, so the next read retries the computation.
§Panics
Reading a memo whose computation (transitively) reads itself panics with a
crate::ReactCycleError message describing the cycle path. To fold the memo’s own
previous value into the next one, use memo_with_prev.
Implementations§
Source§impl<T: 'static> Memo<T>
impl<T: 'static> Memo<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 computed value.
§Panics
Panics with a crate::ReactCycleError message when this memo’s computation
(transitively) reads itself (use memo_with_prev for access to the previous value).
A shared borrow of the cached value is held while f runs, so invalidating this memo
and reading it again from inside f also panics.
Source§impl<T: 'static> Memo<T>
impl<T: 'static> Memo<T>
Sourcepub fn id(&self) -> NodeId
pub fn id(&self) -> NodeId
Returns this memo’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).
Trait Implementations§
Source§impl<T: 'static> Observable for Memo<T>
impl<T: 'static> Observable for Memo<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.