Skip to main content

Contracts

Write batches​

Open one batch at a time. Starting a second batch throws and leaves the first batch open. Inside the batch, callers may write sources and release retains. Read sources and derived nodes, create retains, and run cleanup outside the batch.

Close the batch synchronously in the same turn. Use finally if the body can throw. endWriteBatch() commits the last write to each source, refreshes affected nodes, and delivers callbacks before returning. Writes made before an exception are committed when the finally block closes the batch.

Contracts for correctness​

These rules define the supported API. The failure descriptions below refer to the current implementation. A future release may behave differently. Where no outcome is specified, callers cannot rely on the result.

  • Computations must be synchronous and pure, and read other nodes only through readSource and readDerived, called with the callingNode the computation received. A write to a tracked source or a change in a tracked derived value can trigger a rerun. Changing untracked state alone leaves the cached result in place. A read with a callingNode that a computation kept after it returned, or that belongs to another node, records the dependency on that callingNode's node. The computation that made the read keeps its cached value when the read node changes, and the other node recomputes the next time it is read. If that other node's computation is running, the behavior is unspecified. An asynchronous computation's value is the promise it returns. Its reads after an await are reads with a callingNode kept after the computation returned, so they record dependencies of its own node, and a change to what they read makes the node recompute the next time it is read.

  • A computation must not call anything else in pico-js. readDerivedOutsideComputation inside a computation returns the node's value without recording a dependency, as a read of state outside pico-js does. If it took the cleanup queue from empty to non-empty, it calls schedule while the computation runs, except during a flush, where the call into pico-js that ran the flush calls schedule before it returns. If a schedule called while the computation runs disposes nodes, the behavior is unspecified. The behavior of a write, a batch, a retain, a release, or a disposal inside a computation is unspecified.

  • Computations and change callbacks must not throw. If your code can throw, wrap it in try/catch inside the computation or callback, so nothing throws into pico-js. After a throw reaches pico-js, its state is unspecified. Puchi provides constructors that catch exceptions inside the computation and return them as values.

  • The graph must not have cycles. A computation that reads its own node, or a node that depends on its node, recurses until the call stack overflows and throws a RangeError. After that, pico-js's state is unspecified.

  • A change callback may call readDerivedOutsideComputation and must not do anything else with pico-js. A write inside a callback to a source that no node with a callback depends on, directly or through other nodes, does what that write does outside a callback: it marks the nodes that depend on the source and runs no computation, and the flush goes on calling callbacks. A write to a source that a node with a callback depends on starts a nested flush, which brings the nodes with callbacks that the write reached up to date and then throws a TypeError before it calls any callback, whether or not a value changed. After that throw, pico-js's state is unspecified. A batch inside a callback does the same at its endWriteBatch, for the sources written in it. The behavior of a retain or a release inside a callback is unspecified. Compute a value that can be computed from the graph in a derived node, not in a source that a callback writes. To reset a value when another changes, keep the raw value in the source and compute the effective value in a derived node:

    const query = createSourceNode('a');
    const requested = createSourceNode({ page: 3, query: 'a' });
    const page = createDerivedNode((callingNode) => {
    const { page, query: requestedFor } = readSource(callingNode, requested);
    return requestedFor === readSource(callingNode, query) ? page : 1;
    });

    Starting work outside the graph from a callback, for example a fetch, is fine, and the writeSource from its response runs outside the flush. A write that must follow a change can be deferred with queueMicrotask(() => writeSource(...)), which runs after the flush returns.

  • Release each retain exactly once. The behavior of a second release of the same retain is unspecified. A retain that is never released keeps its node and everything the node depends on cached for good.

  • A write batch must not span an await. Requests using the same module share its batch state. Another request’s writes can join an open batch and its callbacks can be delayed. See server requests.

  • pico-js compares values with === and counts ===-equal values as identical. A recompute whose result is === to the previous value keeps the previous value, the nodes that depend on the node do not recompute on its account, and no callback runs. A computation whose result depends on the sign of zero breaks this contract. A node that reads x and returns 1 / x keeps Infinity when x recomputes from 0 to -0. NaN is never === to itself, so a node whose value is NaN counts as changed on every recompute: the nodes that depend on it recompute, and its callbacks run, after every update that recomputes it.

  • Do not mutate values after passing them to pico-js or reading them from it. A value changed in place is still === to itself, so nodes that depend on it can keep results computed from its old contents.

  • Limit dependency chains to 1,000 nodes or fewer. A shorter chain can still overflow the JavaScript call stack. A first read can nest one computation inside the next. The safe depth depends on the runtime and on what each computation calls. The recursive resolver benchmark overflows at depth 1,000. If a read throws a stack overflow, the engine state is unspecified. Stack depth measurements

Contracts for performance​

These practices reduce unnecessary work. They do not affect correctness.

  • Only update a source when its value changed, for example by checking deepEqual(current, next) before calling writeSource. writeSource does not compare values, so writing the same value marks its direct readers dirty. The next refresh of each reader reruns its computation, usually producing the same result. A second write before any computation reads the source again marks nothing.
  • When a computation's result is structurally equal to its previous value, return the previous value (previous), so that === sees no change. A new but equal value counts as a change, so the nodes that depend on the node recompute, and its callbacks run, although nothing changed.