Skip to main content

Module computed

Module computed 

Source
Expand description

Lazy computed values that auto-update from Observable dependencies.

§Design

Computed<T> wraps a compute function and its cached result in shared, reference-counted storage. When any dependency changes, the cached value is invalidated (marked dirty). The next call to get() recomputes and caches the result.

§Invariants

  1. get() always returns a value consistent with the current state of all dependencies (no stale reads after a dependency mutation completes).
  2. The compute function is called at most once per dependency change cycle (memoization).
  3. If no dependency has changed, get() returns the cached value in O(1).
  4. Version increments by exactly 1 per recomputation.

§Failure Modes

  • Compute function panics: The cached value remains from the last successful computation. The dirty flag stays set so the next get() will retry.
  • Dependency dropped: If the source Observable is dropped, the subscription becomes inert. The computed value retains its last cached result and never becomes dirty again from that source.

Structs§

Computed
A lazily-evaluated, memoized value derived from one or more Observable dependencies.