Pico API reference
These are the named exports of @isograph/pico. The getting started examples show a cached computation and a subscription. Pico assumes that values stay immutable and that computations are synchronous, pure and acyclic. A computation or change callback must handle any exception before it reaches Pico. The contracts describe the consequences of breaking these assumptions.
Creating nodes
function createSourceNode<T>(value: T): SourceNode<T>;
function createDerivedNode<T>(
computation: (callingNode: CallingNode, previous: T | undefined) => T,
): DerivedNode<T>;
createSourceNode(value)stores the initial value. ASourceNode<T>exposes one public property,readonly value: T. Outside a computation,source.valuereads the committed value without tracking a dependency. UsewriteSourceto change it.createDerivedNode(computation)returns an opaqueDerivedNode<T>. Creating the node does not run the computation. A read or retain evaluates it when a value is needed. On each run, the computation receives the previous cached value, orundefinedon the first run or after disposal. A computation can also returnundefinedas an ordinary value. Returning the previous object preserves its identity. Pico considers two results equal when===returns true.CallingNodeis the opaque argument that identifies the computation currently running. Pass the argument to each tracked read made by that computation. A helper called synchronously by the computation can accept the same argument. Its lifetime ends when the computation returns.
Reading
function readSource<T>(callingNode: CallingNode, source: SourceNode<T>): T;
function readDerived<T>(callingNode: CallingNode, node: DerivedNode<T>): T;
function readDerivedOutsideComputation<T>(node: DerivedNode<T>): T;
readSource(callingNode, source)returns the source value and records that the current computation read it.readDerived(callingNode, node)brings the input node up to date, returns its value and records that the current computation read it. The inputs read on this run replace the dependencies from the preceding run.readDerivedOutsideComputation(node)brings the node up to date and returns its value. Call it in application code or a change callback. An unretained node with no dependents becomes eligible for cleanup after the read. Retain the node if the cached value must survive between reads.
Computations can call only the two tracked read functions, with the callingNode they received. Read nodes outside write batches.
Writing and batching
function writeSource<T>(source: SourceNode<T>, value: T): void;
function startWriteBatch(): void;
function endWriteBatch(): void;
writeSource(source, value)publishes immediately outside a batch. The call brings affected nodes with callbacks up to date and delivers changes before returning. Writes inside a batch stage the last value supplied for each source. Pico does no equality check at the source. Skip the write yourself when the application considers the old and new values equivalent.startWriteBatch()opens a batch shared by this module instance. Starting a second batch throws and leaves the first batch open. Inside the batch, you can write sources and release retains.endWriteBatch()publishes the final values together, then runs change callbacks before returning. Pair it withstartWriteBatch()in the same synchronous turn, usingfinallyif the body can throw. Closing the batch commits preceding writes even when the body threw. Calling it with no batch open has no effect.
Retaining and subscribing
function retainDerivedNode<T>(
node: DerivedNode<T>,
onChange?: (value: T) => void,
): Retain<T>;
function releaseRetain<T>(retain: Retain<T>): void;
retainDerivedNode(node)brings the node up to date and keeps its cache alive. Subsequent writes can leave it waiting for its next read. SupplyingonChangealso causes Pico to refresh the node on relevant writes. The callback receives each changed result after all affected callback nodes have current values. Registration does not call the callback with the initial result. Register outside computations, callbacks and batches.Retain<T>is an opaque handle for one registration. A node can have several retains, each with its own lifetime.releaseRetain(retain)ends that registration. Release each handle exactly once, outside computations and callbacks. A release inside a batch takes effect before the batch delivers callbacks. A derived node becomes eligible for disposal when it has neither retains nor dependents.
A change callback may read current derived values with readDerivedOutsideComputation. If the application needs to write or change registrations in response, it can schedule the work after the callback returns.
Disposal and scheduling
Disposal drops a derived node’s cached value and detaches its dependencies. The same node handle can be read or retained again. Call the following disposal functions outside computations, change callbacks and write batches. Most applications use the default scheduler.
const scheduler: { schedule: () => void };
function disposeNext(): boolean;
function disposeAll(): void;
function disposeOlderThanUpdates<D extends { timeRemaining(): number }>(
minAge: number,
budget?: D | number,
): number;
function takeUnneeded(): ReadonlyArray<DerivedNode<unknown>>;
function disposeIfUnneeded(node: DerivedNode<unknown>): boolean;
scheduler.scheduleis writable. Pico calls it when cleanup needs scheduling. The default schedules an idle drain withminAge = 2, using a timer with a 5 ms work budget when the host lacksrequestIdleCallback. A custom scheduler can calldisposeAll(), calldisposeNext()until it returns false, perform an age-based drain, or take responsibility for the queued nodes withtakeUnneeded().disposeNext()removes one queue entry. It disposes the node if the node still has a cache and is unneeded. The return value reports whether more entries remain. Disposing the last queued node can therefore return false.disposeAll()drains the queue, including dependencies that become unneeded during the drain. Nodes with retains or dependents keep their cached values.disposeOlderThanUpdates(minAge, budget?)returns the number of entries still queued. Supply an integer age. A nonpositive age makes entries immediately eligible. A positive age starts at the first drain that sees the newest entry for a node. A batch commit advances the age once. An individual write advances it when the source has dependents to mark. Repeated drains alone do not advance it. A numeric budget is an absoluteperformance.now()deadline. An object budget suppliestimeRemaining()in milliseconds. Even with an exhausted budget, the drain can perform its first eligible disposal.takeUnneeded()empties the queue and returns the still-unneeded computed nodes, oldest entry first. Multiple entries can return the same node. The nodes keep their cached values. The caller takes responsibility for deciding when to try disposal.disposeIfUnneeded(node)checks the node again. It returns true if it disposed a cached node with no retains or dependents. It returns false if the node is needed or already lacks a cached value. Dependencies that become unneeded enter the cleanup queue.