Computed
Derived signals that automatically recompute when their dependencies change.
Derived values that automatically update when their dependencies change.
Creating Computed Values
#Computed.make(fn)
#Creates a computed value from a function. The function is called lazily and cached until a dependency changes.
let count = Signal.make(5)
let doubled = Computed.make(() => Signal.get(count) * 2)
Computed.get(doubled) // 10
Signal.set(count, 10)
Computed.get(doubled) // 20Computed.make(~name, fn)
#Creates a named computed for debugging.
let doubled = Computed.make(~name="doubled", () => {
Signal.get(count) * 2
})Reading Computed Values
#Computed.get(computed)
#Reads the computed value and creates a dependency. Can be used inside other computeds or effects.
let count = Signal.make(5)
let doubled = Computed.make(() => Signal.get(count) * 2)
let quadrupled = Computed.make(() => Computed.get(doubled) * 2)
Computed.get(quadrupled) // 20Computed.peek(computed)
#Reads the computed value without creating a dependency.
let value = Computed.peek(doubled) // No dependency createdDisposal
#Computed.dispose(computed)
#Manually disposes a computed, removing all subscriptions. The computed will no longer track dependencies.
let computed = Computed.make(() => Signal.get(count) * 2)
// Later, when no longer needed
Computed.dispose(computed)Key Characteristics
#- Lazy Evaluation — Computed values are not calculated until they are first read. This avoids unnecessary computation.
- Automatic Caching — Once calculated, the value is cached until a dependency changes. Multiple reads return the cached value.
- Dependency Tracking — Dependencies are automatically tracked when Signal.get() or Computed.get() is called inside the computation function.
- Glitch-Free — Updates are batched and computed values are recalculated in topological order to prevent intermediate inconsistent states.
Was this page helpful?