reflex-quant 0.1.0 → 1.0.0
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.
- package/README.md +63 -58
- package/dist/host.d.cts +10 -1
- package/dist/host.d.ts +10 -1
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +3 -3
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/dist/kernel.d.cts +5 -1
- package/dist/kernel.d.ts +5 -1
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -1,58 +1,63 @@
|
|
|
1
|
-
# reflex-quant
|
|
2
|
-
|
|
3
|
-
An experiment: derive the smallest reactive runtime
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
1
|
+
# reflex-quant
|
|
2
|
+
|
|
3
|
+
An experiment: derive the smallest known reactive runtime implied by a
|
|
4
|
+
frozen contract (semantic invariants and structural bounds, see
|
|
5
|
+
[SPEC.md](SPEC.md)), and measure what is left. The contract was extracted
|
|
6
|
+
from the executable spec of [Reflex](https://github.com/volynetstyle/Reflex)
|
|
7
|
+
(provenance in [research/REPORT.md](research/REPORT.md)); it does not follow
|
|
8
|
+
later changes of Reflex.
|
|
9
|
+
|
|
10
|
+
- **Production code:** three files, [`src/kernel.ts`](src/kernel.ts) (graph,
|
|
11
|
+
tracking, invalidation, validation), [`src/host.ts`](src/host.ts)
|
|
12
|
+
(watcher lifecycle, queue, batch), [`src/index.ts`](src/index.ts)
|
|
13
|
+
(`signal`, `computed`, `effect`, `get`, `set`, `dispose`, `batch`,
|
|
14
|
+
`untracked`; a handle is the node itself). About 2 KB gzip.
|
|
15
|
+
- **Report:** [research/REPORT.md](research/REPORT.md). **Design log**, one
|
|
16
|
+
counterexample per mechanism: [research/LOG.md](research/LOG.md).
|
|
17
|
+
- **Textbook version:** branch
|
|
18
|
+
[`padawan`](https://github.com/volynetstyle/reflex-quant/tree/padawan), the
|
|
19
|
+
same semantics without the engineering (recursive push and pull, one
|
|
20
|
+
exact index, no counters), written as a guided tour. It passes the same
|
|
21
|
+
differential corpus; this branch is its lowering for speed and stack depth.
|
|
22
|
+
|
|
23
|
+
## Results
|
|
24
|
+
|
|
25
|
+
- Equal to Reflex's executable spec, with two documented choices applied,
|
|
26
|
+
on all 27,061 bounded and 18,660 recovery programs and on 100,000
|
|
27
|
+
generated ones, with graph integrity checked after every operation.
|
|
28
|
+
- Current Reflex is that contract plus three failure-path choices, on the
|
|
29
|
+
same corpus. One of them drops an execution obligation that the contract
|
|
30
|
+
(R2) says survives a failure.
|
|
31
|
+
- 23 of 24 mechanism ablations fail a test; the survivor is a constant
|
|
32
|
+
factor only timing sees.
|
|
33
|
+
- On CI: within 25 % of the better of Reflex and alien-signals on 14 of 18
|
|
34
|
+
scenarios, faster than both on 8. Reorders allocate nothing.
|
|
35
|
+
|
|
36
|
+
## Layout
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
src/ kernel.ts, host.ts, index.ts
|
|
40
|
+
test/ differential (spec, contract spec, Reflex, quant), tracking,
|
|
41
|
+
semantics, structural bounds; tools/ (QUANT_TOOLS=1)
|
|
42
|
+
bench/ scenarios, adapters, build, timing (CI only), structure counters
|
|
43
|
+
research/ REPORT.md, LOG.md, mutants.mjs (ablation), versions/ (v0-v3)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Running
|
|
47
|
+
|
|
48
|
+
For development, use Node.js 20.19 or later in the 20.x line, 22.13 or later in the 22.x line, or 24+ and npm (the published library supports Node.js 18+). Install this repository's dependencies with `npm ci`; the Reflex sources, spec harness and fixed production baseline used by the tests and benchmarks are included in `bin/reflex-runtime` and do not require a Reflex checkout. From this directory:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
npm run lint # lint production TypeScript
|
|
52
|
+
npm run format:check # check production formatting
|
|
53
|
+
npm run format # format production code and tool config
|
|
54
|
+
npx vitest run --config vitest.config.ts # all tests
|
|
55
|
+
npx tsc -p tsconfig.json # typecheck src
|
|
56
|
+
node research/mutants.mjs # ablation -> research/mutants.json
|
|
57
|
+
node bench/build.mjs # bundles: quant, Reflex bf71877, alien-signals
|
|
58
|
+
node bench/run.mjs --verify-only # same answers on all three runtimes
|
|
59
|
+
node bench/structure.mjs # structural work per step
|
|
60
|
+
node bench/run.mjs --rounds 9 # timing: run it on CI, not locally
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
CI (`.github/workflows/experiment.yml`) installs this repository and runs the tests, counters and timing independently. The Reflex benchmark baseline is fixed at commit `bf71877`; see `bin/reflex-runtime/README.md` for its provenance.
|
package/dist/host.d.cts
CHANGED
|
@@ -7,7 +7,9 @@ export declare function watcher(fn: Fn, auto: boolean): Node;
|
|
|
7
7
|
/**
|
|
8
8
|
* Check every committed source unless only signals woke it, then, if a run
|
|
9
9
|
* is owed, consume the previous cleanup and run. A watcher neither STALE,
|
|
10
|
-
* WOKEN
|
|
10
|
+
* WOKEN is clean, or cold after a failure until a source changes. A failed
|
|
11
|
+
* check leaves it STALE, so the next run checks again (V1) and the next push
|
|
12
|
+
* finds it queued or delivers it again.
|
|
11
13
|
* Precondition: its own run is not on the stack (flush is not reentrant).
|
|
12
14
|
*/
|
|
13
15
|
export declare function run(w: Node): void;
|
|
@@ -15,5 +17,12 @@ export declare function run(w: Node): void;
|
|
|
15
17
|
export declare function stop(w: Node): void;
|
|
16
18
|
/** Run queued watchers until the queue drains; rethrow the first error. */
|
|
17
19
|
export declare function flush(): void;
|
|
20
|
+
/**
|
|
21
|
+
* A new scheduled watcher's first run. With no drain open, no batch and an
|
|
22
|
+
* empty queue this is `wake(w); flush()` with the queue slot skipped: the
|
|
23
|
+
* drain opens on `w` (so its run stays inside the drain loop, LOG 11), and a
|
|
24
|
+
* fresh watcher has no sources to check and no cleanup, so `run` is `exec`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function start(w: Node): void;
|
|
18
27
|
export declare function batch<T>(fn: () => T): T;
|
|
19
28
|
export {};
|
package/dist/host.d.ts
CHANGED
|
@@ -7,7 +7,9 @@ export declare function watcher(fn: Fn, auto: boolean): Node;
|
|
|
7
7
|
/**
|
|
8
8
|
* Check every committed source unless only signals woke it, then, if a run
|
|
9
9
|
* is owed, consume the previous cleanup and run. A watcher neither STALE,
|
|
10
|
-
* WOKEN
|
|
10
|
+
* WOKEN is clean, or cold after a failure until a source changes. A failed
|
|
11
|
+
* check leaves it STALE, so the next run checks again (V1) and the next push
|
|
12
|
+
* finds it queued or delivers it again.
|
|
11
13
|
* Precondition: its own run is not on the stack (flush is not reentrant).
|
|
12
14
|
*/
|
|
13
15
|
export declare function run(w: Node): void;
|
|
@@ -15,5 +17,12 @@ export declare function run(w: Node): void;
|
|
|
15
17
|
export declare function stop(w: Node): void;
|
|
16
18
|
/** Run queued watchers until the queue drains; rethrow the first error. */
|
|
17
19
|
export declare function flush(): void;
|
|
20
|
+
/**
|
|
21
|
+
* A new scheduled watcher's first run. With no drain open, no batch and an
|
|
22
|
+
* empty queue this is `wake(w); flush()` with the queue slot skipped: the
|
|
23
|
+
* drain opens on `w` (so its run stays inside the drain loop, LOG 11), and a
|
|
24
|
+
* fresh watcher has no sources to check and no cleanup, so `run` is `exec`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function start(w: Node): void;
|
|
18
27
|
export declare function batch<T>(fn: () => T): T;
|
|
19
28
|
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* reflex-quant
|
|
2
|
-
"use strict";var
|
|
1
|
+
/* reflex-quant 1.0.0 | MIT */
|
|
2
|
+
"use strict";var N=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var Q=Object.prototype.hasOwnProperty;var J=(e,n)=>{for(var t in n)N(e,t,{get:n[t],enumerable:!0})},V=(e,n,t,l)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of z(n))!Q.call(e,o)&&o!==t&&N(e,o,{get:()=>n[o],enumerable:!(l=P(n,o))||l.enumerable});return e};var Z=e=>V(N({},"__esModule",{value:!0}),e);var ae={};J(ae,{batch:()=>G,computed:()=>re,dispose:()=>de,effect:()=>se,get:()=>fe,set:()=>ie,signal:()=>ue,untracked:()=>T});module.exports=Z(ae);var d=null,f=[],s=0,g=()=>{};function w(e){g=e}function c(e,n,t){return{flags:t,value:n,ver:0,fn:e,deps:null,last:null,subs:null,lastSub:null,slot:null,gen:0}}function U(e,n){if(!Object.is(e.value,n)){e.value=n,e.ver=e.ver+1&1073741823;for(let t=e.subs;t!==null;t=t.nextSub){let l=t.to,o=l.flags;(o&4)!==0?(o&1)===0&&(l.flags=o|1,(o&16)!==0?g(l):l.subs!==null&&C(l.subs)):(o&16)!==0?(l.flags=o|2|32,g(l)):(o&1)!==0?l.flags=o|2:(l.flags=o|2|1,l.subs!==null&&C(l.subs))}}}function C(e){let n=s,t=e;for(;;){for(;t!==null;){let l=t.to,o=l.flags;if((o&16)!==0)l.flags=o|1,g(l);else if((o&1)===0&&(l.flags=o|1,l.subs!==null)){t.nextSub!==null&&(f[s++]=t.nextSub),t=l.subs;continue}t=t.nextSub}if(s===n)return;t=f[--s],f[s]=null}}function W(e){(e.flags&7)!==0&&m(e);let n=d;if(n!==null){let t=n.last,l=t===null?n.deps:t.next;l!==null&&l.from===e?(n.last=l,l.ver=e.ver):ne(n,e,t,l)}return e.value}function m(e){let n=e.flags;if((n&4)!==0)throw new Error("Cycle detected");if((n&2)!==0)return a(e);$(e,n)}function $(e,n){e.flags=n&-2;let t=s,l=e,o=e.deps;try{for(;;){for(;o!==null;){let u=o.from,r=u.flags;if((r&3)!==0){if((r&4)!==0)throw new Error("Cycle detected");if((r&2)===0){u.flags=r&-2,f[s++]=o,l=u,o=u.deps;continue}a(u)}if(u.ver!==o.ver){a(l);break}o=o.next}for(;;){if(s===t)return;let u=f[--s];if(f[s]=null,l=u.to,u.from.ver===u.ver){o=u.next;break}a(l)}}}catch(u){for(l.flags|=2;s>t;)f[--s].to.flags|=2,f[s]=null;throw u}}function K(e){let n=e.gen+1&1073741823,t=e.deps;if((e.flags&2)===0)for(;t!==null;t=t.next){let l=t.from;if((l.flags&3)!==0&&t.gen!==n&&m(l),l.ver!==t.ver){e.flags|=2,t=t.next;break}}for(;t!==null;t=t.next){let l=t.from;(l.flags&3)!==0&&t.gen!==n&&m(l)}}function T(e){let n=d;d=null;try{return e()}finally{d=n}}function a(e){let n=d,t=s;e.flags=e.flags&-4|4,e.last=null,d=e;let l;try{l=e.fn(e.value)}catch(r){throw d=n,ee(e,t),r}d=n;let o=e.flags;if((o&8)!==0&&B(e,t),e.flags=o&-13,e.fn===null)return k(e);let u=e.last;(u===null?e.deps:u.next)!==null&&M(e,u),e.gen=e.gen+1&1073741823,Object.is(l,e.value)===!1&&(e.value=l,e.ver=e.ver+1&1073741823)}function ee(e,n){if((e.flags&8)!==0&&B(e,n),e.flags&=-13,e.fn===null)return k(e);e.flags|=2;let t=e.gen+1&1073741823,l=e.last;for(let o=l===null?e.deps:l.next;o!==null;){let u=o.next;if(o.gen===t){let r=o.prev;r!==null?r.next=u:e.deps=u,u!==null&&(u.prev=r),O(o),o.prev=null,o.next=null}o=u}}function _(e){e.fn!==null&&(e.fn=null,(e.flags&4)===0&&k(e))}function k(e){M(e,null),e.flags&=-36}function ne(e,n,t,l){if(t!==null&&t.from===n){t.ver=n.ver;return}if((e.flags&8)===0){if(n.subs===null)return A(e,n,t,l,!1);let u=l!==null?l.next:null,r=8;for(;u!==null&&u.from!==n&&--r!==0;)u=u.next;if(u!==null&&u.from===n)return u.ver=n.ver,L(e,u,t,l);let i=t!==null?t.prev:null;for(r=8;i!==null&&i.from!==n&&--r!==0;)i=i.prev;if(i!==null&&i.from===n){i.ver=n.ver;return}if(u===null&&i===null||te(n,e)===!0)return A(e,n,t,l,!1);le(e,l)}let o=n.slot;if(o!==null&&o.to===e){if(o.ver!==-1){o.ver=n.ver;return}return o.ver=n.ver,L(e,o,t,l)}A(e,n,t,l,!0)}function te(e,n){let t=8;for(let l=e.subs;l!==null;l=l.nextSub)if(l.to===n||--t===0)return!1;return!0}function le(e,n){e.flags|=8;let t=!1;for(let l=e.deps;l!==null;l=l.next){l===n&&(t=!0),t&&(l.ver=-1);let o=l.from;o.slot!==null&&(f[s++]=o,f[s++]=o.slot),o.slot=l}}function B(e,n){for(let t=e.deps;t!==null;t=t.next)t.from.slot=null;for(;s>n;){let t=f[--s];f[s]=null;let l=f[--s];f[s]=null,l.slot=t}}function L(e,n,t,l){let o=n.prev,u=n.next;o.next=u,u!==null&&(u.prev=o),n.prev=t,n.next=l,t!==null?t.next=n:e.deps=n,l.prev=n,e.last=n}function A(e,n,t,l,o){let u=n.lastSub,r={from:n,to:e,ver:n.ver,gen:e.gen+1&1073741823,prev:t,next:l,prevSub:u,nextSub:null};t!==null?t.next=r:e.deps=r,l!==null&&(l.prev=r),u!==null?u.nextSub=r:n.subs=r,n.lastSub=r,e.last=r,o&&(n.slot!==null&&(f[s++]=n,f[s++]=n.slot),n.slot=r)}function M(e,n){let t=n===null?e.deps:n.next;if(t!==null)for(n===null?e.deps=null:n.next=null,t.prev=null;t!==null;t=t.next)O(t)}function O(e){let n=e.from,t=e.prevSub,l=e.nextSub;t!==null?t.nextSub=l:n.subs=l,l!==null?l.prevSub=t:n.lastSub=t,e.prevSub=null,e.nextSub=null}var h=64,D=128,I=[],S=0,E=0,v=!1;function X(e){(e.flags&(h|D))===h&&(e.flags|=D,I[S++]=e)}w(X);function H(e,n){return c(e,void 0,19|(n?h:0))}function oe(e){let n=e.flags;if(e.fn===null||(n&33)===0)return;if(e.flags=n&-34,(n&1)!==0)try{K(e)}catch(l){throw e.flags|=1,l}if((e.flags&2)===0||e.fn===null)return;let t=e.value;typeof t=="function"&&(e.value=void 0,T(t),e.fn===null)||a(e)}function q(e){if(e.fn===null)return;let n=e.value;e.value=void 0,_(e),typeof n=="function"&&T(n)}function x(){v||E!==0||S===0||(v=!0,Y(!1,void 0))}function j(e){if(v||E!==0||S!==0)return X(e),x();v=!0;try{a(e)}catch(n){return Y(!0,n)}Y(!1,void 0)}function Y(e,n){for(let t=0;t<S;t++){let l=I[t];I[t]=null,l.flags&=~D;try{oe(l)}catch(o){e||(n=o),e=!0}}if(S=0,v=!1,e)throw n}function G(e){E++;try{return e()}finally{--E===0&&x()}}var R=e=>e;function ue(e){return c(null,e,0)}function re(e){return c(e,void 0,2)}function se(e){let n=H(e,!0);return j(n),n}function fe(e){return W(R(e))}function ie(e,n){U(R(e),n),x()}function de(e){let n=R(e);(n.flags&16)!==0?q(n):_(n)}
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/kernel.ts", "../src/host.ts"],
|
|
4
|
-
"sourcesContent": ["// Public API: signal, computed, effect, get, set, dispose, batch, untracked.\n// A handle is the node itself: nothing is allocated besides the node.\nimport { DIRTY, WATCH, dispose as detach, node, read, write, type Node } from \"./kernel\";\nimport { flush, stop, wake, watcher } from \"./host\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Compile-time brands only. They do not exist at runtime.\ndeclare const SIGNAL: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\nexport interface Signal<T> {\n readonly [SIGNAL]: T;\n}\n\nexport interface Computed<T> {\n readonly [COMPUTED]: T;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport type Readable<T> = Signal<T> | Computed<T>;\n\nconst raw = (handle: unknown): Node => handle as Node;\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T> {\n return node(fn as Node[\"fn\"], undefined, 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 w = watcher(fn, true);\n wake(w);\n flush();\n return w as unknown as Effect;\n}\n\n/** A signal's flags are 0, so one read path serves signals and computeds. */\nexport function get<T>(source: Readable<T>): T {\n return read(raw(source)) as T;\n}\n\nexport function set<T>(source: Signal<T>, value: T): void {\n write(raw(source), value);\n flush();\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const n = raw(target);\n if ((n.flags & WATCH) !== 0) stop(n);\n else detach(n);\n}\n", "// Reactive kernel: nodes, edges, tracking, push, pull, failure.\n//\n// A node is a signal (fn === null), a computed, or a watcher (WATCH). An edge\n// is one relation in two doubly linked lists: `to.deps` in the order `to`\n// last read its sources (prev/next) and `from.subs` in birth order\n// (prevSub/nextSub). Change is a version fact: a node bumps `ver` when its\n// value changes; an edge keeps the `ver` its reader saw.\n//\n// Written for V8: one node shape and one edge shape, each built at a single\n// site; numeric fields are SMI counters (masked); conditions are explicit\n// comparisons, so no ToBoolean runs on mixed values; no recursion, no array\n// methods, and no allocation besides nodes and new edges.\n\ndeclare const __STATS__: boolean;\n\n/** A source below may have changed. For a watcher: validate before running. */\nexport const STALE = 1;\n/** Must run: first run, failed run, or a direct source changed. */\nexport const DIRTY = 2;\n/** Its run is on the stack. */\nexport const BUSY = 4;\n/** Its run put its edges into the sources' slots. */\nconst INDEX = 8;\n/** A watcher: handed to the host, never read. */\nexport const WATCH = 16;\n/** A watcher woken by signals only: its computed sources are clean, so no validation. */\nexport const WOKEN = 32;\n// Bits from 64 up belong to the host.\n\n/** edge.ver of an edge the indexed run has not read yet. */\nconst UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nconst MASK = 0x3fffffff;\n/** Scan budget before indexing: a constant keeps a run linear. */\nconst SCAN = 8;\n\nexport interface Node {\n flags: number;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: number;\n fn: ((prev: unknown) => unknown) | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\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: number;\n}\n\nexport interface Edge {\n from: Node;\n to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: number;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: number;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\nexport const stats = {\n reads: 0,\n hits: 0,\n repeats: 0,\n scans: 0,\n lookups: 0,\n dups: 0,\n moves: 0,\n links: 0,\n unlinks: 0,\n indexes: 0,\n indexed: 0,\n runs: 0,\n marks: 0,\n checks: 0,\n};\n\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.\n */\nconst stack: (Node | Edge | null)[] = [];\nlet top = 0;\nlet wake: (w: Node) => void = () => {};\n\n/** The host is told about every watcher that becomes STALE or WOKEN. */\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\nexport function node(fn: Node[\"fn\"], value: unknown, flags: number): Node {\n return { flags, value, ver: 0, fn, deps: null, last: null, subs: null, lastSub: null, slot: null, gen: 0 };\n}\n\n// ------------------------------------------------------------------- write\n\nexport function write(s: Node, value: unknown): void {\n if (Object.is(s.value, value)) return;\n s.value = value;\n s.ver = (s.ver + 1) & MASK;\n for (let e = s.subs; e !== null; e = e.nextSub) {\n const n = e.to;\n const f = n.flags;\n if (__STATS__) stats.marks++;\n if ((f & BUSY) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n if ((f & WATCH) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & WATCH) !== 0) {\n n.flags = f | DIRTY | WOKEN;\n if ((f & (STALE | WOKEN)) === 0) wake(n);\n } else if ((f & STALE) !== 0) {\n n.flags = f | DIRTY;\n } else {\n n.flags = f | DIRTY | STALE;\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 for (;;) {\n while (e !== null) {\n const n: Node = e.to;\n const f = n.flags;\n if (__STATS__) stats.marks++;\n // A STALE node has STALE subscribers, or is a watcher the host has.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n if ((f & WATCH) !== 0) {\n if ((f & WOKEN) === 0) wake(n);\n } else if (n.subs !== null) {\n if (e.nextSub !== null) stack[top++] = e.nextSub;\n e = n.subs;\n continue;\n }\n }\n e = e.nextSub;\n }\n if (top === base) return;\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 if ((n.flags & (STALE | DIRTY | BUSY)) !== 0) pull(n);\n const c = active;\n if (c !== null) {\n if (__STATS__) stats.reads++;\n const last = c.last;\n const next = last === null ? c.deps : last.next;\n if (next !== null && next.from === n) {\n if (__STATS__) stats.hits++;\n c.last = next;\n next.ver = n.ver;\n } else miss(c, n, last, next);\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 still checking DIRTY.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n if ((f & BUSY) !== 0) throw new Error(\"Cycle detected\");\n if ((f & DIRTY) !== 0) return exec(root);\n root.flags = f & ~STALE;\n const base = top;\n let n = root;\n let e = root.deps;\n try {\n for (;;) {\n let changed = false;\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n if (__STATS__) stats.checks++;\n if ((sf & (STALE | DIRTY)) !== 0) {\n if ((sf & BUSY) !== 0) throw new Error(\"Cycle detected\");\n if ((sf & DIRTY) === 0) {\n s.flags = sf & ~STALE;\n stack[top++] = e;\n n = s;\n e = s.deps;\n continue;\n }\n exec(s);\n }\n if (s.ver !== e.ver) {\n changed = true;\n break;\n }\n e = e.next;\n }\n if (changed) exec(n);\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) return;\n const up = stack[--top] as Edge;\n stack[top] = null;\n n = up.to;\n if (up.from.ver === up.ver) {\n e = up.next;\n break;\n }\n exec(n);\n }\n }\n } catch (error) {\n n.flags |= DIRTY;\n while (top > base) {\n (stack[--top] as Edge).to.flags |= 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 owed = (w.flags & DIRTY) !== 0;\n for (let e = w.deps; e !== null; e = e.next) {\n const s = e.from;\n if (__STATS__) stats.checks++;\n if ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) pull(s);\n if (!owed && s.ver !== e.ver) {\n owed = true;\n w.flags |= DIRTY;\n }\n }\n}\n\nexport function untracked<T>(fn: () => T): T {\n const prev = active;\n active = null;\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 if (__STATS__) stats.runs++;\n // Cleared before the run: a push during it marks the node again.\n n.flags = (n.flags & ~(STALE | DIRTY)) | BUSY;\n n.last = null;\n active = n;\n let value: unknown;\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 if ((f & INDEX) !== 0) unindex(n, base);\n n.flags = f & ~(BUSY | INDEX);\n if (n.fn === null) return detach(n);\n // Commit: drop the unread suffix, close the generation, publish the value.\n const last = n.last as Edge | null;\n if ((last === null ? n.deps : last.next) !== null) cut(n, last);\n n.gen = (n.gen + 1) & MASK;\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 & INDEX) !== 0) unindex(n, base);\n n.flags &= ~(BUSY | INDEX);\n if (n.fn === null) return detach(n);\n n.flags |= DIRTY;\n const tentative = (n.gen + 1) & MASK;\n const last = n.last;\n for (let e = last === null ? n.deps : last.next; e !== null; ) {\n const after: Edge | null = e.next;\n if (e.gen === tentative) {\n const before = e.prev;\n if (before !== null) before.next = after;\n else n.deps = after;\n if (after !== null) after.prev = before;\n unsub(e);\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.fn = null;\n if ((n.flags & BUSY) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(STALE | DIRTY | WOKEN);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** A read that is not the next edge in read order. Every answer is exact. */\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 last.ver = s.ver;\n return;\n }\n if ((c.flags & INDEX) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next, false);\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 while (e !== null && e.from !== s && --budget !== 0) e = e.next;\n if (e !== null && e.from === s) {\n if (__STATS__) stats.scans++;\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 while (p !== null && p.from !== s && --budget !== 0) p = p.prev;\n if (p !== null && p.from === s) {\n if (__STATS__) stats.dups++;\n p.ver = s.ver;\n return;\n }\n if ((e === null && p === null) || absent(s, c) === true) return link(c, s, last, next, false);\n index(c, next);\n }\n if (__STATS__) stats.lookups++;\n const cell = s.slot;\n if (cell !== null && cell.to === c) {\n if (cell.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n cell.ver = s.ver;\n return;\n }\n if (__STATS__) stats.moves++;\n cell.ver = s.ver;\n return move(c, cell, last, next!);\n }\n link(c, s, last, next, true);\n}\n\n/** No edge to `c` among the source's few subscribers. */\nfunction absent(s: Node, c: Node): boolean {\n let budget = SCAN;\n for (let e = s.subs; e !== null; e = e.nextSub) if (e.to === c || --budget === 0) return false;\n return true;\n}\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nfunction index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n c.flags |= INDEX;\n let unread = false;\n for (let e = c.deps; e !== null; e = e.next) {\n if (__STATS__) stats.indexed++;\n if (e === next) unread = true;\n if (unread) e.ver = UNREAD;\n const s = e.from;\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\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 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/** Move an unread edge (strictly after `next`) up to the cursor. */\nfunction 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 if (after !== null) after.prev = before;\n e.prev = last;\n e.next = next;\n if (last !== null) last.next = e;\n else c.deps = e;\n next.prev = e;\n c.last = e;\n}\n\n/** A new edge at the cursor, appended to the source's subscribers. */\nfunction link(c: Node, s: Node, last: Edge | null, next: Edge | null, indexed: boolean): void {\n if (__STATS__) stats.links++;\n const tail = s.lastSub;\n const e: Edge = { from: s, to: c, ver: s.ver, gen: (c.gen + 1) & MASK, prev: last, next, prevSub: tail, nextSub: null };\n if (last !== null) last.next = e;\n else c.deps = e;\n if (next !== null) next.prev = e;\n if (tail !== null) tail.nextSub = e;\n else s.subs = e;\n s.lastSub = e;\n c.last = e;\n if (indexed) {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nfunction cut(c: Node, last: Edge | null): void {\n let e = last === null ? c.deps : last.next;\n if (e === null) return;\n if (last === null) c.deps = null;\n else last.next = null;\n for (; e !== null; e = e.next) unsub(e);\n}\n\n/** Take an edge out of its source's subscriber list. */\nfunction unsub(e: Edge): void {\n if (__STATS__) stats.unlinks++;\n const s = e.from;\n const before = e.prevSub;\n const after = e.nextSub;\n if (before !== null) before.nextSub = after;\n else s.subs = after;\n if (after !== null) after.prevSub = before;\n else s.lastSub = before;\n}\n", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport { DIRTY, STALE, WATCH, WOKEN, check, dispose, exec, node, onWake, untracked, type Node } from \"./kernel\";\n\n/** Run by this host's queue; other watchers are run by their owner. */\nconst AUTO = 64;\nconst QUEUED = 128;\n/** Its check threw: run() checks again although no push marked it (V1). */\nconst RETRY = 256;\n\ntype Fn = () => unknown;\n\nconst queue: (Node | null)[] = [];\nlet size = 0;\nlet depth = 0;\nlet 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 & (AUTO | QUEUED)) !== AUTO) return;\n w.flags |= 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(fn, undefined, WATCH | STALE | DIRTY | (auto ? AUTO : 0));\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 nor RETRY is clean, or cold after a failure until a source changes.\n * Precondition: its own run is not on the stack (flush is not reentrant).\n */\nexport function run(w: Node): void {\n const f = w.flags;\n if (w.fn === null || (f & (STALE | WOKEN | RETRY)) === 0) return;\n w.flags = f & ~(STALE | WOKEN | RETRY);\n if ((f & (STALE | RETRY)) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= RETRY;\n throw error;\n }\n }\n if ((w.flags & DIRTY) === 0 || w.fn === null) return;\n const cleanup = w.value;\n if (typeof cleanup === \"function\") {\n w.value = undefined;\n untracked(cleanup as Fn);\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 const cleanup = w.value;\n w.value = undefined;\n dispose(w);\n if (typeof cleanup === \"function\") untracked(cleanup as Fn);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (draining || depth !== 0 || size === 0) return;\n draining = true;\n let failed = false;\n let first: unknown;\n for (let i = 0; i < size; i++) {\n const w = queue[i]!;\n queue[i] = null;\n w.flags &= ~QUEUED;\n try {\n run(w);\n } catch (error) {\n if (!failed) first = error;\n failed = true;\n }\n }\n size = 0;\n draining = false;\n if (failed) throw first;\n}\n\nexport function batch<T>(fn: () => T): T {\n depth++;\n try {\n return fn();\n } finally {\n if (--depth === 0) flush();\n }\n}\n"],
|
|
5
|
-
"mappings": ";yaAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,aAAAC,GAAA,YAAAC,GAAA,WAAAC,GAAA,QAAAC,GAAA,QAAAC,GAAA,WAAAC,GAAA,cAAAC,IAAA,eAAAC,EAAAV,
|
|
6
|
-
"names": ["index_exports", "__export", "batch", "computed", "dispose", "effect", "get", "set", "signal", "untracked", "__toCommonJS", "active", "stack", "top", "wake", "onWake", "fn", "node", "fn", "value", "flags", "write", "s", "e", "n", "f", "wake", "mark", "first", "base", "top", "stack", "read", "pull", "c", "active", "last", "next", "miss", "root", "exec", "
|
|
4
|
+
"sourcesContent": ["// Public API: signal, computed, effect, get, set, dispose, batch, untracked.\n// A handle is the node itself: nothing is allocated besides the node.\nimport {\n DIRTY,\n WATCH,\n dispose as detach,\n node,\n read,\n write,\n type Node,\n} from \"./kernel\";\nimport { flush, start, stop, watcher } from \"./host\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Compile-time brands only. They do not exist at runtime.\ndeclare const SIGNAL: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\nexport interface Signal<T> {\n readonly [SIGNAL]: T;\n}\n\nexport interface Computed<T> {\n readonly [COMPUTED]: T;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport type Readable<T> = Signal<T> | Computed<T>;\n\nconst raw = (handle: unknown): Node => handle as Node;\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T> {\n return node(fn as Node[\"fn\"], undefined, 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 start(effectNode);\n return effectNode as unknown as Effect;\n}\n\n/** A signal's flags are 0, so one read path serves signals and computeds. */\nexport function get<T>(source: Readable<T>): T {\n return read(raw(source)) as T;\n}\n\nexport function set<T>(source: Signal<T>, value: T): void {\n write(raw(source), value);\n flush();\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const targetNode = raw(target);\n\n if ((targetNode.flags & WATCH) !== 0) {\n stop(targetNode);\n } else {\n detach(targetNode);\n }\n}\n", "// Reactive kernel: nodes, edges, tracking, push, pull, failure.\n//\n// A node is a signal (fn === null), a computed, or a watcher (WATCH). An edge\n// is one relation in two doubly linked lists: `to.deps` in the order `to`\n// last read its sources (prev/next) and `from.subs` in birth order\n// (prevSub/nextSub). Change is a version fact: a node bumps `ver` when its\n// value changes; an edge keeps the `ver` its reader saw.\n//\n// Removal invariant: an edge taken out of a list must not keep a pointer into\n// either list, because garbage that points at live or younger edges is kept\n// alive by the scavenger once it is old (an old dead edge pins newer ones, and\n// through `to` their watchers and closures, until a major GC). Removal is\n// therefore two steps: unlink the neighbours, then clear the edge's own\n// links. `unsub` clears prevSub/nextSub; `cut` clears the head's `prev`, and\n// `fail` clears `prev`/`next`. In `cut` the interior of a removed run keeps\n// its `next` chain, which only reaches other garbage of the same cut and\n// is read by `unsub` while the run is walked. Any new removal path must\n// do the same.\n//\n// Written for V8: one node shape and one edge shape, each built at a single\n// site; numeric fields are SMI counters (masked); conditions are explicit\n// comparisons, so no ToBoolean runs on mixed values; no recursion, no array\n// methods, and no allocation besides nodes and new edges.\n\ndeclare const __STATS__: boolean;\n\n/** A source below may have changed. For a watcher: validate before running. */\nexport const STALE = 1;\n/** Must run: first run, failed run, or a direct source changed. */\nexport const DIRTY = 2;\n/** Its run is on the stack. */\nexport const BUSY = 4;\n/** Its run put its edges into the sources' slots. */\nconst INDEX = 8;\n/** A watcher: handed to the host, never read. */\nexport const WATCH = 16;\n/** A watcher woken by signals only: its computed sources are clean, so no validation. */\nexport const WOKEN = 32;\n// Bits from 64 up belong to the host.\n\n/** edge.ver of an edge the indexed run has not read yet. */\nconst UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nconst MASK = 0x3fffffff;\n/** Scan budget before indexing: a constant keeps a run linear. */\nconst SCAN = 8;\n\nexport interface Node {\n flags: number;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: number;\n fn: ((prev: unknown) => unknown) | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\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: number;\n}\n\nexport interface Edge {\n from: Node;\n to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: number;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: number;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\nexport const stats = {\n reads: 0,\n hits: 0,\n repeats: 0,\n scans: 0,\n lookups: 0,\n dups: 0,\n moves: 0,\n links: 0,\n unlinks: 0,\n indexes: 0,\n indexed: 0,\n runs: 0,\n marks: 0,\n checks: 0,\n};\n\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.\n */\nconst stack: (Node | Edge | null)[] = [];\nlet top = 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\nexport function node(fn: Node[\"fn\"], value: unknown, flags: number): 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// ------------------------------------------------------------------- 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 & BUSY) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n\n if ((f & WATCH) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & WATCH) !== 0) {\n n.flags = f | DIRTY | WOKEN;\n\n wake(n);\n } else if ((f & STALE) !== 0) {\n n.flags = f | DIRTY;\n } else {\n n.flags = f | DIRTY | 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 & WATCH) !== 0) {\n n.flags = f | STALE;\n wake(n);\n } else if ((f & STALE) === 0) {\n n.flags = f | 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 if ((n.flags & (STALE | DIRTY | BUSY)) !== 0) pull(n);\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 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 still checking DIRTY.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n\n if ((f & BUSY) !== 0) throw new Error(\"Cycle detected\");\n\n if ((f & DIRTY) !== 0) return exec(root);\n\n verify(root, f);\n}\n\n/** The walk of `pull` for a STALE, not DIRTY, not BUSY node; kept apart so `pull` stays small. */\nfunction verify(root: Node, f: number): void {\n root.flags = f & ~STALE;\n const base = top;\n let n = root;\n let e = root.deps;\n\n try {\n for (;;) {\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n\n if (__STATS__) stats.checks++;\n\n if ((sf & (STALE | DIRTY)) !== 0) {\n if ((sf & BUSY) !== 0) throw new Error(\"Cycle detected\");\n\n if ((sf & DIRTY) === 0) {\n s.flags = sf & ~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 exec(n);\n break;\n }\n e = e.next;\n }\n\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) return;\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 exec(n);\n }\n }\n } catch (error) {\n n.flags |= DIRTY;\n\n while (top > base) {\n (stack[--top] as Edge).to.flags |= 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.\n if ((w.flags & DIRTY) === 0) {\n for (; e !== null; e = e.next) {\n const s = e.from;\n\n if (__STATS__) stats.checks++;\n\n if ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) pull(s);\n\n if (s.ver !== e.ver) {\n w.flags |= 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 ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) 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.\n n.flags = (n.flags & ~(STALE | DIRTY)) | 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 & INDEX) !== 0) unindex(n, base);\n\n n.flags = f & ~(BUSY | INDEX);\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 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 & INDEX) !== 0) unindex(n, base);\n\n n.flags &= ~(BUSY | INDEX);\n\n if (n.fn === null) return detach(n);\n\n n.flags |= 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 & BUSY) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(STALE | DIRTY | WOKEN);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** A read that is not the next edge in read order. Every answer is exact. */\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 last.ver = s.ver;\n return;\n }\n\n if ((c.flags & INDEX) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next, false);\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 p.ver = s.ver;\n return;\n }\n\n if ((e === null && p === null) || absent(s, c) === true)\n return link(c, s, last, next, false);\n index(c, next);\n }\n\n if (__STATS__) stats.lookups++;\n\n const cell = s.slot;\n\n if (cell !== null && cell.to === c) {\n if (cell.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n\n cell.ver = s.ver;\n return;\n }\n\n if (__STATS__) stats.moves++;\n\n cell.ver = s.ver;\n return move(c, cell, last, next!);\n }\n link(c, s, last, next, true);\n}\n\n/** No edge to `c` among the source's few subscribers. */\nfunction absent(s: Node, c: Node): boolean {\n let budget = SCAN;\n\n for (let e = s.subs; e !== null; e = e.nextSub)\n if (e.to === c || --budget === 0) return false;\n\n return true;\n}\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nfunction index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n\n c.flags |= INDEX;\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 const s = e.from;\n\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\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/** Move an unread edge (strictly after `next`) up to the cursor. */\nfunction 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/** A new edge at the cursor, appended to the source's subscribers. */\nfunction link(\n c: Node,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n indexed: boolean,\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 if (indexed) {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nfunction 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/** Take an edge out of its source's subscriber list. */\nfunction 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", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport {\n DIRTY,\n STALE,\n WATCH,\n WOKEN,\n check,\n dispose,\n exec,\n node,\n onWake,\n untracked,\n type Node,\n} from \"./kernel\";\n\n/** Run by this host's queue; other watchers are run by their owner. */\nconst AUTO = 64;\nconst QUEUED = 128;\n\ntype Fn = () => unknown;\n\nconst queue: (Node | null)[] = [];\nlet size = 0;\nlet depth = 0;\nlet 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 & (AUTO | QUEUED)) !== AUTO) return;\n\n w.flags |= 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(fn, undefined, WATCH | STALE | DIRTY | (auto ? AUTO : 0));\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 & (STALE | WOKEN)) === 0) return;\n\n w.flags = flags & ~(STALE | WOKEN);\n\n if ((flags & STALE) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= STALE;\n throw error;\n }\n }\n\n if ((w.flags & 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 Fn);\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 Fn);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (draining || depth !== 0 || size === 0) return;\n\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 &= ~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": ";yaAAA,IAAAA,GAAA,GAAAC,EAAAD,GAAA,WAAAE,EAAA,aAAAC,GAAA,YAAAC,GAAA,WAAAC,GAAA,QAAAC,GAAA,QAAAC,GAAA,WAAAC,GAAA,cAAAC,IAAA,eAAAC,EAAAV,IC+FA,IAAIW,EAAsB,KAMpBC,EAAgC,CAAC,EACnCC,EAAM,EACNC,EAA0B,IAAM,CAAC,EAO9B,SAASC,EAAOC,EAA6B,CAClDF,EAAOE,CACT,CAOO,SAASC,EAAKC,EAAgBC,EAAgBC,EAAqB,CACxE,MAAO,CACL,MAAAA,EACA,MAAAD,EACA,IAAK,EACL,GAAAD,EACA,KAAM,KACN,KAAM,KACN,KAAM,KACN,QAAS,KACT,KAAM,KACN,IAAK,CACP,CACF,CAIO,SAASG,EAAMC,EAASH,EAAsB,CACnD,GAAI,QAAO,GAAGG,EAAE,MAAOH,CAAK,EAE5B,CAAAG,EAAE,MAAQH,EACVG,EAAE,IAAOA,EAAE,IAAM,EAAK,WAEtB,QAASC,EAAID,EAAE,KAAMC,IAAM,KAAMA,EAAIA,EAAE,QAAS,CAC9C,IAAMC,EAAID,EAAE,GACNE,EAAID,EAAE,OAIPC,EAAI,KAAU,GAEZA,EAAI,KAAW,IAClBD,EAAE,MAAQC,EAAI,GAETA,EAAI,MAAW,EAAGC,EAAKF,CAAC,EACpBA,EAAE,OAAS,MAAMG,EAAKH,EAAE,IAAI,IAE7BC,EAAI,MAAW,GACzBD,EAAE,MAAQC,EAAI,EAAQ,GAEtBC,EAAKF,CAAC,IACIC,EAAI,K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AEfF,EAAOE,CAAU,CAErB",
|
|
6
|
+
"names": ["index_exports", "__export", "batch", "computed", "dispose", "effect", "get", "set", "signal", "untracked", "__toCommonJS", "active", "stack", "top", "wake", "onWake", "fn", "node", "fn", "value", "flags", "write", "s", "e", "n", "f", "wake", "mark", "first", "base", "top", "stack", "read", "pull", "c", "active", "last", "next", "miss", "root", "exec", "verify", "sf", "up", "error", "check", "w", "tentative", "untracked", "prev", "fail", "unindex", "detach", "cut", "after", "before", "unsub", "dispose", "link", "budget", "move", "p", "absent", "index", "cell", "unread", "indexed", "tail", "AUTO", "QUEUED", "queue", "size", "depth", "draining", "wake", "w", "onWake", "watcher", "fn", "auto", "node", "run", "flags", "check", "error", "cleanup", "untracked", "exec", "stop", "dispose", "flush", "drain", "start", "failed", "first", "i", "queuedWatcher", "batch", "raw", "handle", "signal", "value", "node", "computed", "fn", "effect", "effectNode", "watcher", "start", "get", "source", "read", "set", "write", "flush", "dispose", "target", "targetNode", "stop"]
|
|
7
7
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* reflex-quant
|
|
2
|
-
var d=null,f=[],s=0,
|
|
1
|
+
/* reflex-quant 1.0.0 | MIT */
|
|
2
|
+
var d=null,f=[],s=0,v=()=>{};function L(e){v=e}function c(e,n,t){return{flags:t,value:n,ver:0,fn:e,deps:null,last:null,subs:null,lastSub:null,slot:null,gen:0}}function w(e,n){if(!Object.is(e.value,n)){e.value=n,e.ver=e.ver+1&1073741823;for(let t=e.subs;t!==null;t=t.nextSub){let l=t.to,o=l.flags;(o&4)!==0?(o&1)===0&&(l.flags=o|1,(o&16)!==0?v(l):l.subs!==null&&R(l.subs)):(o&16)!==0?(l.flags=o|2|32,v(l)):(o&1)!==0?l.flags=o|2:(l.flags=o|2|1,l.subs!==null&&R(l.subs))}}}function R(e){let n=s,t=e;for(;;){for(;t!==null;){let l=t.to,o=l.flags;if((o&16)!==0)l.flags=o|1,v(l);else if((o&1)===0&&(l.flags=o|1,l.subs!==null)){t.nextSub!==null&&(f[s++]=t.nextSub),t=l.subs;continue}t=t.nextSub}if(s===n)return;t=f[--s],f[s]=null}}function U(e){(e.flags&7)!==0&&A(e);let n=d;if(n!==null){let t=n.last,l=t===null?n.deps:t.next;l!==null&&l.from===e?(n.last=l,l.ver=e.ver):P(n,e,t,l)}return e.value}function A(e){let n=e.flags;if((n&4)!==0)throw new Error("Cycle detected");if((n&2)!==0)return a(e);j(e,n)}function j(e,n){e.flags=n&-2;let t=s,l=e,o=e.deps;try{for(;;){for(;o!==null;){let u=o.from,r=u.flags;if((r&3)!==0){if((r&4)!==0)throw new Error("Cycle detected");if((r&2)===0){u.flags=r&-2,f[s++]=o,l=u,o=u.deps;continue}a(u)}if(u.ver!==o.ver){a(l);break}o=o.next}for(;;){if(s===t)return;let u=f[--s];if(f[s]=null,l=u.to,u.from.ver===u.ver){o=u.next;break}a(l)}}}catch(u){for(l.flags|=2;s>t;)f[--s].to.flags|=2,f[s]=null;throw u}}function W(e){let n=e.gen+1&1073741823,t=e.deps;if((e.flags&2)===0)for(;t!==null;t=t.next){let l=t.from;if((l.flags&3)!==0&&t.gen!==n&&A(l),l.ver!==t.ver){e.flags|=2,t=t.next;break}}for(;t!==null;t=t.next){let l=t.from;(l.flags&3)!==0&&t.gen!==n&&A(l)}}function g(e){let n=d;d=null;try{return e()}finally{d=n}}function a(e){let n=d,t=s;e.flags=e.flags&-4|4,e.last=null,d=e;let l;try{l=e.fn(e.value)}catch(r){throw d=n,G(e,t),r}d=n;let o=e.flags;if((o&8)!==0&&K(e,t),e.flags=o&-13,e.fn===null)return m(e);let u=e.last;(u===null?e.deps:u.next)!==null&&B(e,u),e.gen=e.gen+1&1073741823,Object.is(l,e.value)===!1&&(e.value=l,e.ver=e.ver+1&1073741823)}function G(e,n){if((e.flags&8)!==0&&K(e,n),e.flags&=-13,e.fn===null)return m(e);e.flags|=2;let t=e.gen+1&1073741823,l=e.last;for(let o=l===null?e.deps:l.next;o!==null;){let u=o.next;if(o.gen===t){let r=o.prev;r!==null?r.next=u:e.deps=u,u!==null&&(u.prev=r),M(o),o.prev=null,o.next=null}o=u}}function _(e){e.fn!==null&&(e.fn=null,(e.flags&4)===0&&m(e))}function m(e){B(e,null),e.flags&=-36}function P(e,n,t,l){if(t!==null&&t.from===n){t.ver=n.ver;return}if((e.flags&8)===0){if(n.subs===null)return N(e,n,t,l,!1);let u=l!==null?l.next:null,r=8;for(;u!==null&&u.from!==n&&--r!==0;)u=u.next;if(u!==null&&u.from===n)return u.ver=n.ver,C(e,u,t,l);let i=t!==null?t.prev:null;for(r=8;i!==null&&i.from!==n&&--r!==0;)i=i.prev;if(i!==null&&i.from===n){i.ver=n.ver;return}if(u===null&&i===null||z(n,e)===!0)return N(e,n,t,l,!1);Q(e,l)}let o=n.slot;if(o!==null&&o.to===e){if(o.ver!==-1){o.ver=n.ver;return}return o.ver=n.ver,C(e,o,t,l)}N(e,n,t,l,!0)}function z(e,n){let t=8;for(let l=e.subs;l!==null;l=l.nextSub)if(l.to===n||--t===0)return!1;return!0}function Q(e,n){e.flags|=8;let t=!1;for(let l=e.deps;l!==null;l=l.next){l===n&&(t=!0),t&&(l.ver=-1);let o=l.from;o.slot!==null&&(f[s++]=o,f[s++]=o.slot),o.slot=l}}function K(e,n){for(let t=e.deps;t!==null;t=t.next)t.from.slot=null;for(;s>n;){let t=f[--s];f[s]=null;let l=f[--s];f[s]=null,l.slot=t}}function C(e,n,t,l){let o=n.prev,u=n.next;o.next=u,u!==null&&(u.prev=o),n.prev=t,n.next=l,t!==null?t.next=n:e.deps=n,l.prev=n,e.last=n}function N(e,n,t,l,o){let u=n.lastSub,r={from:n,to:e,ver:n.ver,gen:e.gen+1&1073741823,prev:t,next:l,prevSub:u,nextSub:null};t!==null?t.next=r:e.deps=r,l!==null&&(l.prev=r),u!==null?u.nextSub=r:n.subs=r,n.lastSub=r,e.last=r,o&&(n.slot!==null&&(f[s++]=n,f[s++]=n.slot),n.slot=r)}function B(e,n){let t=n===null?e.deps:n.next;if(t!==null)for(n===null?e.deps=null:n.next=null,t.prev=null;t!==null;t=t.next)M(t)}function M(e){let n=e.from,t=e.prevSub,l=e.nextSub;t!==null?t.nextSub=l:n.subs=l,l!==null?l.prevSub=t:n.lastSub=t,e.prevSub=null,e.nextSub=null}var k=64,h=128,D=[],p=0,E=0,S=!1;function F(e){(e.flags&(k|h))===k&&(e.flags|=h,D[p++]=e)}L(F);function X(e,n){return c(e,void 0,19|(n?k:0))}function J(e){let n=e.flags;if(e.fn===null||(n&33)===0)return;if(e.flags=n&-34,(n&1)!==0)try{W(e)}catch(l){throw e.flags|=1,l}if((e.flags&2)===0||e.fn===null)return;let t=e.value;typeof t=="function"&&(e.value=void 0,g(t),e.fn===null)||a(e)}function H(e){if(e.fn===null)return;let n=e.value;e.value=void 0,_(e),typeof n=="function"&&g(n)}function x(){S||E!==0||p===0||(S=!0,I(!1,void 0))}function q(e){if(S||E!==0||p!==0)return F(e),x();S=!0;try{a(e)}catch(n){return I(!0,n)}I(!1,void 0)}function I(e,n){for(let t=0;t<p;t++){let l=D[t];D[t]=null,l.flags&=~h;try{J(l)}catch(o){e||(n=o),e=!0}}if(p=0,S=!1,e)throw n}function V(e){E++;try{return e()}finally{--E===0&&x()}}var y=e=>e;function le(e){return c(null,e,0)}function oe(e){return c(e,void 0,2)}function ue(e){let n=X(e,!0);return q(n),n}function re(e){return U(y(e))}function se(e,n){w(y(e),n),x()}function fe(e){let n=y(e);(n.flags&16)!==0?H(n):_(n)}export{V as batch,oe as computed,fe as dispose,ue as effect,re as get,se as set,le as signal,g as untracked};
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/kernel.ts", "../src/host.ts", "../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["// Reactive kernel: nodes, edges, tracking, push, pull, failure.\n//\n// A node is a signal (fn === null), a computed, or a watcher (WATCH). An edge\n// is one relation in two doubly linked lists: `to.deps` in the order `to`\n// last read its sources (prev/next) and `from.subs` in birth order\n// (prevSub/nextSub). Change is a version fact: a node bumps `ver` when its\n// value changes; an edge keeps the `ver` its reader saw.\n//\n// Written for V8: one node shape and one edge shape, each built at a single\n// site; numeric fields are SMI counters (masked); conditions are explicit\n// comparisons, so no ToBoolean runs on mixed values; no recursion, no array\n// methods, and no allocation besides nodes and new edges.\n\ndeclare const __STATS__: boolean;\n\n/** A source below may have changed. For a watcher: validate before running. */\nexport const STALE = 1;\n/** Must run: first run, failed run, or a direct source changed. */\nexport const DIRTY = 2;\n/** Its run is on the stack. */\nexport const BUSY = 4;\n/** Its run put its edges into the sources' slots. */\nconst INDEX = 8;\n/** A watcher: handed to the host, never read. */\nexport const WATCH = 16;\n/** A watcher woken by signals only: its computed sources are clean, so no validation. */\nexport const WOKEN = 32;\n// Bits from 64 up belong to the host.\n\n/** edge.ver of an edge the indexed run has not read yet. */\nconst UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nconst MASK = 0x3fffffff;\n/** Scan budget before indexing: a constant keeps a run linear. */\nconst SCAN = 8;\n\nexport interface Node {\n flags: number;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: number;\n fn: ((prev: unknown) => unknown) | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\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: number;\n}\n\nexport interface Edge {\n from: Node;\n to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: number;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: number;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\nexport const stats = {\n reads: 0,\n hits: 0,\n repeats: 0,\n scans: 0,\n lookups: 0,\n dups: 0,\n moves: 0,\n links: 0,\n unlinks: 0,\n indexes: 0,\n indexed: 0,\n runs: 0,\n marks: 0,\n checks: 0,\n};\n\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.\n */\nconst stack: (Node | Edge | null)[] = [];\nlet top = 0;\nlet wake: (w: Node) => void = () => {};\n\n/** The host is told about every watcher that becomes STALE or WOKEN. */\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\nexport function node(fn: Node[\"fn\"], value: unknown, flags: number): Node {\n return { flags, value, ver: 0, fn, deps: null, last: null, subs: null, lastSub: null, slot: null, gen: 0 };\n}\n\n// ------------------------------------------------------------------- write\n\nexport function write(s: Node, value: unknown): void {\n if (Object.is(s.value, value)) return;\n s.value = value;\n s.ver = (s.ver + 1) & MASK;\n for (let e = s.subs; e !== null; e = e.nextSub) {\n const n = e.to;\n const f = n.flags;\n if (__STATS__) stats.marks++;\n if ((f & BUSY) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n if ((f & WATCH) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & WATCH) !== 0) {\n n.flags = f | DIRTY | WOKEN;\n if ((f & (STALE | WOKEN)) === 0) wake(n);\n } else if ((f & STALE) !== 0) {\n n.flags = f | DIRTY;\n } else {\n n.flags = f | DIRTY | STALE;\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 for (;;) {\n while (e !== null) {\n const n: Node = e.to;\n const f = n.flags;\n if (__STATS__) stats.marks++;\n // A STALE node has STALE subscribers, or is a watcher the host has.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n if ((f & WATCH) !== 0) {\n if ((f & WOKEN) === 0) wake(n);\n } else if (n.subs !== null) {\n if (e.nextSub !== null) stack[top++] = e.nextSub;\n e = n.subs;\n continue;\n }\n }\n e = e.nextSub;\n }\n if (top === base) return;\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 if ((n.flags & (STALE | DIRTY | BUSY)) !== 0) pull(n);\n const c = active;\n if (c !== null) {\n if (__STATS__) stats.reads++;\n const last = c.last;\n const next = last === null ? c.deps : last.next;\n if (next !== null && next.from === n) {\n if (__STATS__) stats.hits++;\n c.last = next;\n next.ver = n.ver;\n } else miss(c, n, last, next);\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 still checking DIRTY.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n if ((f & BUSY) !== 0) throw new Error(\"Cycle detected\");\n if ((f & DIRTY) !== 0) return exec(root);\n root.flags = f & ~STALE;\n const base = top;\n let n = root;\n let e = root.deps;\n try {\n for (;;) {\n let changed = false;\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n if (__STATS__) stats.checks++;\n if ((sf & (STALE | DIRTY)) !== 0) {\n if ((sf & BUSY) !== 0) throw new Error(\"Cycle detected\");\n if ((sf & DIRTY) === 0) {\n s.flags = sf & ~STALE;\n stack[top++] = e;\n n = s;\n e = s.deps;\n continue;\n }\n exec(s);\n }\n if (s.ver !== e.ver) {\n changed = true;\n break;\n }\n e = e.next;\n }\n if (changed) exec(n);\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) return;\n const up = stack[--top] as Edge;\n stack[top] = null;\n n = up.to;\n if (up.from.ver === up.ver) {\n e = up.next;\n break;\n }\n exec(n);\n }\n }\n } catch (error) {\n n.flags |= DIRTY;\n while (top > base) {\n (stack[--top] as Edge).to.flags |= 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 owed = (w.flags & DIRTY) !== 0;\n for (let e = w.deps; e !== null; e = e.next) {\n const s = e.from;\n if (__STATS__) stats.checks++;\n if ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) pull(s);\n if (!owed && s.ver !== e.ver) {\n owed = true;\n w.flags |= DIRTY;\n }\n }\n}\n\nexport function untracked<T>(fn: () => T): T {\n const prev = active;\n active = null;\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 if (__STATS__) stats.runs++;\n // Cleared before the run: a push during it marks the node again.\n n.flags = (n.flags & ~(STALE | DIRTY)) | BUSY;\n n.last = null;\n active = n;\n let value: unknown;\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 if ((f & INDEX) !== 0) unindex(n, base);\n n.flags = f & ~(BUSY | INDEX);\n if (n.fn === null) return detach(n);\n // Commit: drop the unread suffix, close the generation, publish the value.\n const last = n.last as Edge | null;\n if ((last === null ? n.deps : last.next) !== null) cut(n, last);\n n.gen = (n.gen + 1) & MASK;\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 & INDEX) !== 0) unindex(n, base);\n n.flags &= ~(BUSY | INDEX);\n if (n.fn === null) return detach(n);\n n.flags |= DIRTY;\n const tentative = (n.gen + 1) & MASK;\n const last = n.last;\n for (let e = last === null ? n.deps : last.next; e !== null; ) {\n const after: Edge | null = e.next;\n if (e.gen === tentative) {\n const before = e.prev;\n if (before !== null) before.next = after;\n else n.deps = after;\n if (after !== null) after.prev = before;\n unsub(e);\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.fn = null;\n if ((n.flags & BUSY) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(STALE | DIRTY | WOKEN);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** A read that is not the next edge in read order. Every answer is exact. */\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 last.ver = s.ver;\n return;\n }\n if ((c.flags & INDEX) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next, false);\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 while (e !== null && e.from !== s && --budget !== 0) e = e.next;\n if (e !== null && e.from === s) {\n if (__STATS__) stats.scans++;\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 while (p !== null && p.from !== s && --budget !== 0) p = p.prev;\n if (p !== null && p.from === s) {\n if (__STATS__) stats.dups++;\n p.ver = s.ver;\n return;\n }\n if ((e === null && p === null) || absent(s, c) === true) return link(c, s, last, next, false);\n index(c, next);\n }\n if (__STATS__) stats.lookups++;\n const cell = s.slot;\n if (cell !== null && cell.to === c) {\n if (cell.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n cell.ver = s.ver;\n return;\n }\n if (__STATS__) stats.moves++;\n cell.ver = s.ver;\n return move(c, cell, last, next!);\n }\n link(c, s, last, next, true);\n}\n\n/** No edge to `c` among the source's few subscribers. */\nfunction absent(s: Node, c: Node): boolean {\n let budget = SCAN;\n for (let e = s.subs; e !== null; e = e.nextSub) if (e.to === c || --budget === 0) return false;\n return true;\n}\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nfunction index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n c.flags |= INDEX;\n let unread = false;\n for (let e = c.deps; e !== null; e = e.next) {\n if (__STATS__) stats.indexed++;\n if (e === next) unread = true;\n if (unread) e.ver = UNREAD;\n const s = e.from;\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\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 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/** Move an unread edge (strictly after `next`) up to the cursor. */\nfunction 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 if (after !== null) after.prev = before;\n e.prev = last;\n e.next = next;\n if (last !== null) last.next = e;\n else c.deps = e;\n next.prev = e;\n c.last = e;\n}\n\n/** A new edge at the cursor, appended to the source's subscribers. */\nfunction link(c: Node, s: Node, last: Edge | null, next: Edge | null, indexed: boolean): void {\n if (__STATS__) stats.links++;\n const tail = s.lastSub;\n const e: Edge = { from: s, to: c, ver: s.ver, gen: (c.gen + 1) & MASK, prev: last, next, prevSub: tail, nextSub: null };\n if (last !== null) last.next = e;\n else c.deps = e;\n if (next !== null) next.prev = e;\n if (tail !== null) tail.nextSub = e;\n else s.subs = e;\n s.lastSub = e;\n c.last = e;\n if (indexed) {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nfunction cut(c: Node, last: Edge | null): void {\n let e = last === null ? c.deps : last.next;\n if (e === null) return;\n if (last === null) c.deps = null;\n else last.next = null;\n for (; e !== null; e = e.next) unsub(e);\n}\n\n/** Take an edge out of its source's subscriber list. */\nfunction unsub(e: Edge): void {\n if (__STATS__) stats.unlinks++;\n const s = e.from;\n const before = e.prevSub;\n const after = e.nextSub;\n if (before !== null) before.nextSub = after;\n else s.subs = after;\n if (after !== null) after.prevSub = before;\n else s.lastSub = before;\n}\n", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport { DIRTY, STALE, WATCH, WOKEN, check, dispose, exec, node, onWake, untracked, type Node } from \"./kernel\";\n\n/** Run by this host's queue; other watchers are run by their owner. */\nconst AUTO = 64;\nconst QUEUED = 128;\n/** Its check threw: run() checks again although no push marked it (V1). */\nconst RETRY = 256;\n\ntype Fn = () => unknown;\n\nconst queue: (Node | null)[] = [];\nlet size = 0;\nlet depth = 0;\nlet 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 & (AUTO | QUEUED)) !== AUTO) return;\n w.flags |= 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(fn, undefined, WATCH | STALE | DIRTY | (auto ? AUTO : 0));\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 nor RETRY is clean, or cold after a failure until a source changes.\n * Precondition: its own run is not on the stack (flush is not reentrant).\n */\nexport function run(w: Node): void {\n const f = w.flags;\n if (w.fn === null || (f & (STALE | WOKEN | RETRY)) === 0) return;\n w.flags = f & ~(STALE | WOKEN | RETRY);\n if ((f & (STALE | RETRY)) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= RETRY;\n throw error;\n }\n }\n if ((w.flags & DIRTY) === 0 || w.fn === null) return;\n const cleanup = w.value;\n if (typeof cleanup === \"function\") {\n w.value = undefined;\n untracked(cleanup as Fn);\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 const cleanup = w.value;\n w.value = undefined;\n dispose(w);\n if (typeof cleanup === \"function\") untracked(cleanup as Fn);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (draining || depth !== 0 || size === 0) return;\n draining = true;\n let failed = false;\n let first: unknown;\n for (let i = 0; i < size; i++) {\n const w = queue[i]!;\n queue[i] = null;\n w.flags &= ~QUEUED;\n try {\n run(w);\n } catch (error) {\n if (!failed) first = error;\n failed = true;\n }\n }\n size = 0;\n draining = false;\n if (failed) throw first;\n}\n\nexport function batch<T>(fn: () => T): T {\n depth++;\n try {\n return fn();\n } finally {\n if (--depth === 0) flush();\n }\n}\n", "// Public API: signal, computed, effect, get, set, dispose, batch, untracked.\n// A handle is the node itself: nothing is allocated besides the node.\nimport { DIRTY, WATCH, dispose as detach, node, read, write, type Node } from \"./kernel\";\nimport { flush, stop, wake, watcher } from \"./host\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Compile-time brands only. They do not exist at runtime.\ndeclare const SIGNAL: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\nexport interface Signal<T> {\n readonly [SIGNAL]: T;\n}\n\nexport interface Computed<T> {\n readonly [COMPUTED]: T;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport type Readable<T> = Signal<T> | Computed<T>;\n\nconst raw = (handle: unknown): Node => handle as Node;\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T> {\n return node(fn as Node[\"fn\"], undefined, 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 w = watcher(fn, true);\n wake(w);\n flush();\n return w as unknown as Effect;\n}\n\n/** A signal's flags are 0, so one read path serves signals and computeds. */\nexport function get<T>(source: Readable<T>): T {\n return read(raw(source)) as T;\n}\n\nexport function set<T>(source: Signal<T>, value: T): void {\n write(raw(source), value);\n flush();\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const n = raw(target);\n if ((n.flags & WATCH) !== 0) stop(n);\n else detach(n);\n}\n"],
|
|
5
|
-
"mappings": ";
|
|
6
|
-
"names": ["active", "stack", "top", "wake", "onWake", "fn", "node", "fn", "value", "flags", "write", "s", "e", "n", "f", "wake", "mark", "first", "base", "top", "stack", "read", "pull", "c", "active", "last", "next", "miss", "root", "exec", "
|
|
4
|
+
"sourcesContent": ["// Reactive kernel: nodes, edges, tracking, push, pull, failure.\n//\n// A node is a signal (fn === null), a computed, or a watcher (WATCH). An edge\n// is one relation in two doubly linked lists: `to.deps` in the order `to`\n// last read its sources (prev/next) and `from.subs` in birth order\n// (prevSub/nextSub). Change is a version fact: a node bumps `ver` when its\n// value changes; an edge keeps the `ver` its reader saw.\n//\n// Removal invariant: an edge taken out of a list must not keep a pointer into\n// either list, because garbage that points at live or younger edges is kept\n// alive by the scavenger once it is old (an old dead edge pins newer ones, and\n// through `to` their watchers and closures, until a major GC). Removal is\n// therefore two steps: unlink the neighbours, then clear the edge's own\n// links. `unsub` clears prevSub/nextSub; `cut` clears the head's `prev`, and\n// `fail` clears `prev`/`next`. In `cut` the interior of a removed run keeps\n// its `next` chain, which only reaches other garbage of the same cut and\n// is read by `unsub` while the run is walked. Any new removal path must\n// do the same.\n//\n// Written for V8: one node shape and one edge shape, each built at a single\n// site; numeric fields are SMI counters (masked); conditions are explicit\n// comparisons, so no ToBoolean runs on mixed values; no recursion, no array\n// methods, and no allocation besides nodes and new edges.\n\ndeclare const __STATS__: boolean;\n\n/** A source below may have changed. For a watcher: validate before running. */\nexport const STALE = 1;\n/** Must run: first run, failed run, or a direct source changed. */\nexport const DIRTY = 2;\n/** Its run is on the stack. */\nexport const BUSY = 4;\n/** Its run put its edges into the sources' slots. */\nconst INDEX = 8;\n/** A watcher: handed to the host, never read. */\nexport const WATCH = 16;\n/** A watcher woken by signals only: its computed sources are clean, so no validation. */\nexport const WOKEN = 32;\n// Bits from 64 up belong to the host.\n\n/** edge.ver of an edge the indexed run has not read yet. */\nconst UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nconst MASK = 0x3fffffff;\n/** Scan budget before indexing: a constant keeps a run linear. */\nconst SCAN = 8;\n\nexport interface Node {\n flags: number;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: number;\n fn: ((prev: unknown) => unknown) | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\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: number;\n}\n\nexport interface Edge {\n from: Node;\n to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: number;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: number;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\nexport const stats = {\n reads: 0,\n hits: 0,\n repeats: 0,\n scans: 0,\n lookups: 0,\n dups: 0,\n moves: 0,\n links: 0,\n unlinks: 0,\n indexes: 0,\n indexed: 0,\n runs: 0,\n marks: 0,\n checks: 0,\n};\n\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.\n */\nconst stack: (Node | Edge | null)[] = [];\nlet top = 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\nexport function node(fn: Node[\"fn\"], value: unknown, flags: number): 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// ------------------------------------------------------------------- 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 & BUSY) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & STALE) === 0) {\n n.flags = f | STALE;\n\n if ((f & WATCH) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & WATCH) !== 0) {\n n.flags = f | DIRTY | WOKEN;\n\n wake(n);\n } else if ((f & STALE) !== 0) {\n n.flags = f | DIRTY;\n } else {\n n.flags = f | DIRTY | 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 & WATCH) !== 0) {\n n.flags = f | STALE;\n wake(n);\n } else if ((f & STALE) === 0) {\n n.flags = f | 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 if ((n.flags & (STALE | DIRTY | BUSY)) !== 0) pull(n);\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 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 still checking DIRTY.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n\n if ((f & BUSY) !== 0) throw new Error(\"Cycle detected\");\n\n if ((f & DIRTY) !== 0) return exec(root);\n\n verify(root, f);\n}\n\n/** The walk of `pull` for a STALE, not DIRTY, not BUSY node; kept apart so `pull` stays small. */\nfunction verify(root: Node, f: number): void {\n root.flags = f & ~STALE;\n const base = top;\n let n = root;\n let e = root.deps;\n\n try {\n for (;;) {\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n\n if (__STATS__) stats.checks++;\n\n if ((sf & (STALE | DIRTY)) !== 0) {\n if ((sf & BUSY) !== 0) throw new Error(\"Cycle detected\");\n\n if ((sf & DIRTY) === 0) {\n s.flags = sf & ~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 exec(n);\n break;\n }\n e = e.next;\n }\n\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) return;\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 exec(n);\n }\n }\n } catch (error) {\n n.flags |= DIRTY;\n\n while (top > base) {\n (stack[--top] as Edge).to.flags |= 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.\n if ((w.flags & DIRTY) === 0) {\n for (; e !== null; e = e.next) {\n const s = e.from;\n\n if (__STATS__) stats.checks++;\n\n if ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) pull(s);\n\n if (s.ver !== e.ver) {\n w.flags |= 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 ((s.flags & (STALE | DIRTY)) !== 0 && e.gen !== tentative) 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.\n n.flags = (n.flags & ~(STALE | DIRTY)) | 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 & INDEX) !== 0) unindex(n, base);\n\n n.flags = f & ~(BUSY | INDEX);\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 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 & INDEX) !== 0) unindex(n, base);\n\n n.flags &= ~(BUSY | INDEX);\n\n if (n.fn === null) return detach(n);\n\n n.flags |= 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 & BUSY) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(STALE | DIRTY | WOKEN);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** A read that is not the next edge in read order. Every answer is exact. */\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 last.ver = s.ver;\n return;\n }\n\n if ((c.flags & INDEX) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next, false);\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 p.ver = s.ver;\n return;\n }\n\n if ((e === null && p === null) || absent(s, c) === true)\n return link(c, s, last, next, false);\n index(c, next);\n }\n\n if (__STATS__) stats.lookups++;\n\n const cell = s.slot;\n\n if (cell !== null && cell.to === c) {\n if (cell.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n\n cell.ver = s.ver;\n return;\n }\n\n if (__STATS__) stats.moves++;\n\n cell.ver = s.ver;\n return move(c, cell, last, next!);\n }\n link(c, s, last, next, true);\n}\n\n/** No edge to `c` among the source's few subscribers. */\nfunction absent(s: Node, c: Node): boolean {\n let budget = SCAN;\n\n for (let e = s.subs; e !== null; e = e.nextSub)\n if (e.to === c || --budget === 0) return false;\n\n return true;\n}\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nfunction index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n\n c.flags |= INDEX;\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 const s = e.from;\n\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\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/** Move an unread edge (strictly after `next`) up to the cursor. */\nfunction 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/** A new edge at the cursor, appended to the source's subscribers. */\nfunction link(\n c: Node,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n indexed: boolean,\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 if (indexed) {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n }\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nfunction 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/** Take an edge out of its source's subscriber list. */\nfunction 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", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport {\n DIRTY,\n STALE,\n WATCH,\n WOKEN,\n check,\n dispose,\n exec,\n node,\n onWake,\n untracked,\n type Node,\n} from \"./kernel\";\n\n/** Run by this host's queue; other watchers are run by their owner. */\nconst AUTO = 64;\nconst QUEUED = 128;\n\ntype Fn = () => unknown;\n\nconst queue: (Node | null)[] = [];\nlet size = 0;\nlet depth = 0;\nlet 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 & (AUTO | QUEUED)) !== AUTO) return;\n\n w.flags |= 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(fn, undefined, WATCH | STALE | DIRTY | (auto ? AUTO : 0));\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 & (STALE | WOKEN)) === 0) return;\n\n w.flags = flags & ~(STALE | WOKEN);\n\n if ((flags & STALE) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= STALE;\n throw error;\n }\n }\n\n if ((w.flags & 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 Fn);\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 Fn);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (draining || depth !== 0 || size === 0) return;\n\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 &= ~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", "// Public API: signal, computed, effect, get, set, dispose, batch, untracked.\n// A handle is the node itself: nothing is allocated besides the node.\nimport {\n DIRTY,\n WATCH,\n dispose as detach,\n node,\n read,\n write,\n type Node,\n} from \"./kernel\";\nimport { flush, start, stop, watcher } from \"./host\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Compile-time brands only. They do not exist at runtime.\ndeclare const SIGNAL: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\nexport interface Signal<T> {\n readonly [SIGNAL]: T;\n}\n\nexport interface Computed<T> {\n readonly [COMPUTED]: T;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport type Readable<T> = Signal<T> | Computed<T>;\n\nconst raw = (handle: unknown): Node => handle as Node;\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T> {\n return node(fn as Node[\"fn\"], undefined, 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 start(effectNode);\n return effectNode as unknown as Effect;\n}\n\n/** A signal's flags are 0, so one read path serves signals and computeds. */\nexport function get<T>(source: Readable<T>): T {\n return read(raw(source)) as T;\n}\n\nexport function set<T>(source: Signal<T>, value: T): void {\n write(raw(source), value);\n flush();\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const targetNode = raw(target);\n\n if ((targetNode.flags & WATCH) !== 0) {\n stop(targetNode);\n } else {\n detach(targetNode);\n }\n}\n"],
|
|
5
|
+
"mappings": ";AA+FA,IAAIA,EAAsB,KAMpBC,EAAgC,CAAC,EACnCC,EAAM,EACNC,EAA0B,IAAM,CAAC,EAO9B,SAASC,EAAOC,EAA6B,CAClDF,EAAOE,CACT,CAOO,SAASC,EAAKC,EAAgBC,EAAgBC,EAAqB,CACxE,MAAO,CACL,MAAAA,EACA,MAAAD,EACA,IAAK,EACL,GAAAD,EACA,KAAM,KACN,KAAM,KACN,KAAM,KACN,QAAS,KACT,KAAM,KACN,IAAK,CACP,CACF,CAIO,SAASG,EAAMC,EAASH,EAAsB,CACnD,GAAI,QAAO,GAAGG,EAAE,MAAOH,CAAK,EAE5B,CAAAG,EAAE,MAAQH,EACVG,EAAE,IAAOA,EAAE,IAAM,EAAK,WAEtB,QAASC,EAAID,EAAE,KAAMC,IAAM,KAAMA,EAAIA,EAAE,QAAS,CAC9C,IAAMC,EAAID,EAAE,GACNE,EAAID,EAAE,OAIPC,EAAI,KAAU,GAEZA,EAAI,KAAW,IAClBD,EAAE,MAAQC,EAAI,GAETA,EAAI,MAAW,EAAGC,EAAKF,CAAC,EACpBA,EAAE,OAAS,MAAMG,EAAKH,EAAE,IAAI,IAE7BC,EAAI,MAAW,GACzBD,EAAE,MAAQC,EAAI,EAAQ,GAEtBC,EAAKF,CAAC,IACIC,EAAI,KAAW,EACzBD,EAAE,MAAQC,EAAI,GAEdD,EAAE,MAAQC,EAAI,EAAQ,EAElBD,EAAE,OAAS,MAAMG,EAAKH,EAAE,IAAI,EAEpC,EACF,CAGA,SAASG,EAAKC,EAAmB,CAC/B,IAAMC,EAAOC,EACTP,EAAiBK,EAErB,OAAS,CACP,KAAOL,IAAM,MAAM,CACjB,IAAMC,EAAUD,EAAE,GACZE,EAAID,EAAE,MAIZ,IAAKC,EAAI,MAAW,EAClBD,EAAE,MAAQC,EAAI,EACdC,EA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EAAMyB,CAAC,EAC7BzB,EAAMyB,CAAC,EAAI,KACXC,EAAc,OAAS,CAAC3B,EAExB,GAAI,CACFY,EAAIe,CAAa,CACnB,OAASZ,EAAO,CACTS,IAAQC,EAAQV,GAErBS,EAAS,EACX,CACF,CAIA,GAHAtB,EAAO,EACPE,EAAW,GAEPoB,EAAQ,MAAMC,CACpB,CAEO,SAASG,EAASnB,EAAgB,CACvCN,IAEA,GAAI,CACF,OAAOM,EAAG,CACZ,QAAE,CACI,EAAEN,IAAU,GAAGkB,EAAM,CAC3B,CACF,CCjHA,IAAMQ,EAAOC,GAA0BA,EAEhC,SAASC,GAAUC,EAAqB,CAC7C,OAAOC,EAAK,KAAMD,EAAO,CAAC,CAC5B,CAEO,SAASE,GAAYC,EAA6C,CACvE,OAAOF,EAAKE,EAAkB,OAAW,CAAK,CAChD,CAGO,SAASC,GAAOD,EAAuC,CAC5D,IAAME,EAAaC,EAAQH,EAAI,EAAI,EACnC,OAAAI,EAAMF,CAAU,EACTA,CACT,CAGO,SAASG,GAAOC,EAAwB,CAC7C,OAAOC,EAAKb,EAAIY,CAAM,CAAC,CACzB,CAEO,SAASE,GAAOF,EAAmBT,EAAgB,CACxDY,EAAMf,EAAIY,CAAM,EAAGT,CAAK,EACxBa,EAAM,CACR,CAEO,SAASC,GAAQC,EAA0C,CAChE,IAAMC,EAAanB,EAAIkB,CAAM,GAExBC,EAAW,MAAQ,MAAW,EACjCC,EAAKD,CAAU,EAEfF,EAAOE,CAAU,CAErB",
|
|
6
|
+
"names": ["active", "stack", "top", "wake", "onWake", "fn", "node", "fn", "value", "flags", "write", "s", "e", "n", "f", "wake", "mark", "first", "base", "top", "stack", "read", "pull", "c", "active", "last", "next", "miss", "root", "exec", "verify", "sf", "up", "error", "check", "w", "tentative", "untracked", "prev", "fail", "unindex", "detach", "cut", "after", "before", "unsub", "dispose", "link", "budget", "move", "p", "absent", "index", "cell", "unread", "indexed", "tail", "AUTO", "QUEUED", "queue", "size", "depth", "draining", "wake", "w", "onWake", "watcher", "fn", "auto", "node", "run", "flags", "check", "error", "cleanup", "untracked", "exec", "stop", "dispose", "flush", "drain", "start", "failed", "first", "i", "queuedWatcher", "batch", "raw", "handle", "signal", "value", "node", "computed", "fn", "effect", "effectNode", "watcher", "start", "get", "source", "read", "set", "write", "flush", "dispose", "target", "targetNode", "stop"]
|
|
7
7
|
}
|
package/dist/kernel.d.cts
CHANGED
|
@@ -53,7 +53,11 @@ export declare const stats: {
|
|
|
53
53
|
marks: number;
|
|
54
54
|
checks: number;
|
|
55
55
|
};
|
|
56
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* The host is told about every watcher a write or mark reaches. It owns the
|
|
58
|
+
* deduplication (a queued watcher is not queued again), so a watcher left
|
|
59
|
+
* STALE by a failed check is delivered again by the next push.
|
|
60
|
+
*/
|
|
57
61
|
export declare function onWake(fn: (w: Node) => void): void;
|
|
58
62
|
/** Nothing running, the work stack empty (tests). */
|
|
59
63
|
export declare function idle(): boolean;
|
package/dist/kernel.d.ts
CHANGED
|
@@ -53,7 +53,11 @@ export declare const stats: {
|
|
|
53
53
|
marks: number;
|
|
54
54
|
checks: number;
|
|
55
55
|
};
|
|
56
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* The host is told about every watcher a write or mark reaches. It owns the
|
|
58
|
+
* deduplication (a queued watcher is not queued again), so a watcher left
|
|
59
|
+
* STALE by a failed check is delivered again by the next push.
|
|
60
|
+
*/
|
|
57
61
|
export declare function onWake(fn: (w: Node) => void): void;
|
|
58
62
|
/** Nothing running, the work stack empty (tests). */
|
|
59
63
|
export declare function idle(): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reflex-quant",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "The smallest reactive runtime the invariants of Reflex imply: signals, computeds, effects; exact dependency tracking, no recursion, ~2 KB gzip.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"signals",
|
|
@@ -52,12 +52,22 @@
|
|
|
52
52
|
"scripts": {
|
|
53
53
|
"build": "node scripts/build.mjs",
|
|
54
54
|
"check": "node scripts/check.mjs",
|
|
55
|
+
"lint": "eslint src",
|
|
56
|
+
"format": "prettier --write src eslint.config.mjs .prettierrc.json",
|
|
57
|
+
"format:check": "prettier --check src eslint.config.mjs .prettierrc.json",
|
|
55
58
|
"test": "vitest run --config vitest.config.ts",
|
|
56
59
|
"typecheck": "tsc -p tsconfig.json",
|
|
57
60
|
"prepublishOnly": "npm run typecheck && npm run build && npm run check"
|
|
58
61
|
},
|
|
59
62
|
"devDependencies": {
|
|
63
|
+
"alien-signals": "3.2.1",
|
|
60
64
|
"esbuild": "^0.25.0",
|
|
61
|
-
"
|
|
65
|
+
"eslint": "^10.14.0",
|
|
66
|
+
"eslint-config-prettier": "^10.1.8",
|
|
67
|
+
"fast-check": "^4.7.0",
|
|
68
|
+
"prettier": "^3.9.10",
|
|
69
|
+
"typescript": "^5.6.0",
|
|
70
|
+
"typescript-eslint": "^8.72.1",
|
|
71
|
+
"vitest": "^4.1.5"
|
|
62
72
|
}
|
|
63
73
|
}
|