reflex-quant 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts", "../src/graph.ts", "../src/kernel.ts", "../src/host.ts"],
4
- "sourcesContent": ["// Public API: signal, computed, effect, get, peek, set, update, dispose,\n// batch, untracked. A handle is the node itself: nothing is allocated besides\n// the node, and handle <-> node conversions are type assertions only (no\n// runtime wrapper). Every operation here exposes existing kernel and host\n// semantics; none adds state.\nimport { Flag } from \"./graph\";\nimport { depth, drainNow, draining, size, start, stop, watcher } from \"./host\";\nimport {\n dispose as detach,\n node,\n peek as refresh,\n read,\n untracked,\n write,\n} from \"./kernel\";\nimport type { Fn, Node } from \"./graph\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Public handles: compile-time brands over the node, which is the runtime\n// handle. Variance is explicit: reading is covariant, a signal is also\n// written, so it is invariant.\ndeclare const READ: unique symbol;\ndeclare const WRITE: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\n/** Anything `get` accepts. */\nexport interface Readable<out T> {\n readonly [READ]: T;\n}\n\nexport interface Signal<in out T> extends Readable<T> {\n readonly [WRITE]: (value: T) => void;\n}\n\nexport interface Computed<out T> extends Readable<T> {\n readonly [COMPUTED]: true;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\n/**\n * `fn` receives `prev`, the last successfully committed value: `initial`\n * (or undefined) before the first commit, unchanged by a failed run.\n * Returning `prev` itself is a cutoff: consumers do not rerun, so custom\n * equality is `(prev) => (equal(prev, next) ? prev : next)`.\n */\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T>;\nexport function computed<T>(fn: (prev: T) => T, initial: T): Computed<T>;\nexport function computed<T>(\n fn: (prev: T | undefined) => T,\n initial?: T,\n): Computed<T> {\n return node(fn as Fn, initial, Flag.Dirty) as unknown as Computed<T>;\n}\n\n/** Runs now (or when the enclosing batch or drain ends), then on every change it read. */\nexport function effect(fn: () => void | (() => void)): Effect {\n const effectNode = watcher(fn, true);\n\n try {\n start(effectNode);\n } catch (error) {\n stop(effectNode);\n throw error;\n }\n\n return effectNode as unknown as Effect;\n}\n\n/**\n * Kernel `read` itself under the public type: a signal's flags are 0, so one\n * read path serves signals and computeds, and no surface function stands in\n * front of it.\n */\nexport const get: <T>(source: Readable<T>) => T = read as never;\n\n/** `get` without observation: up to date, but the running consumer does not depend on it. */\nexport const peek: <T>(source: Readable<T>) => T = refresh as never;\n\n/** Writes, then runs what the write scheduled; a write that woke nothing stops at one load. */\nexport function set<T>(source: Signal<T>, value: T): void {\n write(source as unknown as Node, value);\n\n if (size !== 0 && !draining && depth === 0) drainNow();\n}\n\n/**\n * `set(source, fn(current))`. An update is a write, not an observation:\n * `fn` runs untracked, so neither `source` nor what `fn` reads becomes a\n * dependency of the running consumer. If `fn` throws, nothing is written.\n */\nexport function update<T>(source: Signal<T>, fn: (value: T) => T): void {\n set(\n source,\n untracked(() => fn((source as unknown as Node).value as T)),\n );\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const targetNode = target as unknown as Node;\n\n if ((targetNode.flags & Flag.Watch) !== 0) stop(targetNode);\n else detach(targetNode);\n}\n", "import { stats } from \"./instrumentation\";\n\n// Compile-time vocabulary of the runtime graph. Nothing here exists at runtime.\n\n// The aliases name different facts but do not brand numbers: counters wrap at\n// MASK and edge versions can carry UNREAD.\nexport type Flags = number;\nexport type Version = number;\nexport type Generation = number;\n\n/** A computed's or watcher's body; a signal has none. */\nexport type Fn = (prev: unknown) => unknown;\n/** A watcher's cleanup, run untracked. */\nexport type Cleanup = () => unknown;\n\nexport interface Node {\n flags: Flags;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: Version;\n /** null for a signal and for a disposed node. */\n fn: Fn | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\n /** Subscribers, in birth order. */\n subs: Edge | null;\n lastSub: Edge | null;\n /** While a reader indexes: this node's edge into that reader. */\n slot: Edge | null;\n /** Successful runs; edges born since the last one are tentative. */\n gen: Generation;\n}\n\nexport interface Edge {\n readonly from: Node;\n readonly to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: Version | typeof UNREAD;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: Generation;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\n/** Shared LIFO of the kernel's walks: edges, and (source, cell) pairs. */\nexport type WorkStack = (Node | Edge | null)[];\n\n// Flag layout and constants used by the kernel and host.\nexport const enum Flag {\n /** A source below may have changed; a watcher must validate. */\n Stale = 1 << 0,\n /** Must run: first run, failed run, or a direct source changed. */\n Dirty = 1 << 1,\n /** Its run is on the stack. */\n Busy = 1 << 2,\n /** Its run put its edges into the sources' slots. */\n Indexed = 1 << 3,\n /** A watcher: handed to the host, never read. */\n Watch = 1 << 4,\n /** Woken by signals only; computed sources need no validation. */\n Woken = 1 << 5,\n /**\n * Development builds only (C1 is a precondition): a computed whose\n * validation is on the stack, no longer STALE and not yet clean. WOKEN's\n * bit, so every mask stays a byte: WOKEN is a watcher's, CHECKING a\n * computed's, and no node is both. A production build never sets it, so\n * a mask that tests it is inert there.\n */\n Checking = 1 << 5,\n /** Host policy: scheduled by the reference host. */\n Auto = 1 << 6,\n /** Host policy: already in its queue. */\n Queued = 1 << 7,\n /** Its run marked its unread edges UNREAD; pairs are found through subscribers. */\n Marked = 1 << 8,\n}\n\n/** edge.ver of an edge the marked or indexed run has not read yet. */\nexport const UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nexport const MASK = 0x3fffffff;\n/**\n * A committed edge keeps its birth generation, and generations wrap: an edge\n * 2^30 - 1 commits old would read as born in the next run (tentative). So a\n * node restamps its edges with its own generation whenever a commit makes\n * `gen & SETTLE` zero, twice a cycle, and no edge falls that far behind.\n */\nexport const SETTLE = 0x1fffffff;\n/** Scan budget of every bounded search: a constant keeps a run linear. */\nexport const SCAN = 8;\n/**\n * Work-stack slots kept at rest, and the length past which the outermost\n * walk's end cuts the stack back to them. The gap keeps walks only a little\n * deeper than KEEP from cutting and regrowing on every pass.\n */\nexport const KEEP = 256;\nexport const TRIM = 512;\n\n// Intrusive graph operations independent of the execution context.\n\nexport function node(fn: Node[\"fn\"], value: unknown, flags: Flags): Node {\n return {\n flags,\n value,\n ver: 0,\n fn,\n deps: null,\n last: null,\n subs: null,\n lastSub: null,\n slot: null,\n gen: 0,\n };\n}\n\n/** Move an unread edge (strictly after `next`) up to the cursor. */\nexport function move(c: Node, e: Edge, last: Edge | null, next: Edge): void {\n const before = e.prev!;\n const after = e.next;\n before.next = after;\n\n if (after !== null) after.prev = before;\n\n e.prev = last;\n e.next = next;\n\n if (last !== null) last.next = e;\n else c.deps = e;\n next.prev = e;\n c.last = e;\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nexport function cut(c: Node, last: Edge | null): void {\n let e = last === null ? c.deps : last.next;\n\n if (e === null) return;\n\n if (last === null) c.deps = null;\n else last.next = null;\n // The cut head's back pointer would tie this garbage to the live list, and\n // so to garbage cut later: old garbage keeps newer garbage alive.\n e.prev = null;\n\n for (; e !== null; e = e.next) unsub(e);\n}\n\n/** Restamp every edge of `c`, all of them committed, with its generation (SETTLE). */\nexport function settle(c: Node): void {\n const gen = c.gen;\n\n for (let e = c.deps; e !== null; e = e.next) e.gen = gen;\n}\n\n/** Take an edge out of its source's subscriber list. */\nexport function unsub(e: Edge): void {\n if (__STATS__) stats.unlinks++;\n\n const s = e.from;\n const before = e.prevSub;\n const after = e.nextSub;\n\n if (before !== null) before.nextSub = after;\n else s.subs = after;\n\n if (after !== null) after.prevSub = before;\n else s.lastSub = before;\n // Subscribers are in birth order, so an old removed edge would point at a\n // much younger one and keep it (and its watcher) alive across scavenges.\n e.prevSub = null;\n e.nextSub = null;\n}\n", "// Reactive kernel: tracking, propagation, validation and execution.\n// Detached edges must not retain pointers into live lists.\n// See research/LOG.md for representation rationale.\n\nimport {\n Flag,\n KEEP,\n MASK,\n SCAN,\n SETTLE,\n TRIM,\n UNREAD,\n cut,\n move,\n settle,\n unsub,\n} from \"./graph\";\nimport type { Edge, Flags, Node, WorkStack } from \"./graph\";\nimport { stats } from \"./instrumentation\";\n\nexport { node } from \"./graph\";\nexport { stats } from \"./instrumentation\";\n\n/** The running consumer, or null outside a tracking pass. */\nlet active: Node | null = null;\n/**\n * One LIFO work stack shared by all walks: sibling edges to resume (mark),\n * edges to climb back through (pull), and (source, cell) pairs an indexed run\n * displaced. Every walk pops back to its own base before returning, and the\n * outermost one gives back what a deep walk grew (`trim`).\n */\nconst stack: WorkStack = new Array(256).fill(null);\n/** First free index of `stack`. */\nlet top = 0;\n/** Longest the work stack has been when `trim` cut it (research). */\nlet peak = 0;\nlet wake: (w: Node) => void = () => {};\n\n/**\n * The host is told about every watcher a write or mark reaches. It owns the\n * deduplication (a queued watcher is not queued again), so a watcher left\n * STALE by a failed check is delivered again by the next push.\n */\nexport function onWake(fn: (w: Node) => void): void {\n wake = fn;\n}\n\n/** Nothing running, the work stack empty (tests). */\nexport function idle(): boolean {\n return active === null && top === 0;\n}\n\n/**\n * Deepest work-stack top reached so far (research). Walks null the slots\n * they pop and only `trim` shrinks the array, so the high-water mark is its\n * length or the longest length `trim` cut.\n */\nexport function stackHighWater(): number {\n return stack.length > peak ? stack.length : peak;\n}\n\n/** Slots the work stack holds now (tests). */\nexport function stackLength(): number {\n return stack.length;\n}\n\n/**\n * At rest, cut a stack that a deep walk grew past TRIM back to KEEP slots,\n * so one deep walk does not hold its depth in memory for good. Setting the\n * length right-trims V8's backing store in place: no allocation, and the\n * array stays packed.\n *\n * Only the end of an outermost validation calls it (top 0), whichever walk\n * grew the stack. A deep mark walk runs through computeds that are STALE\n * afterwards, so their next demand is such a validation; `write` and `mark`\n * stay as they were. The guard costs one length load and one compare per\n * validation, nothing per node.\n */\nfunction trim(): void {\n if (__STATS__ && stack.length > peak) peak = stack.length;\n\n stack.length = KEEP;\n}\n\n// ------------------------------------------------------------------- write\n\nexport function write(s: Node, value: unknown): void {\n if (Object.is(s.value, value)) return;\n\n s.value = value;\n s.ver = (s.ver + 1) & MASK;\n\n for (let e = s.subs; e !== null; e = e.nextSub) {\n const n = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n\n if ((f & Flag.Busy) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n if ((f & Flag.Watch) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Dirty | Flag.Woken;\n\n wake(n);\n } else if ((f & Flag.Stale) !== 0) {\n n.flags = f | Flag.Dirty;\n } else {\n n.flags = f | Flag.Dirty | Flag.Stale;\n\n if (n.subs !== null) mark(n.subs);\n }\n }\n}\n\n/** Mark everything below the subscriber list `first` STALE, depth first. */\nfunction mark(first: Edge): void {\n const base = top;\n let e: Edge | null = first;\n\n for (;;) {\n while (e !== null) {\n const n: Node = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n // A watcher has no subscribers; the host deduplicates, so it is always told.\n if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Stale;\n wake(n);\n } else if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n if (n.subs !== null) {\n if (e.nextSub !== null) stack[top++] = e.nextSub;\n\n e = n.subs;\n continue;\n }\n }\n e = e.nextSub;\n }\n\n if (top === base) return;\n\n e = stack[--top] as Edge;\n stack[top] = null;\n }\n}\n\n// -------------------------------------------------------------------- read\n\n/** Bring `n` up to date if it is a stale computed, then track it. */\nexport function read(n: Node): unknown {\n const value = peek(n);\n const c = active;\n\n if (c !== null) {\n if (__STATS__) stats.reads++;\n\n const last = c.last;\n const next = last === null ? c.deps : last.next;\n\n if (next !== null && next.from === n) {\n if (__STATS__) stats.hits++;\n\n c.last = next;\n next.ver = n.ver;\n } else miss(c, n, last, next);\n }\n return value;\n}\n\n/** `read` without its tracking half: bring `n` up to date, record no relation. */\nexport function peek(n: Node): unknown {\n if ((n.flags & (Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking)) !== 0)\n pull(n);\n\n return n.value;\n}\n\n/**\n * Pull a computed up to date. Existential, in read order: sources are\n * checked first to last and the first changed one makes the node run. A\n * STALE source is descended into (the stack keeps the edge to climb back\n * through); a DIRTY one runs. A throw leaves every node on the walk DIRTY.\n * A node whose run is on the stack is a cycle. A development build also\n * diagnoses a node whose validation is on the stack (CHECKING): C1 is a\n * precondition, so its committed value never answers such a demand there.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n\n if ((f & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((f & Flag.Dirty) !== 0) return exec(root);\n\n verify(root, f);\n}\n\n/**\n * The walk of `pull` for a STALE node that is not DIRTY, BUSY or CHECKING;\n * kept apart so `pull` stays small. A node disposed during the walk is not\n * run (W3), nor left DIRTY.\n *\n * Development builds diagnose C1: a node on the walk is CHECKING until it is\n * clean or runs, and a source on the stack (BUSY or CHECKING) is a cycle\n * even when no push marked it again, since its version is not yet a fact.\n * CHECKING is set by the store that clears STALE and cleared by `exec` as it\n * sets BUSY; only a node found clean pays a store of its own. Production\n * builds compile all of it away.\n */\nfunction verify(root: Node, f: Flags): void {\n root.flags = __DEV__ ? (f & ~Flag.Stale) | Flag.Checking : f & ~Flag.Stale;\n const base = top;\n let n = root;\n let e = root.deps;\n\n try {\n for (;;) {\n settled: {\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n\n if (__STATS__) stats.checks++;\n\n if (\n (sf &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0\n ) {\n if ((sf & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((sf & Flag.Dirty) === 0) {\n s.flags = __DEV__\n ? (sf & ~Flag.Stale) | Flag.Checking\n : sf & ~Flag.Stale;\n stack[top++] = e;\n n = s;\n e = s.deps;\n continue;\n }\n exec(s);\n }\n\n if (s.ver !== e.ver) {\n if (n.fn !== null) exec(n);\n break settled;\n }\n e = e.next;\n }\n // Every source unchanged: clean, and no longer on the walk.\n if (__DEV__) n.flags &= ~Flag.Checking;\n }\n\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) {\n if (stack.length > TRIM && base === 0) trim();\n\n return;\n }\n\n const up = stack[--top] as Edge;\n stack[top] = null;\n n = up.to;\n\n if (up.from.ver === up.ver) {\n e = up.next;\n break;\n }\n\n if (n.fn !== null) exec(n);\n }\n }\n } catch (error) {\n if (n.fn !== null)\n n.flags = __DEV__\n ? (n.flags | Flag.Dirty) & ~Flag.Checking\n : n.flags | Flag.Dirty;\n\n while (top > base) {\n const p = (stack[--top] as Edge).to;\n\n if (p.fn !== null)\n p.flags = __DEV__\n ? (p.flags | Flag.Dirty) & ~Flag.Checking\n : p.flags | Flag.Dirty;\n stack[top] = null;\n }\n throw error;\n }\n}\n\n/**\n * Universal check of a watcher: pull every committed source, so a throwing\n * one surfaces before any irreversible step. Marks DIRTY on change. Tentative\n * edges (reads of a failed run) only wake; they exist only while a run is\n * owed, and then versions need no comparison.\n */\nexport function check(w: Node): void {\n const tentative = (w.gen + 1) & MASK;\n let e = w.deps;\n\n // Until a run is owed, versions decide; then only the pulls remain. A\n // development build pulls a source on the stack too, so the C1 diagnosis\n // fails here, before the cleanup (P4).\n if ((w.flags & Flag.Dirty) === 0) {\n for (; e !== null; e = e.next) {\n const s = e.from;\n\n if (__STATS__) stats.checks++;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n\n if (s.ver !== e.ver) {\n w.flags |= Flag.Dirty;\n e = e.next;\n break;\n }\n }\n }\n\n for (; e !== null; e = e.next) {\n if (__STATS__) stats.checks++;\n\n const s = e.from;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n }\n}\n\nexport function untracked<T>(fn: () => T): T {\n const prev = active;\n active = null;\n\n try {\n return fn();\n } finally {\n active = prev;\n }\n}\n\n// --------------------------------------------------------------------- run\n\n/** Run `n.fn` as a tracking pass; commit value and edges on success. */\nexport function exec(n: Node): void {\n const prev = active;\n const base = top;\n\n if (__STATS__) stats.runs++;\n // Cleared before the run: a push during it marks the node again. In a\n // development build a computed's validation ends here, BUSY taking over\n // from CHECKING; on a watcher the bit is WOKEN, which this run discharges.\n n.flags =\n (n.flags &\n ~(__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Checking\n : Flag.Stale | Flag.Dirty)) |\n Flag.Busy;\n n.last = null;\n active = n;\n let value: unknown;\n\n try {\n value = n.fn!(n.value);\n } catch (error) {\n active = prev;\n fail(n, base);\n throw error;\n }\n active = prev;\n const f = n.flags;\n\n if ((f & Flag.Indexed) !== 0) unindex(n, base);\n\n n.flags = f & ~(Flag.Busy | Flag.Indexed | Flag.Marked);\n\n if (n.fn === null) return detach(n);\n // Commit only after a successful run. Dropping the unread suffix removes\n // sources that this pass no longer depends on.\n const last = n.last as Edge | null;\n\n if ((last === null ? n.deps : last.next) !== null) cut(n, last);\n\n n.gen = (n.gen + 1) & MASK;\n\n // Every edge is committed now; see SETTLE.\n if ((n.gen & SETTLE) === 0) settle(n);\n\n if (Object.is(value, n.value) === false) {\n n.value = value;\n n.ver = (n.ver + 1) & MASK;\n }\n}\n\n/**\n * A failed run: keep committed edges and this run's reads as the wake set,\n * drop unread edges earlier failed runs left behind, owe a run.\n */\nfunction fail(n: Node, base: number): void {\n if ((n.flags & Flag.Indexed) !== 0) unindex(n, base);\n\n n.flags &= ~(Flag.Busy | Flag.Indexed | Flag.Marked);\n\n if (n.fn === null) return detach(n);\n\n n.flags |= Flag.Dirty;\n const tentative = (n.gen + 1) & MASK;\n const last = n.last;\n\n for (let e = last === null ? n.deps : last.next; e !== null;) {\n const after: Edge | null = e.next;\n\n if (e.gen === tentative) {\n const before = e.prev;\n\n if (before !== null) before.next = after;\n else n.deps = after;\n\n if (after !== null) after.prev = before;\n\n unsub(e);\n e.prev = null;\n e.next = null;\n }\n e = after;\n }\n}\n\n/** Detach `n` from its sources for good; inside its own run, at its end. */\nexport function dispose(n: Node): void {\n if (n.fn === null) return;\n\n n.fn = null;\n\n if ((n.flags & Flag.Busy) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(Flag.Stale | Flag.Dirty | Flag.Woken);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nexport function index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n\n c.flags |= Flag.Indexed;\n let unread = false;\n\n for (let e = c.deps; e !== null; e = e.next) {\n if (__STATS__) stats.indexed++;\n\n if (e === next) unread = true;\n\n if (unread) e.ver = UNREAD;\n\n claim(e.from, e);\n }\n}\n\n/** Point the source's slot at `e`, keeping a displaced cell (an enclosing run's) to give back. */\nexport function claim(s: Node, e: Edge): void {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n}\n\n/** Clear the run's slots, give back the cells it displaced. */\nfunction unindex(n: Node, base: number): void {\n for (let e = n.deps; e !== null; e = e.next) e.from.slot = null;\n\n while (top > base) {\n const cell = stack[--top] as Edge;\n stack[top] = null;\n const s = stack[--top] as Node;\n stack[top] = null;\n s.slot = cell;\n }\n}\n\n/** A new edge at the cursor, appended to the source's subscribers. */\nexport function link(\n c: Node,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (__STATS__) stats.links++;\n\n const tail = s.lastSub;\n const e: Edge = {\n from: s,\n to: c,\n ver: s.ver,\n gen: (c.gen + 1) & MASK,\n prev: last,\n next,\n prevSub: tail,\n nextSub: null,\n };\n\n if (last !== null) last.next = e;\n else c.deps = e;\n\n if (next !== null) next.prev = e;\n\n if (tail !== null) tail.nextSub = e;\n else s.subs = e;\n s.lastSub = e;\n c.last = e;\n}\n\n// Exact dependency tracking: resolve a read outside the stable cursor path.\n// Proofs come cheapest first: the cursor (in `read`), the previous read, a\n// source nobody reads, bounded scans of this reader's list, then the source's\n// first subscribers. Only a source with more subscribers than the budget needs\n// the slot index. Slot walks and edge creation remain in the kernel because\n// they mutate its shared stack.\n\n/**\n * A read that is not the next edge in read order. Every answer is exact. A\n * repeated read keeps the version of the pass's first observation (P5): a\n * change between the two reads stays visible to the next validation.\n */\nfunction miss(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (last !== null && last.from === s) {\n if (__STATS__) stats.repeats++;\n\n return;\n }\n\n const f = c.flags;\n\n if ((f & Flag.Indexed) !== 0) return slot(c, s, last, next);\n /** The pair's edge through the source's subscribers; undefined: unknown. */\n let found: Edge | null | undefined;\n\n if ((f & Flag.Marked) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next);\n // An edge found by a scan is the pair's only one; absence needs one side\n // scanned completely (this reader's list, or the source's subscribers).\n let e = next !== null ? next.next : null;\n let budget = SCAN;\n\n while (e !== null && e.from !== s && --budget !== 0) e = e.next;\n\n if (e !== null && e.from === s) {\n if (__STATS__) stats.scans++;\n\n e.ver = s.ver;\n return move(c, e, last, next!);\n }\n let p = last !== null ? last.prev : null;\n budget = SCAN;\n\n while (p !== null && p.from !== s && --budget !== 0) p = p.prev;\n\n if (p !== null && p.from === s) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n\n // Absent if this reader's whole list was scanned; otherwise the source's\n // first subscribers decide. Found as the first one: a lookup through the\n // subscribers costs no load beyond the read itself, so mark the unread\n // suffix and let the rest of the pass find its pairs that way, with no\n // slot to fill or clear. Found deeper: every lookup would walk other\n // readers' edges, so index; either way the found edge is taken at once.\n found = e === null && p === null ? null : pair(s, c);\n\n if (found !== null && found !== undefined) begin(c, s, found, next);\n } else found = pair(s, c);\n\n if (found === null) return link(c, s, last, next);\n\n if (found !== undefined) return take(c, found, s, last, next);\n // Past the budget: index the pass, and the slot decides from here on.\n index(c, next);\n slot(c, s, last, next);\n}\n\n/** An indexed pass's lookup: the source's slot holds the pair's edge, or the pair is absent. */\nfunction slot(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (__STATS__) stats.lookups++;\n\n const cell = s.slot;\n\n if (cell !== null && cell.to === c) return take(c, cell, s, last, next);\n link(c, s, last, next);\n claim(s, c.last as Edge);\n}\n\n/**\n * The pair's edge among the source's first SCAN subscribers: null if the list\n * ends first (absent), undefined if the budget runs out (unknown).\n */\nfunction pair(s: Node, c: Node): Edge | null | undefined {\n if (__STATS__) stats.lookups++;\n\n let budget = SCAN;\n\n for (let e = s.subs; e !== null; e = e.nextSub) {\n if (e.to === c) return e;\n\n if (--budget === 0) return undefined;\n }\n return null;\n}\n\n/**\n * The pass's lookup from here on, decided by the found edge's place: the\n * source's first subscriber marks the unread suffix, from `next` on (a walk\n * over edges, not sources); deeper, index.\n */\nfunction begin(c: Node, s: Node, e: Edge, next: Edge | null): void {\n if (e !== s.subs) return index(c, next);\n\n if (__STATS__) stats.sweeps++;\n\n c.flags |= Flag.Marked;\n\n for (let u = next; u !== null; u = u.next) {\n if (__STATS__) stats.indexed++;\n\n u.ver = UNREAD;\n }\n}\n\n/**\n * A found edge: an unread one is observed and moves to the cursor; a repeated\n * read keeps its place and its first observation's version (P5).\n */\nfunction take(\n c: Node,\n e: Edge,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (e.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n if (__STATS__) stats.moves++;\n\n e.ver = s.ver;\n move(c, e, last, next!);\n}\n", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport { Flag } from \"./graph\";\nimport type { Cleanup, Fn, Node } from \"./graph\";\nimport { check, dispose, exec, node, onWake, untracked } from \"./kernel\";\n\nconst queue: (Node | null)[] = [];\n// The host's registers. Exported so a write's caller can test for work\n// without a call into the host; ES bindings are read-only to importers, so\n// only this module writes them.\n/** Queued watchers. */\nexport let size = 0;\n/** Open batches. */\nexport let depth = 0;\n/** A drain loop is open. */\nexport let draining = false;\n\n/** Queue a scheduled watcher once; the kernel calls this when it wakes one. */\nexport function wake(w: Node): void {\n if ((w.flags & (Flag.Auto | Flag.Queued)) !== Flag.Auto) return;\n\n w.flags |= Flag.Queued;\n queue[size++] = w;\n}\n\nonWake(wake);\n\n/** A watcher runs `fn` and keeps a returned function as its cleanup. */\nexport function watcher(fn: Fn, auto: boolean): Node {\n return node(\n fn,\n undefined,\n Flag.Watch | Flag.Stale | Flag.Dirty | (auto ? Flag.Auto : 0),\n );\n}\n\n/**\n * Check every committed source unless only signals woke it, then, if a run\n * is owed, consume the previous cleanup and run. A watcher neither STALE,\n * WOKEN is clean, or cold after a failure until a source changes. A failed\n * check leaves it STALE, so the next run checks again (V1) and the next push\n * finds it queued or delivers it again.\n * Precondition: its own run is not on the stack (flush is not reentrant).\n */\nexport function run(w: Node): void {\n const flags = w.flags;\n\n if (w.fn === null || (flags & (Flag.Stale | Flag.Woken)) === 0) return;\n\n w.flags = flags & ~(Flag.Stale | Flag.Woken);\n\n if ((flags & Flag.Stale) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= Flag.Stale;\n throw error;\n }\n }\n\n if ((w.flags & Flag.Dirty) === 0 || w.fn === null) return;\n\n const cleanup = w.value;\n\n if (typeof cleanup === \"function\") {\n w.value = undefined;\n untracked(cleanup as Cleanup);\n\n if (w.fn === null) return;\n }\n exec(w);\n}\n\n/** Terminal: detach, then run the last cleanup outside tracking. */\nexport function stop(w: Node): void {\n if (w.fn === null) return;\n\n const cleanup = w.value;\n w.value = undefined;\n dispose(w);\n\n if (typeof cleanup === \"function\") untracked(cleanup as Cleanup);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (size === 0 || draining || depth !== 0) return;\n\n drainNow();\n}\n\n/**\n * `flush` with its guard at the call site: a write's caller tests\n * `size !== 0 && !draining && depth === 0` first (the queue first, so a write\n * that woke nothing costs one load), and calls this only when there is work.\n */\nexport function drainNow(): void {\n draining = true;\n drain(false, undefined);\n}\n\n/**\n * A new scheduled watcher's first run. With no drain open, no batch and an\n * empty queue this is `wake(w); flush()` with the queue slot skipped: the\n * drain opens on `w` (so its run stays inside the drain loop, LOG 11), and a\n * fresh watcher has no sources to check and no cleanup, so `run` is `exec`.\n */\nexport function start(w: Node): void {\n if (draining || depth !== 0 || size !== 0) {\n wake(w);\n return flush();\n }\n draining = true;\n\n try {\n exec(w);\n } catch (error) {\n return drain(true, error);\n }\n drain(false, undefined);\n}\n\n/** The drain loop, opened by the caller: FIFO, the first error rethrown at the end. */\nfunction drain(failed: boolean, first: unknown): void {\n for (let i = 0; i < size; i++) {\n const queuedWatcher = queue[i]!;\n queue[i] = null;\n queuedWatcher.flags &= ~Flag.Queued;\n\n try {\n run(queuedWatcher);\n } catch (error) {\n if (!failed) first = error;\n\n failed = true;\n }\n }\n size = 0;\n draining = false;\n\n if (failed) throw first;\n}\n\nexport function batch<T>(fn: () => T): T {\n depth++;\n\n try {\n return fn();\n } finally {\n if (--depth === 0) flush();\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAAA;AAAA,EAAA;AAAA;AAAA,cAAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACkFO,IAAM,SAAS,IAET,OAAO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eAAK,UAAU,GACT;AAAA,EACR;AAEA,SAAO;AACT;AAOO,IAAM,MAAqC,MAGrCC,QAAsC;AAG5C,SAAS,IAAO,QAAmB,OAAgB;AACxD,QAAM,QAA2B,KAAK,GAElC,SAAS,KAAK,CAAC,YAAY,UAAU,KAAG,SAAS;AACvD;AAOO,SAAS,OAAU,QAAmB,IAA2B;AACtE;AAAA,IACE;AAAA,IACA,UAAU,MAAM,GAAI,OAA2B,KAAU,CAAC;AAAA,EAC5D;AACF;AAEO,SAASC,SAAQ,QAA0C;AAChE,MAAM,aAAa;AAEnB,GAAK,WAAW,QAAQ,oBAAgB,IAAG,KAAK,UAAU,IACrD,QAAO,UAAU;AACxB;",
6
- "names": ["dispose", "peek", "wake", "peek", "dispose"]
4
+ "sourcesContent": ["// Public API: signal, computed, effect, get, peek, set, update, dispose,\n// batch, untracked. A handle is the node itself: nothing is allocated besides\n// the node, and handle <-> node conversions are type assertions only (no\n// runtime wrapper). Every operation here exposes existing kernel and host\n// semantics; none adds state.\nimport { Flag } from \"./graph\";\nimport { depth, drainNow, draining, size, start, stop, watcher } from \"./host\";\nimport {\n dispose as detach,\n node,\n peek as refresh,\n read,\n untracked,\n write,\n} from \"./kernel\";\nimport type { Fn, Node } from \"./graph\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Public handles: compile-time brands over the node, which is the runtime\n// handle. Variance is explicit: reading is covariant, a signal is also\n// written, so it is invariant.\ndeclare const READ: unique symbol;\ndeclare const WRITE: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\n/** Anything `get` accepts. */\nexport interface Readable<out T> {\n readonly [READ]: T;\n}\n\nexport interface Signal<in out T> extends Readable<T> {\n readonly [WRITE]: (value: T) => void;\n}\n\nexport interface Computed<out T> extends Readable<T> {\n readonly [COMPUTED]: true;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\n/**\n * `fn` receives `prev`, the last successfully committed value: `initial`\n * (or undefined) before the first commit, unchanged by a failed run.\n * Returning `prev` itself is a cutoff: consumers do not rerun, so custom\n * equality is `(prev) => (equal(prev, next) ? prev : next)`.\n */\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T>;\nexport function computed<T>(fn: (prev: T) => T, initial: T): Computed<T>;\nexport function computed<T>(\n fn: (prev: T | undefined) => T,\n initial?: T,\n): Computed<T> {\n return node(fn as Fn, initial, Flag.Dirty) as unknown as Computed<T>;\n}\n\n/** Runs now (or when the enclosing batch or drain ends), then on every change it read. */\nexport function effect(fn: () => void | (() => void)): Effect {\n const effectNode = watcher(fn, true);\n\n try {\n start(effectNode);\n } catch (error) {\n stop(effectNode);\n throw error;\n }\n\n return effectNode as unknown as Effect;\n}\n\n/**\n * Kernel `read` itself under the public type: a signal's flags are 0, so one\n * read path serves signals and computeds, and no surface function stands in\n * front of it.\n */\nexport const get: <T>(source: Readable<T>) => T = read as never;\n\n/** `get` without observation: up to date, but the running consumer does not depend on it. */\nexport const peek: <T>(source: Readable<T>) => T = refresh as never;\n\n/** Writes, then runs what the write scheduled; a write that woke nothing stops at one load. */\nexport function set<T>(source: Signal<T>, value: T): void {\n write(source as unknown as Node, value);\n\n if (size !== 0 && !draining && depth === 0) drainNow();\n}\n\n/**\n * `set(source, fn(current))`. An update is a write, not an observation:\n * `fn` runs untracked, so neither `source` nor what `fn` reads becomes a\n * dependency of the running consumer. If `fn` throws, nothing is written.\n */\nexport function update<T>(source: Signal<T>, fn: (value: T) => T): void {\n set(\n source,\n untracked(() => fn((source as unknown as Node).value as T)),\n );\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const targetNode = target as unknown as Node;\n\n if ((targetNode.flags & Flag.Watch) !== 0) stop(targetNode);\n else detach(targetNode);\n}\n", "import { stats } from \"./instrumentation\";\n\n// Compile-time vocabulary of the runtime graph. Nothing here exists at runtime.\n\n// The aliases name different facts but do not brand numbers: counters wrap at\n// MASK and edge versions can carry UNREAD.\nexport type Flags = number;\nexport type Version = number;\nexport type Generation = number;\n\n/** A computed's or watcher's body; a signal has none. */\nexport type Fn = (prev: unknown) => unknown;\n/** A watcher's cleanup, run untracked. */\nexport type Cleanup = () => unknown;\n\nexport interface Node {\n flags: Flags;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: Version;\n /** null for a signal and for a disposed node. */\n fn: Fn | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** Subscribers, in birth order. */\n subs: Edge | null;\n lastSub: Edge | null;\n /** While a reader indexes: this node's edge into that reader. */\n slot: Edge | null;\n /** Successful runs; edges born since the last one are tentative. */\n gen: Generation;\n}\n\nexport interface Edge {\n readonly from: Node;\n readonly to: Node;\n /** from.ver when `to` read it; UNREAD in the pass's marked unread suffix. */\n ver: Version | typeof UNREAD;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: Generation;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\n/** Shared LIFO of the kernel's walks: resume edges and displaced slot cells. */\nexport type WorkStack = (Edge | null)[];\n\n// Flag layout and constants used by the kernel and host.\nexport const enum Flag {\n /** A source below may have changed; a watcher must validate. */\n Stale = 1 << 0,\n /** Must run: first run, failed run, or a direct source changed. */\n Dirty = 1 << 1,\n /** Its run is on the stack. */\n Busy = 1 << 2,\n /** A watcher: handed to the host, never read. */\n Watch = 1 << 4,\n /** Woken by signals only; computed sources need no validation. */\n Woken = 1 << 5,\n /**\n * Development builds only (C1 is a precondition): a computed whose\n * validation is on the stack, no longer STALE and not yet clean. WOKEN's\n * bit, so every mask stays a byte: WOKEN is a watcher's, CHECKING a\n * computed's, and no node is both. A production build never sets it, so\n * a mask that tests it is inert there.\n */\n Checking = 1 << 5,\n /** Host policy: scheduled by the reference host. */\n Auto = 1 << 6,\n /** Host policy: already in its queue. */\n Queued = 1 << 7,\n}\n\n/** Counters wrap inside the SMI range. */\nexport const enum CounterLimit {\n Mask = 0x3fffffff,\n}\n\n/** edge.ver of an edge the marked or indexed run has not read yet. */\nexport const UNREAD = -1;\n/**\n * A committed edge keeps its birth generation, and generations wrap: an edge\n * 2^30 - 1 commits old would read as born in the next run (tentative). So a\n * node restamps its edges with its own generation whenever a commit makes\n * `gen & SETTLE` zero, twice a cycle, and no edge falls that far behind.\n */\nexport const SETTLE = 0x1fffffff;\n/** Scan budget of every bounded search: a constant keeps a run linear. */\nexport const SCAN = 8;\n/**\n * Work-stack slots kept at rest, and the length past which the outermost\n * walk's end cuts the stack back to them. The gap keeps walks only a little\n * deeper than KEEP from cutting and regrowing on every pass.\n */\nexport const KEEP = 256;\nexport const TRIM = 512;\n\n// Intrusive graph operations independent of the execution context.\n\nexport function node(fn: Node[\"fn\"], value: unknown, flags: Flags): Node {\n return {\n flags,\n value,\n ver: 0,\n fn,\n deps: null,\n subs: null,\n lastSub: null,\n slot: null,\n gen: 0,\n };\n}\n\n/** Move an unread edge (strictly after `next`) up to the cursor. */\nexport function move(c: Node, e: Edge, last: Edge | null, next: Edge): void {\n const before = e.prev!;\n const after = e.next;\n before.next = after;\n\n if (after !== null) after.prev = before;\n\n e.prev = last;\n e.next = next;\n\n if (last !== null) last.next = e;\n else c.deps = e;\n next.prev = e;\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nexport function cut(c: Node, last: Edge | null): void {\n let e = last === null ? c.deps : last.next;\n\n if (e === null) return;\n\n if (last === null) c.deps = null;\n else last.next = null;\n // The cut head's back pointer would tie this garbage to the live list, and\n // so to garbage cut later: old garbage keeps newer garbage alive.\n e.prev = null;\n\n for (; e !== null; e = e.next) unsub(e);\n}\n\n/** Restamp every edge of `c`, all of them committed, with its generation (SETTLE). */\nexport function settle(c: Node): void {\n const gen = c.gen;\n\n for (let e = c.deps; e !== null; e = e.next) e.gen = gen;\n}\n\n/** Take an edge out of its source's subscriber list. */\nexport function unsub(e: Edge): void {\n if (__STATS__) stats.unlinks++;\n\n const s = e.from;\n const before = e.prevSub;\n const after = e.nextSub;\n\n if (before !== null) before.nextSub = after;\n else s.subs = after;\n\n if (after !== null) after.prevSub = before;\n else s.lastSub = before;\n // Subscribers are in birth order, so an old removed edge would point at a\n // much younger one and keep it (and its watcher) alive across scavenges.\n e.prevSub = null;\n e.nextSub = null;\n}\n", "// Reactive kernel: tracking, propagation, validation and execution.\n// Detached edges must not retain pointers into live lists.\n// See research/LOG.md for representation rationale.\n\nimport {\n Flag,\n KEEP,\n CounterLimit,\n SCAN,\n SETTLE,\n TRIM,\n UNREAD,\n cut,\n move,\n settle,\n unsub,\n} from \"./graph\";\nimport type { Edge, Flags, Node, WorkStack } from \"./graph\";\nimport { stats } from \"./instrumentation\";\n\nexport { node } from \"./graph\";\nexport { stats } from \"./instrumentation\";\n\n/** The running consumer, or null outside a tracking pass. */\nlet active: Node | null = null;\n/** Last accepted edge of the running pass; saved with `active` on nesting. */\nlet cursor: Edge | null = null;\n/**\n * One LIFO work stack shared by all walks: sibling edges to resume (mark),\n * edges to climb back through (pull), and slot cells an indexed run displaced.\n * Every walk pops back to its own base before returning. Once the stack is\n * empty, an oversized buffer is cut back (`trim`).\n */\nconst stack: WorkStack = new Array(256).fill(null);\n/** First free index of `stack`. */\nlet top = 0;\n/** Longest the work stack has been when `trim` cut it (research). */\nlet peak = 0;\nlet wake: (w: Node) => void = () => {};\n\n/**\n * The host is told about every watcher a write or mark reaches. It owns the\n * deduplication (a queued watcher is not queued again), so a watcher left\n * STALE by a failed check is delivered again by the next push.\n */\nexport function onWake(fn: (w: Node) => void): void {\n wake = fn;\n}\n\n/** No tracked consumer or occupied work-stack slot (tests; untracked suspends cursor). */\nexport function idle(): boolean {\n return active === null && top === 0;\n}\n\n/**\n * Deepest work-stack top reached so far (research). Walks null the slots\n * they pop and only `trim` shrinks the array, so the high-water mark is its\n * length or the longest length `trim` cut.\n */\nexport function stackHighWater(): number {\n return stack.length > peak ? stack.length : peak;\n}\n\n/** Slots the work stack holds now (tests). */\nexport function stackLength(): number {\n return stack.length;\n}\n\n/**\n * When empty, cut a stack that a deep walk grew past TRIM back to KEEP slots,\n * so one deep walk does not hold its depth in memory for good. Setting the\n * length right-trims V8's backing store in place: no allocation, and the\n * array stays packed.\n *\n * Only walks that push check at their exits: mark, verify (also on failure),\n * and unindex. An empty stack has no entries to preserve even if a user\n * callback or a graph cursor is suspended in local variables. `active`\n * is a tracking context, not a call-depth counter (untracked clears it).\n * The length test comes first: a small buffer pays no load of `top`.\n */\nfunction trim(): void {\n if (stack.length > TRIM && top === 0) {\n if (__STATS__ && stack.length > peak) peak = stack.length;\n\n stack.length = KEEP;\n }\n}\n\n// ------------------------------------------------------------------- write\n\nexport function write(s: Node, value: unknown): void {\n if (Object.is(s.value, value)) return;\n\n s.value = value;\n s.ver = (s.ver + 1) & CounterLimit.Mask;\n\n for (let e = s.subs; e !== null; e = e.nextSub) {\n const n = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n\n if ((f & Flag.Busy) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n if ((f & Flag.Watch) !== 0) wake(n);\n else {\n const subs = n.subs;\n\n if (subs !== null) mark(subs);\n }\n }\n } else if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Dirty | Flag.Woken;\n\n wake(n);\n } else if ((f & Flag.Stale) !== 0) {\n n.flags = f | Flag.Dirty;\n } else {\n n.flags = f | Flag.Dirty | Flag.Stale;\n\n const subs = n.subs;\n\n if (subs !== null) mark(subs);\n }\n }\n}\n\n/** Mark everything below the subscriber list `first` STALE, depth first. */\nfunction mark(first: Edge): void {\n const base = top;\n let e: Edge | null = first;\n\n for (;;) {\n while (e !== null) {\n const n: Node = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n // A watcher has no subscribers; the host deduplicates, so it is always told.\n if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Stale;\n wake(n);\n } else if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n const subs = n.subs;\n\n if (subs !== null) {\n const next = e.nextSub;\n\n if (next !== null) stack[top++] = next;\n\n e = subs;\n continue;\n }\n }\n e = e.nextSub;\n }\n\n if (top === base) {\n trim();\n return;\n }\n\n e = stack[--top] as Edge;\n stack[top] = null;\n }\n}\n\n// -------------------------------------------------------------------- read\n\n/** Bring `n` up to date if it is a stale computed, then track it. */\nexport function read(n: Node): unknown {\n const value = peek(n);\n const c = active;\n\n if (c !== null) {\n if (__STATS__) stats.reads++;\n\n const last = cursor;\n const next = last === null ? c.deps : last.next;\n\n if (next !== null && next.from === n) {\n if (__STATS__) stats.hits++;\n\n cursor = next;\n next.ver = n.ver;\n } else miss(c, n, last, next);\n }\n return value;\n}\n\n/** `read` without its tracking half: bring `n` up to date, record no relation. */\nexport function peek(n: Node): unknown {\n if ((n.flags & (Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking)) !== 0)\n pull(n);\n\n return n.value;\n}\n\n/**\n * Pull a computed up to date. Existential, in read order: sources are\n * checked first to last and the first changed one makes the node run. A\n * STALE source is descended into (the stack keeps the edge to climb back\n * through); a DIRTY one runs. A throw leaves every node on the walk DIRTY.\n * A node whose run is on the stack is a cycle. A development build also\n * diagnoses a node whose validation is on the stack (CHECKING): C1 is a\n * precondition, so its committed value never answers such a demand there.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n\n if ((f & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((f & Flag.Dirty) !== 0) return exec(root);\n\n verify(root, f);\n}\n\n/**\n * The walk of `pull` for a STALE node that is not DIRTY, BUSY or CHECKING;\n * kept apart so `pull` stays small. A node disposed during the walk is not\n * run (W3), nor left DIRTY.\n *\n * Development builds diagnose C1: a node on the walk is CHECKING until it is\n * clean or runs, and a source on the stack (BUSY or CHECKING) is a cycle\n * even when no push marked it again, since its version is not yet a fact.\n * CHECKING is set by the store that clears STALE and cleared by `exec` as it\n * sets BUSY; only a node found clean pays a store of its own. Production\n * builds compile all of it away.\n */\nfunction verify(root: Node, f: Flags): void {\n root.flags = __DEV__ ? (f & ~Flag.Stale) | Flag.Checking : f & ~Flag.Stale;\n const base = top;\n let n = root;\n let e = root.deps;\n\n try {\n for (;;) {\n settled: {\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n\n if (__STATS__) stats.checks++;\n\n if (\n (sf &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0\n ) {\n if ((sf & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((sf & Flag.Dirty) === 0) {\n s.flags = __DEV__\n ? (sf & ~Flag.Stale) | Flag.Checking\n : sf & ~Flag.Stale;\n stack[top++] = e;\n n = s;\n e = s.deps;\n continue;\n }\n exec(s);\n }\n\n if (s.ver !== e.ver) {\n if (n.fn !== null) exec(n);\n break settled;\n }\n e = e.next;\n }\n // Every source unchanged: clean, and no longer on the walk.\n if (__DEV__) n.flags &= ~Flag.Checking;\n }\n\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) {\n trim();\n\n return;\n }\n\n const up = stack[--top] as Edge;\n stack[top] = null;\n n = up.to;\n\n if (up.from.ver === up.ver) {\n e = up.next;\n break;\n }\n\n if (n.fn !== null) exec(n);\n }\n }\n } catch (error) {\n if (n.fn !== null)\n n.flags = __DEV__\n ? (n.flags | Flag.Dirty) & ~Flag.Checking\n : n.flags | Flag.Dirty;\n\n while (top > base) {\n const p = (stack[--top] as Edge).to;\n\n if (p.fn !== null)\n p.flags = __DEV__\n ? (p.flags | Flag.Dirty) & ~Flag.Checking\n : p.flags | Flag.Dirty;\n stack[top] = null;\n }\n trim();\n throw error;\n }\n}\n\n/**\n * Universal check of a watcher: pull every committed source, so a throwing\n * one surfaces before any irreversible step. Marks DIRTY on change. Tentative\n * edges (reads of a failed run) only wake; they exist only while a run is\n * owed, and then versions need no comparison.\n */\nexport function check(w: Node): void {\n const tentative = (w.gen + 1) & CounterLimit.Mask;\n let e = w.deps;\n\n // Until a run is owed, versions decide; then only the pulls remain. A\n // development build pulls a source on the stack too, so the C1 diagnosis\n // fails here, before the cleanup (P4).\n if ((w.flags & Flag.Dirty) === 0) {\n for (; e !== null; e = e.next) {\n const s = e.from;\n\n if (__STATS__) stats.checks++;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n\n if (s.ver !== e.ver) {\n w.flags |= Flag.Dirty;\n e = e.next;\n break;\n }\n }\n }\n\n for (; e !== null; e = e.next) {\n if (__STATS__) stats.checks++;\n\n const s = e.from;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n }\n}\n\nexport function untracked<T>(fn: () => T): T {\n const prev = active;\n active = null;\n\n try {\n return fn();\n } finally {\n active = prev;\n }\n}\n\n// --------------------------------------------------------------------- run\n\n/** Run `n.fn` as a tracking pass; commit value and edges on success. */\nexport function exec(n: Node): void {\n const prev = active;\n const prevCursor = cursor;\n const base = top;\n\n if (__STATS__) stats.runs++;\n // Cleared before the run: a push during it marks the node again. In a\n // development build a computed's validation ends here, BUSY taking over\n // from CHECKING; on a watcher the bit is WOKEN, which this run discharges.\n n.flags =\n (n.flags &\n ~(__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Checking\n : Flag.Stale | Flag.Dirty)) |\n Flag.Busy;\n active = n;\n cursor = null;\n let value: unknown;\n\n try {\n value = n.fn!(n.value);\n } catch (error) {\n const last = cursor as Edge | null;\n active = prev;\n cursor = prevCursor;\n fail(n, base, last);\n throw error;\n }\n const last = cursor as Edge | null;\n active = prev;\n cursor = prevCursor;\n const f = n.flags;\n\n if (last !== null && last.from.slot === last) unindex(n, base);\n\n n.flags = f & ~Flag.Busy;\n\n if (n.fn === null) return detach(n);\n // Commit only after a successful run. Dropping the unread suffix removes\n // sources that this pass no longer depends on.\n if ((last === null ? n.deps : last.next) !== null) cut(n, last);\n\n n.gen = (n.gen + 1) & CounterLimit.Mask;\n\n // Every edge is committed now; see SETTLE.\n if ((n.gen & SETTLE) === 0) settle(n);\n\n if (Object.is(value, n.value) === false) {\n n.value = value;\n n.ver = (n.ver + 1) & CounterLimit.Mask;\n }\n}\n\n/**\n * A failed run: keep committed edges and this run's reads as the wake set,\n * drop unread edges earlier failed runs left behind, owe a run.\n */\nfunction fail(n: Node, base: number, last: Edge | null): void {\n if (last !== null && last.from.slot === last) unindex(n, base);\n\n n.flags &= ~Flag.Busy;\n\n if (n.fn === null) return detach(n);\n\n n.flags |= Flag.Dirty;\n const tentative = (n.gen + 1) & CounterLimit.Mask;\n for (let e = last === null ? n.deps : last.next; e !== null;) {\n const after: Edge | null = e.next;\n\n if (e.gen === tentative) {\n const before = e.prev;\n\n if (before !== null) before.next = after;\n else n.deps = after;\n\n if (after !== null) after.prev = before;\n\n unsub(e);\n e.prev = null;\n e.next = null;\n }\n e = after;\n }\n}\n\n/** Detach `n` from its sources for good; inside its own run, at its end. */\nexport function dispose(n: Node): void {\n if (n.fn === null) return;\n\n n.fn = null;\n\n if ((n.flags & Flag.Busy) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(Flag.Stale | Flag.Dirty | Flag.Woken);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nexport function index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n\n let unread = false;\n\n for (let e = c.deps; e !== null; e = e.next) {\n if (__STATS__) stats.indexed++;\n\n if (e === next) unread = true;\n\n if (unread) e.ver = UNREAD;\n\n claim(e.from, e);\n }\n}\n\n/**\n * Point the source's slot at `e`, keeping a displaced cell to give back.\n * A cell's immutable `from` is its source, so the cell alone is enough.\n */\nexport function claim(s: Node, e: Edge): void {\n const cell = s.slot;\n\n if (cell !== null) stack[top++] = cell;\n s.slot = e;\n}\n\n/** Clear the run's slots, give back the cells it displaced. */\nfunction unindex(n: Node, base: number): void {\n for (let e = n.deps; e !== null; e = e.next) e.from.slot = null;\n\n while (top > base) {\n const cell = stack[--top] as Edge;\n stack[top] = null;\n cell.from.slot = cell;\n }\n trim();\n}\n\n/** A new edge at the cursor, appended to the source's subscribers. */\nexport function link(\n c: Node,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (__STATS__) stats.links++;\n\n const tail = s.lastSub;\n const e: Edge = {\n from: s,\n to: c,\n ver: s.ver,\n gen: (c.gen + 1) & CounterLimit.Mask,\n prev: last,\n next,\n prevSub: tail,\n nextSub: null,\n };\n\n if (last !== null) last.next = e;\n else c.deps = e;\n\n if (next !== null) next.prev = e;\n\n if (tail !== null) tail.nextSub = e;\n else s.subs = e;\n s.lastSub = e;\n cursor = e;\n}\n\n// Exact dependency tracking: resolve a read outside the stable cursor path.\n// Proofs come cheapest first: the cursor (in `read`), the previous read, a\n// source nobody reads, bounded scans of this reader's list, then the source's\n// first subscribers. Only a source with more subscribers than the budget needs\n// the slot index. Slot walks and edge creation remain in the kernel because\n// they mutate its shared stack.\n\n/**\n * A read that is not the next edge in read order. Every answer is exact. A\n * repeated read keeps the version of the pass's first observation (P5): a\n * change between the two reads stays visible to the next validation.\n */\nfunction miss(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (last !== null) {\n const from = last.from;\n\n if (from === s) {\n if (__STATS__) stats.repeats++;\n\n return;\n }\n // An indexed pass owns its cursor's source slot. Nested passes restore it.\n if (from.slot === last) return slot(c, s, last, next);\n }\n /** The pair's edge through the source's subscribers; undefined: unknown. */\n let found: Edge | null | undefined;\n const first = s.subs;\n\n // A source without subscribers is absent, whatever the unread suffix mode.\n if (first === null) return link(c, s, last, next);\n\n // Marking, moves and links preserve a continuous UNREAD suffix.\n if (next === null || next.ver !== UNREAD) {\n // An edge found by a scan is the pair's only one; absence needs one side\n // scanned completely (this reader's list, or the source's subscribers).\n let e = next !== null ? next.next : null;\n let budget = SCAN;\n\n while (e !== null && e.from !== s && --budget !== 0) e = e.next;\n\n if (e !== null && e.from === s) {\n if (__STATS__) stats.scans++;\n\n e.ver = s.ver;\n cursor = e;\n return move(c, e, last, next!);\n }\n let p = last !== null ? last.prev : null;\n budget = SCAN;\n\n while (p !== null && p.from !== s && --budget !== 0) p = p.prev;\n\n if (p !== null && p.from === s) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n\n // Absent if this reader's whole list was scanned; otherwise the source's\n // first subscribers decide. Found as the first one: a lookup through the\n // subscribers costs no load beyond the read itself, so mark the unread\n // suffix and let the rest of the pass find its pairs that way, with no\n // slot to fill or clear. Found deeper: every lookup would walk other\n // readers' edges, so index; either way the found edge is taken at once.\n found = e === null && p === null ? null : pair(first, c);\n\n if (found !== null && found !== undefined) begin(c, found, first, next);\n } else found = pair(first, c);\n\n if (found === null) return link(c, s, last, next);\n\n if (found !== undefined) return take(c, found, s, last, next);\n // Past the budget: index the pass, and the slot decides from here on.\n index(c, next);\n slot(c, s, last, next);\n}\n\n/** An indexed pass's lookup: the source's slot holds the pair's edge, or the pair is absent. */\nfunction slot(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (__STATS__) stats.lookups++;\n\n const cell = s.slot;\n\n if (cell !== null && cell.to === c) return take(c, cell, s, last, next);\n link(c, s, last, next);\n claim(s, cursor as Edge);\n}\n\n/**\n * The pair's edge among the source's first SCAN subscribers: null if the list\n * ends first (absent), undefined if the budget runs out (unknown).\n */\nfunction pair(first: Edge | null, c: Node): Edge | null | undefined {\n if (__STATS__) stats.lookups++;\n\n let budget = SCAN;\n\n for (let e = first; e !== null; e = e.nextSub) {\n if (e.to === c) return e;\n\n if (--budget === 0) return undefined;\n }\n return null;\n}\n\n/**\n * The pass's lookup from here on, decided by the found edge's place: the\n * source's first subscriber marks the unread suffix, from `next` on (a walk\n * over edges, not sources); deeper, index.\n */\nfunction begin(c: Node, e: Edge, first: Edge | null, next: Edge | null): void {\n if (next === null) return;\n\n if (e !== first) return index(c, next);\n\n if (__STATS__) stats.sweeps++;\n\n for (let u: Edge | null = next; u !== null; u = u.next) {\n if (__STATS__) stats.indexed++;\n\n u.ver = UNREAD;\n }\n}\n\n/**\n * A found edge: an unread one is observed and moves to the cursor; a repeated\n * read keeps its place and its first observation's version (P5).\n */\nfunction take(\n c: Node,\n e: Edge,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (e.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n if (__STATS__) stats.moves++;\n\n e.ver = s.ver;\n move(c, e, last, next!);\n cursor = e;\n}\n", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport { Flag } from \"./graph\";\nimport type { Cleanup, Fn, Node } from \"./graph\";\nimport { check, dispose, exec, node, onWake, untracked } from \"./kernel\";\n\nconst queue: (Node | null)[] = [];\n// The host's registers. Exported so a write's caller can test for work\n// without a call into the host; ES bindings are read-only to importers, so\n// only this module writes them.\n/** Queued watchers. */\nexport let size = 0;\n/** Open batches. */\nexport let depth = 0;\n/** A drain loop is open. */\nexport let draining = false;\n\n/** Queue a scheduled watcher once; the kernel calls this when it wakes one. */\nexport function wake(w: Node): void {\n const flags = w.flags;\n\n if ((flags & (Flag.Auto | Flag.Queued)) !== Flag.Auto) return;\n\n w.flags = flags | Flag.Queued;\n queue[size++] = w;\n}\n\nonWake(wake);\n\n/** A watcher runs `fn` and keeps a returned function as its cleanup. */\nexport function watcher(fn: Fn, auto: boolean): Node {\n return node(\n fn,\n undefined,\n Flag.Watch | Flag.Stale | Flag.Dirty | (auto ? Flag.Auto : 0),\n );\n}\n\n/**\n * Check every committed source unless only signals woke it, then, if a run\n * is owed, consume the previous cleanup and run. A watcher neither STALE,\n * WOKEN is clean, or cold after a failure until a source changes. A failed\n * check leaves it STALE, so the next run checks again (V1) and the next push\n * finds it queued or delivers it again.\n * Precondition: its own run is not on the stack (flush is not reentrant).\n */\nexport function run(w: Node): void {\n const flags = w.flags;\n\n if (w.fn === null || (flags & (Flag.Stale | Flag.Woken)) === 0) return;\n\n w.flags = flags & ~(Flag.Stale | Flag.Woken);\n\n if ((flags & Flag.Stale) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= Flag.Stale;\n throw error;\n }\n }\n\n if ((w.flags & Flag.Dirty) === 0 || w.fn === null) return;\n\n const cleanup = w.value;\n\n if (typeof cleanup === \"function\") {\n w.value = undefined;\n untracked(cleanup as Cleanup);\n\n if (w.fn === null) return;\n }\n exec(w);\n}\n\n/** Terminal: detach, then run the last cleanup outside tracking. */\nexport function stop(w: Node): void {\n if (w.fn === null) return;\n\n const cleanup = w.value;\n w.value = undefined;\n dispose(w);\n\n if (typeof cleanup === \"function\") untracked(cleanup as Cleanup);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (size === 0 || draining || depth !== 0) return;\n\n drainNow();\n}\n\n/**\n * `flush` with its guard at the call site: a write's caller tests\n * `size !== 0 && !draining && depth === 0` first (the queue first, so a write\n * that woke nothing costs one load), and calls this only when there is work.\n */\nexport function drainNow(): void {\n draining = true;\n drain(false, undefined);\n}\n\n/**\n * A new scheduled watcher's first run. With no drain open, no batch and an\n * empty queue this is `wake(w); flush()` with the queue slot skipped: the\n * drain opens on `w` (so its run stays inside the drain loop, LOG 11), and a\n * fresh watcher has no sources to check and no cleanup, so `run` is `exec`.\n */\nexport function start(w: Node): void {\n if (draining || depth !== 0 || size !== 0) {\n wake(w);\n return flush();\n }\n draining = true;\n\n try {\n exec(w);\n } catch (error) {\n return drain(true, error);\n }\n drain(false, undefined);\n}\n\n/** The drain loop, opened by the caller: FIFO, the first error rethrown at the end. */\nfunction drain(failed: boolean, first: unknown): void {\n for (let i = 0; i < size; i++) {\n const queuedWatcher = queue[i]!;\n queue[i] = null;\n queuedWatcher.flags &= ~Flag.Queued;\n\n try {\n run(queuedWatcher);\n } catch (error) {\n if (!failed) first = error;\n\n failed = true;\n }\n }\n size = 0;\n draining = false;\n\n if (failed) throw first;\n}\n\nexport function batch<T>(fn: () => T): T {\n depth++;\n\n try {\n return fn();\n } finally {\n if (--depth === 0) flush();\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAAA;AAAA,EAAA;AAAA;AAAA,cAAAC;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiFO,IAAM,SAAS,IAOT,SAAS,WAET,OAAO,GAMP,OAAO,KACP,OAAO;AAIb,SAAS,KAAK,IAAgB,OAAgB,OAAoB;AACvE,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,KAAK;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN,MAAM;AAAA,IACN,SAAS;AAAA,IACT,MAAM;AAAA,IACN,KAAK;AAAA,EACP;AACF;AAGO,SAAS,KAAK,GAAS,GAAS,MAAmB,MAAkB;AAC1E,MAAM,SAAS,EAAE,MACX,QAAQ,EAAE;AAChB,SAAO,OAAO,OAEV,UAAU,SAAM,MAAM,OAAO,SAEjC,EAAE,OAAO,MACT,EAAE,OAAO,MAEL,SAAS,OAAM,KAAK,OAAO,IAC1B,EAAE,OAAO,GACd,KAAK,OAAO;AACd;AAGO,SAAS,IAAI,GAAS,MAAyB;AACpD,MAAI,IAAI,SAAS,OAAO,EAAE,OAAO,KAAK;AAEtC,MAAI,MAAM;AAQV,SANI,SAAS,OAAM,EAAE,OAAO,OACvB,KAAK,OAAO,MAGjB,EAAE,OAAO,MAEF,MAAM,MAAM,IAAI,EAAE,KAAM,OAAM,CAAC;AACxC;AAGO,SAAS,OAAO,GAAe;AACpC,MAAM,MAAM,EAAE;AAEd,WAAS,IAAI,EAAE,MAAM,MAAM,M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
  // src/graph.ts
4
- var UNREAD = -1, MASK = 1073741823, SETTLE = 536870911, SCAN = 8, KEEP = 256, TRIM = 512;
4
+ var UNREAD = -1, SETTLE = 536870911, SCAN = 8, KEEP = 256, TRIM = 512;
5
5
  function node(fn, value, flags) {
6
6
  return {
7
7
  flags,
@@ -9,7 +9,6 @@ function node(fn, value, flags) {
9
9
  ver: 0,
10
10
  fn,
11
11
  deps: null,
12
- last: null,
13
12
  subs: null,
14
13
  lastSub: null,
15
14
  slot: null,
@@ -18,7 +17,7 @@ function node(fn, value, flags) {
18
17
  }
19
18
  function move(c, e, last, next) {
20
19
  let before = e.prev, after = e.next;
21
- before.next = after, after !== null && (after.prev = before), e.prev = last, e.next = next, last !== null ? last.next = e : c.deps = e, next.prev = e, c.last = e;
20
+ before.next = after, after !== null && (after.prev = before), e.prev = last, e.next = next, last !== null ? last.next = e : c.deps = e, next.prev = e;
22
21
  }
23
22
  function cut(c, last) {
24
23
  let e = last === null ? c.deps : last.next;
@@ -35,21 +34,36 @@ function unsub(e) {
35
34
  }
36
35
 
37
36
  // src/kernel.ts
38
- var active = null, stack = new Array(256).fill(null), top = 0;
37
+ var active = null, cursor = null, stack = new Array(256).fill(null), top = 0;
39
38
  var wake = () => {
40
39
  };
41
40
  function onWake(fn) {
42
41
  wake = fn;
43
42
  }
44
43
  function trim() {
45
- stack.length = KEEP;
44
+ stack.length > TRIM && top === 0 && (stack.length = KEEP);
46
45
  }
47
46
  function write(s, value) {
48
47
  if (!Object.is(s.value, value)) {
49
- s.value = value, s.ver = s.ver + 1 & MASK;
48
+ s.value = value, s.ver = s.ver + 1 & 1073741823 /* Mask */;
50
49
  for (let e = s.subs; e !== null; e = e.nextSub) {
51
50
  let n = e.to, f = n.flags;
52
- (f & 4 /* Busy */) !== 0 ? (f & 1 /* Stale */) === 0 && (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0 ? wake(n) : n.subs !== null && mark(n.subs)) : (f & 16 /* Watch */) !== 0 ? (n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n)) : (f & 1 /* Stale */) !== 0 ? n.flags = f | 2 /* Dirty */ : (n.flags = f | 2 /* Dirty */ | 1 /* Stale */, n.subs !== null && mark(n.subs));
51
+ if ((f & 4 /* Busy */) !== 0) {
52
+ if ((f & 1 /* Stale */) === 0)
53
+ if (n.flags = f | 1 /* Stale */, (f & 16 /* Watch */) !== 0) wake(n);
54
+ else {
55
+ let subs = n.subs;
56
+ subs !== null && mark(subs);
57
+ }
58
+ } else if ((f & 16 /* Watch */) !== 0)
59
+ n.flags = f | 2 /* Dirty */ | 32 /* Woken */, wake(n);
60
+ else if ((f & 1 /* Stale */) !== 0)
61
+ n.flags = f | 2 /* Dirty */;
62
+ else {
63
+ n.flags = f | 2 /* Dirty */ | 1 /* Stale */;
64
+ let subs = n.subs;
65
+ subs !== null && mark(subs);
66
+ }
53
67
  }
54
68
  }
55
69
  }
@@ -60,21 +74,29 @@ function mark(first) {
60
74
  let n = e.to, f = n.flags;
61
75
  if ((f & 16 /* Watch */) !== 0)
62
76
  n.flags = f | 1 /* Stale */, wake(n);
63
- else if ((f & 1 /* Stale */) === 0 && (n.flags = f | 1 /* Stale */, n.subs !== null)) {
64
- e.nextSub !== null && (stack[top++] = e.nextSub), e = n.subs;
65
- continue;
77
+ else if ((f & 1 /* Stale */) === 0) {
78
+ n.flags = f | 1 /* Stale */;
79
+ let subs = n.subs;
80
+ if (subs !== null) {
81
+ let next = e.nextSub;
82
+ next !== null && (stack[top++] = next), e = subs;
83
+ continue;
84
+ }
66
85
  }
67
86
  e = e.nextSub;
68
87
  }
69
- if (top === base) return;
88
+ if (top === base) {
89
+ trim();
90
+ return;
91
+ }
70
92
  e = stack[--top], stack[top] = null;
71
93
  }
72
94
  }
73
95
  function read(n) {
74
96
  let value = peek(n), c = active;
75
97
  if (c !== null) {
76
- let last = c.last, next = last === null ? c.deps : last.next;
77
- next !== null && next.from === n ? (c.last = next, next.ver = n.ver) : miss(c, n, last, next);
98
+ let last = cursor, next = last === null ? c.deps : last.next;
99
+ next !== null && next.from === n ? (cursor = next, next.ver = n.ver) : miss(c, n, last, next);
78
100
  }
79
101
  return value;
80
102
  }
@@ -115,7 +137,7 @@ function verify(root, f) {
115
137
  }
116
138
  for (; ; ) {
117
139
  if (top === base) {
118
- stack.length > TRIM && base === 0 && trim();
140
+ trim();
119
141
  return;
120
142
  }
121
143
  let up = stack[--top];
@@ -131,11 +153,11 @@ function verify(root, f) {
131
153
  let p = stack[--top].to;
132
154
  p.fn !== null && (p.flags = (p.flags | 2 /* Dirty */) & -33), stack[top] = null;
133
155
  }
134
- throw error;
156
+ throw trim(), error;
135
157
  }
136
158
  }
137
159
  function check(w) {
138
- let tentative = w.gen + 1 & MASK, e = w.deps;
160
+ let tentative = w.gen + 1 & 1073741823 /* Mask */, e = w.deps;
139
161
  if ((w.flags & 2 /* Dirty */) === 0)
140
162
  for (; e !== null; e = e.next) {
141
163
  let s = e.from;
@@ -159,24 +181,25 @@ function untracked(fn) {
159
181
  }
160
182
  }
161
183
  function exec(n) {
162
- let prev = active, base = top;
163
- n.flags = n.flags & -36 | 4 /* Busy */, n.last = null, active = n;
184
+ let prev = active, prevCursor = cursor, base = top;
185
+ n.flags = n.flags & -36 | 4 /* Busy */, active = n, cursor = null;
164
186
  let value;
165
187
  try {
166
188
  value = n.fn(n.value);
167
189
  } catch (error) {
168
- throw active = prev, fail(n, base), error;
190
+ let last2 = cursor;
191
+ throw active = prev, cursor = prevCursor, fail(n, base, last2), error;
169
192
  }
170
- active = prev;
193
+ let last = cursor;
194
+ active = prev, cursor = prevCursor;
171
195
  let f = n.flags;
172
- if ((f & 8 /* Indexed */) !== 0 && unindex(n, base), n.flags = f & -269, n.fn === null) return detach(n);
173
- let last = n.last;
174
- (last === null ? n.deps : last.next) !== null && cut(n, last), n.gen = n.gen + 1 & MASK, (n.gen & SETTLE) === 0 && settle(n), Object.is(value, n.value) === !1 && (n.value = value, n.ver = n.ver + 1 & MASK);
196
+ if (last !== null && last.from.slot === last && unindex(n, base), n.flags = f & -5, n.fn === null) return detach(n);
197
+ (last === null ? n.deps : last.next) !== null && cut(n, last), n.gen = n.gen + 1 & 1073741823 /* Mask */, (n.gen & SETTLE) === 0 && settle(n), Object.is(value, n.value) === !1 && (n.value = value, n.ver = n.ver + 1 & 1073741823 /* Mask */);
175
198
  }
176
- function fail(n, base) {
177
- if ((n.flags & 8 /* Indexed */) !== 0 && unindex(n, base), n.flags &= -269, n.fn === null) return detach(n);
199
+ function fail(n, base, last) {
200
+ if (last !== null && last.from.slot === last && unindex(n, base), n.flags &= -5, n.fn === null) return detach(n);
178
201
  n.flags |= 2 /* Dirty */;
179
- let tentative = n.gen + 1 & MASK, last = n.last;
202
+ let tentative = n.gen + 1 & 1073741823 /* Mask */;
180
203
  for (let e = last === null ? n.deps : last.next; e !== null; ) {
181
204
  let after = e.next;
182
205
  if (e.gen === tentative) {
@@ -193,54 +216,55 @@ function detach(n) {
193
216
  cut(n, null), n.flags &= -36;
194
217
  }
195
218
  function index(c, next) {
196
- c.flags |= 8 /* Indexed */;
197
219
  let unread = !1;
198
220
  for (let e = c.deps; e !== null; e = e.next)
199
221
  e === next && (unread = !0), unread && (e.ver = UNREAD), claim(e.from, e);
200
222
  }
201
223
  function claim(s, e) {
202
- s.slot !== null && (stack[top++] = s, stack[top++] = s.slot), s.slot = e;
224
+ let cell = s.slot;
225
+ cell !== null && (stack[top++] = cell), s.slot = e;
203
226
  }
204
227
  function unindex(n, base) {
205
228
  for (let e = n.deps; e !== null; e = e.next) e.from.slot = null;
206
229
  for (; top > base; ) {
207
230
  let cell = stack[--top];
208
- stack[top] = null;
209
- let s = stack[--top];
210
- stack[top] = null, s.slot = cell;
231
+ stack[top] = null, cell.from.slot = cell;
211
232
  }
233
+ trim();
212
234
  }
213
235
  function link(c, s, last, next) {
214
236
  let tail = s.lastSub, e = {
215
237
  from: s,
216
238
  to: c,
217
239
  ver: s.ver,
218
- gen: c.gen + 1 & MASK,
240
+ gen: c.gen + 1 & 1073741823 /* Mask */,
219
241
  prev: last,
220
242
  next,
221
243
  prevSub: tail,
222
244
  nextSub: null
223
245
  };
224
- last !== null ? last.next = e : c.deps = e, next !== null && (next.prev = e), tail !== null ? tail.nextSub = e : s.subs = e, s.lastSub = e, c.last = e;
246
+ last !== null ? last.next = e : c.deps = e, next !== null && (next.prev = e), tail !== null ? tail.nextSub = e : s.subs = e, s.lastSub = e, cursor = e;
225
247
  }
226
248
  function miss(c, s, last, next) {
227
- if (last !== null && last.from === s)
228
- return;
229
- let f = c.flags;
230
- if ((f & 8 /* Indexed */) !== 0) return slot(c, s, last, next);
231
- let found;
232
- if ((f & 256 /* Marked */) === 0) {
233
- if (s.subs === null) return link(c, s, last, next);
249
+ if (last !== null) {
250
+ let from = last.from;
251
+ if (from === s)
252
+ return;
253
+ if (from.slot === last) return slot(c, s, last, next);
254
+ }
255
+ let found, first = s.subs;
256
+ if (first === null) return link(c, s, last, next);
257
+ if (next === null || next.ver !== UNREAD) {
234
258
  let e = next !== null ? next.next : null, budget = SCAN;
235
259
  for (; e !== null && e.from !== s && --budget !== 0; ) e = e.next;
236
260
  if (e !== null && e.from === s)
237
- return e.ver = s.ver, move(c, e, last, next);
261
+ return e.ver = s.ver, cursor = e, move(c, e, last, next);
238
262
  let p = last !== null ? last.prev : null;
239
263
  for (budget = SCAN; p !== null && p.from !== s && --budget !== 0; ) p = p.prev;
240
264
  if (p !== null && p.from === s)
241
265
  return;
242
- found = e === null && p === null ? null : pair(s, c), found != null && begin(c, s, found, next);
243
- } else found = pair(s, c);
266
+ found = e === null && p === null ? null : pair(first, c), found != null && begin(c, found, first, next);
267
+ } else found = pair(first, c);
244
268
  if (found === null) return link(c, s, last, next);
245
269
  if (found !== void 0) return take(c, found, s, last, next);
246
270
  index(c, next), slot(c, s, last, next);
@@ -248,30 +272,32 @@ function miss(c, s, last, next) {
248
272
  function slot(c, s, last, next) {
249
273
  let cell = s.slot;
250
274
  if (cell !== null && cell.to === c) return take(c, cell, s, last, next);
251
- link(c, s, last, next), claim(s, c.last);
275
+ link(c, s, last, next), claim(s, cursor);
252
276
  }
253
- function pair(s, c) {
277
+ function pair(first, c) {
254
278
  let budget = SCAN;
255
- for (let e = s.subs; e !== null; e = e.nextSub) {
279
+ for (let e = first; e !== null; e = e.nextSub) {
256
280
  if (e.to === c) return e;
257
281
  if (--budget === 0) return;
258
282
  }
259
283
  return null;
260
284
  }
261
- function begin(c, s, e, next) {
262
- if (e !== s.subs) return index(c, next);
263
- c.flags |= 256 /* Marked */;
264
- for (let u = next; u !== null; u = u.next)
265
- u.ver = UNREAD;
285
+ function begin(c, e, first, next) {
286
+ if (next !== null) {
287
+ if (e !== first) return index(c, next);
288
+ for (let u = next; u !== null; u = u.next)
289
+ u.ver = UNREAD;
290
+ }
266
291
  }
267
292
  function take(c, e, s, last, next) {
268
- e.ver === UNREAD && (e.ver = s.ver, move(c, e, last, next));
293
+ e.ver === UNREAD && (e.ver = s.ver, move(c, e, last, next), cursor = e);
269
294
  }
270
295
 
271
296
  // src/host.ts
272
297
  var queue = [], size = 0, depth = 0, draining = !1;
273
298
  function wake2(w) {
274
- (w.flags & 192) === 64 /* Auto */ && (w.flags |= 128 /* Queued */, queue[size++] = w);
299
+ let flags = w.flags;
300
+ (flags & 192) === 64 /* Auto */ && (w.flags = flags | 128 /* Queued */, queue[size++] = w);
275
301
  }
276
302
  onWake(wake2);
277
303
  function watcher(fn, auto) {