Computed
See source codeTable of contents
Extends Signal<Value, Diff>
.
A computed signal created via the computed
function or @computed
decorator.
Computed signals derive their values from other signals and automatically update when their dependencies change.
They use lazy evaluation, only recalculating when accessed and dependencies have changed.
interface Computed<Value, Diff = unknown> extends Signal<Value, Diff> {}
Example
const firstName = atom('firstName', 'John')
const lastName = atom('lastName', 'Doe')
const fullName = computed(
'fullName',
() => `${firstName.get()} ${lastName.get()}`
)
console.log(fullName.get()) // "John Doe"
firstName.set('Jane')
console.log(fullName.get()) // "Jane Doe"
Properties
isActivelyListening
Whether this computed signal is involved in an actively-running effect graph. Returns true if there are any reactions or other computed signals depending on this one.
readonly isActivelyListening: boolean
lastChangedEpoch
The global epoch number when this signal's value last changed.
Note that this represents when the value actually changed, not when it was last computed. A computed signal may recalculate and produce the same value without changing its epoch. This is used internally for dependency tracking and history management.
lastChangedEpoch: number
name
A human-readable identifier for this signal, used primarily for debugging and performance profiling.
The name is displayed in debug output from whyAmIRunning and other diagnostic tools. It does not need to be globally unique within your application.
name: string
Methods
get
Gets the current value of the signal and establishes a dependency relationship.
When called from within a computed signal or effect, this signal will be automatically tracked as a dependency. If this signal's value changes, any dependent computations or effects will be marked for re-execution.
getDiffSince
Gets the sequence of diffs that occurred between a specific epoch and the current state.
This method enables incremental synchronization by providing a list of changes that have occurred since a specific point in time. If the requested epoch is too far in the past or the signal doesn't have enough history, it returns the unique symbol RESET_VALUE to indicate that a full state reset is required.
Parameters
Name | Description |
---|---|
|
The epoch timestamp to get diffs since |
Returns
Diff[] | RESET_VALUE
An array of diff objects representing changes since the epoch, or the unique symbol RESET_VALUE if insufficient history is available