pub struct Computed<T> { /* private fields */ }Expand description
A lazily-evaluated, memoized value derived from one or more Observable
dependencies.
Cloning a Computed creates a new handle to the same inner state.
§Invariants
dirtyis true after any dependency changes and beforeget().versionincrements by 1 on each recomputation.- The compute function is called only when
dirtyis true andget()is called.
Implementations§
Source§impl<T: Clone + 'static> Computed<T>
impl<T: Clone + 'static> Computed<T>
Sourcepub fn from_observable<S: Clone + PartialEq + 'static>(
source: &Observable<S>,
map: impl Fn(&S) -> T + 'static,
) -> Self
pub fn from_observable<S: Clone + PartialEq + 'static>( source: &Observable<S>, map: impl Fn(&S) -> T + 'static, ) -> Self
Create a computed value derived from a single observable.
The map function receives a reference to the source value and
returns the derived value.
Sourcepub fn from2<S1, S2>(
s1: &Observable<S1>,
s2: &Observable<S2>,
map: impl Fn(&S1, &S2) -> T + 'static,
) -> Self
pub fn from2<S1, S2>( s1: &Observable<S1>, s2: &Observable<S2>, map: impl Fn(&S1, &S2) -> T + 'static, ) -> Self
Create a computed value derived from two observables.
Sourcepub fn from3<S1, S2, S3>(
s1: &Observable<S1>,
s2: &Observable<S2>,
s3: &Observable<S3>,
map: impl Fn(&S1, &S2, &S3) -> T + 'static,
) -> Self
pub fn from3<S1, S2, S3>( s1: &Observable<S1>, s2: &Observable<S2>, s3: &Observable<S3>, map: impl Fn(&S1, &S2, &S3) -> T + 'static, ) -> Self
Create a computed value derived from three observables.
Sourcepub fn from_fn(
compute: impl Fn() -> T + 'static,
subscriptions: Vec<Subscription>,
) -> Self
pub fn from_fn( compute: impl Fn() -> T + 'static, subscriptions: Vec<Subscription>, ) -> Self
Create a computed value from a standalone compute function and pre-built subscriptions.
This is the low-level constructor for advanced use cases where the caller manages dependency subscriptions manually.
Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Get the current value, recomputing if any dependency has changed.
Returns a clone of the cached value. If the value is dirty, the compute function is called first and the result is cached.
Sourcepub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
pub fn with<R>(&self, f: impl FnOnce(&T) -> R) -> R
Access the current value by reference without cloning.
Forces recomputation if dirty. The closure receives an immutable reference to the cached value.
§Panics
Panics if the closure attempts to call get() on the same
Computed (re-entrant borrow).
Sourcepub fn invalidate(&self)
pub fn invalidate(&self)
Force invalidation of the cached value. The next get() will
recompute.