Node lifetimes and SSR
Retaining and releasing nodes
A retain keeps a node and its dependencies cached. retainDerivedNode brings the node up to date before returning the retain. A React component can retain the values it displays on mount and release them on unmount. When a node has no remaining retains or dependents, pico-js can reclaim its cached result.
Keep the retain handle for as long as the subscriber needs the node. Release that same handle when the subscriber goes away. Multiple components can retain the same node independently. Releasing one component’s retain leaves the other components subscribed.
Disposal and garbage collection
Pico disposal drops a derived node’s cached value and removes its dependency edges. A caller that still holds the node can read it again. The next read computes a fresh value. JavaScript’s garbage collector reclaims the object itself once nothing references it. If an application keeps nodes in a lookup map, it must also drop those entries or the map when it no longer needs them.
The default cleanup scheduler gives recently abandoned results time to be reused, for example when two routes display the same record. The disposal guide describes the queue, age threshold, and scheduler API.
Server requests
A source holds its own value, and a derived node holds its own cached result. For request-specific data, create the nodes and any lookup maps per request. Reusing a module-level node for the current user would let requests overwrite one another’s data.
Requests that load the same copy of pico-js share its open batch and cleanup scheduler. Close each batch before yielding to another request. Otherwise a write from that request can join the pending writes, and an attempt to open another batch throws. Configure the scheduler once for the server.
For example, a request handler can create a source for that request’s viewer and pass its derived nodes to the renderer. After rendering, the handler releases any retains it created and drops references to the request’s store. The next request creates its own sources and derived nodes.