Documentation / Internals

Graph

How Refract tracks dependencies and turns a state change into a host update.

State, derived values, and observers

refract-graph is a single-threaded runtime for typed attributes. State<T> stores a writable value; a Binding<T> exposes it and can project a field. Derived<T> caches a computation. An Observer tracks reads made outside the graph, such as evaluating a mounted view body. Projected bindings still observe their source attribute.

Captures deduplicate actual reads in encounter order, using a small list and adding a membership index for wider sets. Completed captures transfer their read lists without cloning. Derived computations publish dependencies immediately; view reads are staged until commit, so discarded preparations cannot change accepted subscriptions. An identical ordered read list reuses existing edges without revalidation; changed lists are validated before replacement.

What happens when a value changes

Imagine a quantity stepper whose label reads a derived total. A button press reaches app.dispatch, which runs the mounted action in a graph batch. The action updates quantity through its binding. Propagation queues dependent work, deduplicates queued entries, and lets related writes settle together.

Propagation reevaluates derived computations and marks affected observers dirty. Derived::distinct stops downstream propagation when its output compares equal: an in-stock label can stay clean when quantity rises from two to three. Even an equal result updates its dependencies when conditional reads change. Dirty observers tell core which mounted bodies need evaluation.

From dirty observer to host patch

On the next flush, app.prepare() maps dirty observers to mounted views. Dirty paths join at already-marked ancestors, avoiding repeated walks to the root. Traversal skips unaffected siblings and does not reevaluate clean ancestor bodies. Equal memo(props) inputs and a matching environment scope also skip clean subtrees; dirty descendants still receive updates.

Reconciliation matches unchanged child prefixes directly, using a lookup table for the remaining suffix. Built-in primitives supply identity metadata without cloning descriptions. The adapter applies any resulting patches, then app.commit(pending) accepts the prepared tree and subscriptions. Reevaluation can produce identical output and no host patch; memoization does not undo construction already performed by a parent.

Ownership and retirement

State slots belong to mounted identities, including keyed children that move. Preparation shares accepted slot maps, avoiding copies along ancestor paths, and reuses them after validating stable declarations. Cloned graph handles share ownership. Dropping the last handle queues retirement; Runtime::collect removes retired attributes and their edges at a safe point.

Read crates/graph/src/runtime.rs for dependency capture and propagation, and crates/core/src/runtime/mod.rs for the bridge from observers to mounted views and patches.