ComputationNode
A memo or an effect: an owner that reads sources and (for memos) is itself a source.
A memo or an effect: an owner that reads sources and (for memos) is itself a source. Exported from
@shadow-js/core/extend as ComputationNode; create one with createComputation and read a memo
with readComputation. It owns what its function creates: each run first disposes the children
and runs the cleanups of the previous run.
Import#
ts
import { ComputationNode } from "@shadow-js/core/extend";
Signature#
ts
class Computation extends Owner implements Source
Members#
| Name | Type | Description |
|---|---|---|
kind |
kind: number |
1 for a memo, 2 for an effect. |
state |
state: number |
How stale the node is: 0 (clean), 1 (a transitive source changed), 2 (a direct source changed). |
flags |
flags: number |
Engine bit flags (queued, running, disposed, render phase, ...). Read-only outside the engine. |
value |
value: unknown |
A memo's last value; an effect's last computed value when it has an apply stage. |
error |
error: unknown |
The error a memo's last run threw, rethrown to its readers; undefined when it succeeded. |
equals |
equals: Equals<unknown> |
A memo's equality: readers are notified only when it says the new value differs. |
fn |
fn: ((previous: never) => unknown) | null |
The function each run calls with the previous value; null once disposed. |
apply |
apply: ((value: never, previous: never) => unknown) | null |
An effect's second stage, run untracked with the computed value and the previous one; null for none and once disposed. |
sources |
sources: Edges<Source> |
The sources the last run read, in reading order, as pairs: a source, then the index of this computation in that source's observers. Read it with sourcesOf, which returns the sources alone. |
observers |
observers: Edges<Computation> | null |
The computations that read this memo in their last run, as pairs (see Source.observers). |
stamp |
stamp |
Stamp of the current run while it tracks, and of the last run that tracked this memo. |
trackIndex |
trackIndex |
During a run: how many leading entries of sources were confirmed again (they keep their edges). |
seq |
seq: number |
Creation order; breaks ties between effects at equal depth (G-16). |
name |
name: string | undefined |
Label shown by devtools and diagnostics. |
Examples#
ts
import { createComputation, createSignalNode, readComputation, readSignal } from "@shadow-js/core/extend";
const price = createSignalNode(10, Object.is);
const total = createComputation({ kind: "memo", fn: () => readSignal(price) * 2 });
console.log(readComputation<number>(total)); // 20