pico-js
The TypeScript package @isograph/pico caches values computed from changing inputs. Each computation records the nodes it reads. When a source changes, pico-js uses those dependencies to decide which results it needs to recompute.
Priorities
pico-js prioritizes speed over convenience. The caller decides when a source value has changed and writes the new value. A computation can return its previous result when the new result is equivalent, letting pico-js skip dependent work with a === check. The contracts describe what callers must provide. Puchi wraps Pico with callable signals, callback-style batches, and error handling.
Getting started
Install the build from Isograph’s main branch. The functions in both examples are named exports of @isograph/pico.
npm install @isograph/pico@main
Read a cached computation
doubled returns twice the value of number. A retain keeps the computed value cached between reads.
const number = createSourceNode(2);
const doubled = createDerivedNode(
(callingNode) => 2 * readSource(callingNode, number),
);
const retain = retainDerivedNode(doubled); // Computes 4 for the first time.
readDerivedOutsideComputation(doubled); // 4. The computation did not run again.
readDerivedOutsideComputation(doubled); // 4. The computation did not run again.
writeSource(number, 3); // Marks doubled for recomputation.
readDerivedOutsideComputation(doubled); // 6. The computation ran again.
readDerivedOutsideComputation(doubled); // 6. The computation did not run again.
releaseRetain(retain);
number.value returns the most recently committed value. Inside the computation, readSource(callingNode, number) also records the dependency on number.
React to a change
A cart total depends on two sources. An onChange callback receives the new total after both writes in the batch take effect. Use a separate read to display the initial total.
const price = createSourceNode(10);
const quantity = createSourceNode(2);
const total = createDerivedNode(
(caller) => readSource(caller, price) * readSource(caller, quantity),
);
const subscription = retainDerivedNode(total, (value) => console.log(value));
readDerivedOutsideComputation(total); // 20. The callback has not run yet.
startWriteBatch();
try {
writeSource(price, 12);
writeSource(quantity, 3);
} finally {
endWriteBatch(); // Logs 36 once, before this call returns.
}
releaseRetain(subscription);
writeSource(quantity, 4); // The released subscription receives no further updates.
API
The Pico API reference lists each function, its return value, and where you can call it. Disposing nodes explains when Pico drops cached results and how to customize cleanup.
Advanced usage
Returning the previous value
A computation receives its previous result as its second argument. Returning that same value lets dependents reuse their cached results unless another dependency changed. This matters for operations such as filter, which allocate a new array even if all the elements stay the same.
const numbers = createSourceNode([1, 2, 3, 4]);
const evens = createDerivedNode<ReadonlyArray<number>>(
(callingNode, previous) => {
const next = readSource(callingNode, numbers).filter((n) => n % 2 === 0);
if (
previous?.length === next.length &&
next.every((n, i) => n === previous[i])
) {
return previous;
}
return next;
},
);
filter returns a new array every time, so without the check every update to numbers would give evens a new value. With it, updating numbers to [1, 2, 5, 4] leaves evens as the same [2, 4] array, and a node that reads only evens does not recompute.
Changing dependencies
A computation records only the nodes it reads on its most recent run. In this example, selected initially depends on useLeft and left. After the switch, writes to left no longer invalidate it.
const useLeft = createSourceNode(true);
const left = createSourceNode(10);
const right = createSourceNode(20);
const selected = createDerivedNode((callingNode) =>
readSource(callingNode, useLeft)
? readSource(callingNode, left)
: readSource(callingNode, right),
);
const selection = retainDerivedNode(selected);
writeSource(useLeft, false);
readDerivedOutsideComputation(selected); // 20. The computation ran and now depends on useLeft and right.
writeSource(left, 11);
readDerivedOutsideComputation(selected); // 20. The computation did not run again.
releaseRetain(selection);
Nodes with parameters
A computation receives only callingNode and previous. For a value that depends on a parameter, create one derived node per parameter value, and look each node up however suits your app. For example, with a map:
const multipleNodes = new Map<number, DerivedNode<number>>();
function multipleOfNumber(factor: number): DerivedNode<number> {
let node = multipleNodes.get(factor);
if (node === undefined) {
node = createDerivedNode(
(callingNode) => readSource(callingNode, number) * factor,
);
multipleNodes.set(factor, node);
}
return node;
}
Retaining nodes
A retain keeps a derived result cached while a caller needs it. A component typically retains on mount and releases on unmount. Node lifetimes and SSR covers request-scoped state and cleanup.
Reacting to changes
The flush runs in two phases. First, pico-js refreshes the affected nodes that have callbacks. Then it calls onChange once for each retain whose node changed value. Each callback can read the final value of any derived node. Outside a batch, the flush finishes before writeSource returns. Inside a batch, endWriteBatch runs the flush. Releasing a node’s last callback retain makes the node lazy again.
Grouping updates
A batch publishes all of its writes together. A second write to the same source replaces the pending value. Change callbacks run before endWriteBatch() returns.
startWriteBatch();
try {
writeSource(number, 5);
writeSource(number, 6);
} finally {
endWriteBatch();
}
readDerivedOutsideComputation(doubled); // 12
Contracts
Open one synchronous write batch at a time. Inside it, write sources and release retains. Read nodes and create retains after endWriteBatch() returns. The contracts cover computations, callbacks, equality, and cleanup.
Guarantees
Under the correctness contracts, every tracked computation, top-level read, and change callback receives current values. A cached node reruns when it needs a fresh result and one of its direct sources received a write or one of its derived inputs changed value. Writing the same value to a source still counts as a write. A change callback runs at most once per update or batch, after all affected nodes with callbacks are up to date.
The design explains invalidation and recomputation. The performance notes describe the cost of those operations, and the correctness proof establishes when reads return current values.