reflex-quant 1.0.0 → 1.1.1
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 +144 -63
- package/dist/index.cjs +2 -2
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.cts +34 -10
- package/dist/index.d.ts +34 -10
- package/dist/index.development.cjs +401 -0
- package/dist/index.development.cjs.map +7 -0
- package/dist/index.development.js +382 -0
- package/dist/index.development.js.map +7 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/package.json +4 -1
- package/dist/host.d.cts +0 -28
- package/dist/host.d.ts +0 -28
- package/dist/kernel.d.cts +0 -79
- package/dist/kernel.d.ts +0 -79
package/README.md
CHANGED
|
@@ -1,63 +1,144 @@
|
|
|
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
|
-
[
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
[CONTRACT.md](CONTRACT.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
|
+
The runtime has three operational layers: the **kernel** owns the dependency
|
|
11
|
+
machine (graph, tracking, propagation and validation); the **host** handles
|
|
12
|
+
watcher lifecycle and delivery; and the **surface** exposes the typed API.
|
|
13
|
+
Research instrumentation and development diagnostics are eliminated from the
|
|
14
|
+
production build. The standalone ESM bundle is about 2.2 KB gzip.
|
|
15
|
+
|
|
16
|
+
- **Report:** [research/REPORT.md](research/REPORT.md). **Design log**, one
|
|
17
|
+
counterexample per mechanism: [research/LOG.md](research/LOG.md).
|
|
18
|
+
- **Textbook version:** branch
|
|
19
|
+
[`padawan`](https://github.com/volynetstyle/reflex-quant/tree/padawan), the
|
|
20
|
+
same semantics without the engineering (recursive push and pull, one
|
|
21
|
+
exact index, no counters), written as a guided tour. It passes the same
|
|
22
|
+
differential corpus; this branch is its lowering for speed and stack depth.
|
|
23
|
+
|
|
24
|
+
## Example
|
|
25
|
+
|
|
26
|
+
A computation depends on exactly what its last run read, so dependencies
|
|
27
|
+
change from run to run:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { computed, effect, get, set, signal } from "reflex-quant";
|
|
31
|
+
|
|
32
|
+
const full = signal(false);
|
|
33
|
+
const first = signal("Ada");
|
|
34
|
+
const last = signal("Lovelace");
|
|
35
|
+
const name = computed(() => (get(full) ? `${get(first)} ${get(last)}` : get(first)));
|
|
36
|
+
|
|
37
|
+
effect(() => console.log(get(name))); // "Ada" name reads full, first
|
|
38
|
+
set(last, "Byron"); // nothing runs: last was not read
|
|
39
|
+
set(full, true); // "Ada Byron" name now reads last too
|
|
40
|
+
set(full, false); // "Ada" last is dropped again
|
|
41
|
+
set(last, "King"); // nothing runs
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
When a computation reads the same sources in a different order, its
|
|
45
|
+
existing relations are moved, not rebuilt: with 256 sources rotated every
|
|
46
|
+
pass (`reorder.rotate-256`), quant creates and removes no relations per
|
|
47
|
+
pass, where alien-signals relinks 255 (`node bench/structure.mjs`).
|
|
48
|
+
|
|
49
|
+
## Results
|
|
50
|
+
|
|
51
|
+
- Equal to Reflex's executable spec, with three documented choices applied,
|
|
52
|
+
on all 27,061 bounded and 18,660 recovery programs and on 100,000
|
|
53
|
+
generated ones, with graph integrity checked after every operation.
|
|
54
|
+
- Current Reflex is that contract plus three failure-path choices, on the
|
|
55
|
+
same corpus. One of them drops an execution obligation that the contract
|
|
56
|
+
(R2) says survives a failure.
|
|
57
|
+
- All 34 mechanism ablations fail a test: 25 a semantic test, 8 only a
|
|
58
|
+
structural bound or a representation test (the work an optimization saves, counted), 1 only a
|
|
59
|
+
sibling-order test (a convention). The 7 ablations of the development
|
|
60
|
+
diagnostic (C1) fail a test of the development build.
|
|
61
|
+
- On CI (run 42287159440): within 25 % of the better of Reflex and
|
|
62
|
+
alien-signals on all 18 scenarios, at or ahead of it on 8; geometric mean
|
|
63
|
+
0.97 of the better one, 0.95 of Reflex, 0.42 of alien-signals. Reorders
|
|
64
|
+
allocate nothing. Reflex remains faster on every reorder scenario
|
|
65
|
+
(quant 1.14–1.24 of it). The suite is this project's own; per-scenario
|
|
66
|
+
numbers are in research/REPORT.md (Q6), the trajectory over every CI run
|
|
67
|
+
in research/HISTORY.md.
|
|
68
|
+
|
|
69
|
+
## Surface
|
|
70
|
+
|
|
71
|
+
Every operation exposes semantics the kernel and host already have; none adds
|
|
72
|
+
a field, a flag or a branch on a hot path ([research/LOG.md](research/LOG.md) item 26).
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const a = signal(1);
|
|
76
|
+
const total = computed((prev) => prev + get(a), 0); // prev: last committed value
|
|
77
|
+
get(total); // refresh and observe
|
|
78
|
+
peek(total); // refresh, do not observe
|
|
79
|
+
set(a, 2);
|
|
80
|
+
update(a, (v) => v + 1); // untracked fn, then set
|
|
81
|
+
dispose(total); // detach; the value stays readable
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
- `computed(fn, initial?)`: `prev` is the last **successfully committed** value
|
|
85
|
+
(`initial` before the first commit; a failed run leaves it unchanged). It
|
|
86
|
+
advances per execution, not per write: writes coalesced by a batch or by
|
|
87
|
+
laziness are one step. Returning `prev` itself is a cutoff, so custom
|
|
88
|
+
equality needs no comparator: `(prev) => (equal(prev, next) ? prev : next)`.
|
|
89
|
+
Sources keep `Object.is`.
|
|
90
|
+
- `peek(x)`: `get` without its tracking half; equal to
|
|
91
|
+
`untracked(() => get(x))`, without the closure.
|
|
92
|
+
- `update(s, fn)`: a write, not an observation. `fn` runs untracked, so
|
|
93
|
+
neither `s` nor what `fn` reads becomes a dependency of the running
|
|
94
|
+
consumer; if `fn` throws, nothing is written.
|
|
95
|
+
- `effect(fn)`: if the call throws, it returns no handle and leaves nothing
|
|
96
|
+
live; a cleanup its run returned has already run.
|
|
97
|
+
|
|
98
|
+
### Development build
|
|
99
|
+
|
|
100
|
+
Bundlers that resolve the `development` export condition, and Node with
|
|
101
|
+
`--conditions=development`, load `dist/index.development.js` (or `.cjs`). It
|
|
102
|
+
diagnoses a precondition that the production build does not check (contract C1):
|
|
103
|
+
a read or a validation that reaches a computed whose evaluation is in
|
|
104
|
+
progress throws `Cycle detected`, instead of answering with the computed's
|
|
105
|
+
previous value. Such a demand comes from a cycle, or from a write inside a
|
|
106
|
+
computed whose synchronous effects read back into it. The production build
|
|
107
|
+
pays nothing for the diagnosis; the release check verifies the split.
|
|
108
|
+
|
|
109
|
+
## Layout
|
|
110
|
+
|
|
111
|
+
```text
|
|
112
|
+
src/ graph representation, kernel machine, host, public surface, instrumentation
|
|
113
|
+
test/ differential (spec, contract spec, Reflex, quant), tracking,
|
|
114
|
+
semantics, structural bounds; tools/ (QUANT_TOOLS=1)
|
|
115
|
+
bench/ scenarios, adapters, build, timing (CI only), structure counters
|
|
116
|
+
research/ evidence, design log, ablations, and historical benchmark snapshots
|
|
117
|
+
reference/ frozen Reflex implementation and provenance fixtures
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Running
|
|
121
|
+
|
|
122
|
+
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 `reference/reflex` and do not require a Reflex checkout. From this directory:
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
npm run lint # lint production TypeScript
|
|
126
|
+
npm run format:check # check production formatting
|
|
127
|
+
npm run format # format production code and tool config
|
|
128
|
+
npm run build && npm run check # standalone release and declarations
|
|
129
|
+
npx vitest run --config vitest.config.ts # all tests, development build
|
|
130
|
+
npm run test:production # the same suite, production build
|
|
131
|
+
npx tsc -p tsconfig.json # typecheck src
|
|
132
|
+
node research/mutants.mjs # ablation -> research/mutants.json
|
|
133
|
+
node bench/build.mjs # bundles: quant, Reflex bf71877, alien-signals
|
|
134
|
+
node bench/run.mjs --verify-only # same answers on all three runtimes
|
|
135
|
+
node bench/structure.mjs # structural work per step
|
|
136
|
+
node bench/run.mjs --rounds 9 # timing: run it on CI, not locally
|
|
137
|
+
node bench/run.mjs --suite mechanisms --only '^push\.' # one mechanism: read write track push pull effect batch lifecycle
|
|
138
|
+
node bench/run.mjs --suite scaling --only '^chain@' --out .cache/bench/s.json && node bench/scaling-report.mjs .cache/bench/s.json
|
|
139
|
+
# growth with load: N = 16..4096, log-log slope per runtime
|
|
140
|
+
node bench/cost/build.mjs && node bench/cost/run.mjs regime --rounds 0
|
|
141
|
+
# cost-model probes (research/COST-MODEL.md); timing on CI
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
CI is split in two workflows. `.github/workflows/ci.yml` runs lint, typecheck/release build and tests as parallel jobs. `.github/workflows/benchmark.yml` builds and checksum-verifies the bundles once, then runs in parallel one timing job per mechanism (`bench/mechanisms.mjs`), the graph scenarios (`bench/scenarios.mjs`) in shards, the structural counters and the edge-churn probe. Each timing job runs all three runtimes on one runner: compare ratios within a job, never times across jobs. The Reflex benchmark baseline is fixed at commit `bf71877`; see `reference/reflex/README.md` for its provenance.
|
package/dist/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* reflex-quant 1.
|
|
2
|
-
"use strict";var
|
|
1
|
+
/* reflex-quant 1.1.1 | MIT */
|
|
2
|
+
"use strict";var b=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var le=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var re=(e,n)=>{for(var t in n)b(e,t,{get:n[t],enumerable:!0})},ue=(e,n,t,l)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of le(n))!oe.call(e,o)&&o!==t&&b(e,o,{get:()=>n[o],enumerable:!(l=te(n,o))||l.enumerable});return e};var fe=e=>ue(b({},"__esModule",{value:!0}),e);var Ee={};re(Ee,{batch:()=>ee,computed:()=>_e,dispose:()=>ke,effect:()=>ve,get:()=>Se,peek:()=>xe,set:()=>ne,signal:()=>ge,untracked:()=>p,update:()=>Te});module.exports=fe(Ee);var x=-1,d=1073741823,V=536870911,T=8,I=256,M=512;function v(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 m(e,n,t,l){let o=n.prev,r=n.next;o.next=r,r!==null&&(r.prev=o),n.prev=t,n.next=l,t!==null?t.next=n:e.deps=n,l.prev=n,e.last=n}function h(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)N(t)}function q(e){let n=e.gen;for(let t=e.deps;t!==null;t=t.next)t.gen=n}function N(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 s=null,i=new Array(256).fill(null),f=0;var k=()=>{};function P(e){k=e}function ie(){i.length=I}function Q(e,n){if(!Object.is(e.value,n)){e.value=n,e.ver=e.ver+1&d;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?k(l):l.subs!==null&&U(l.subs)):(o&16)!==0?(l.flags=o|2|32,k(l)):(o&1)!==0?l.flags=o|2:(l.flags=o|2|1,l.subs!==null&&U(l.subs))}}}function U(e){let n=f,t=e;for(;;){for(;t!==null;){let l=t.to,o=l.flags;if((o&16)!==0)l.flags=o|1,k(l);else if((o&1)===0&&(l.flags=o|1,l.subs!==null)){t.nextSub!==null&&(i[f++]=t.nextSub),t=l.subs;continue}t=t.nextSub}if(f===n)return;t=i[--f],i[f]=null}}function G(e){let n=C(e),t=s;if(t!==null){let l=t.last,o=l===null?t.deps:l.next;o!==null&&o.from===e?(t.last=o,o.ver=e.ver):de(t,e,l,o)}return n}function C(e){return(e.flags&39)!==0&&D(e),e.value}function D(e){let n=e.flags;if((n&36)!==0)throw new Error("Cycle detected");if((n&2)!==0)return c(e);se(e,n)}function se(e,n){e.flags=n&-2;let t=f,l=e,o=e.deps;try{for(;;){e:for(;o!==null;){let r=o.from,u=r.flags;if((u&3)!==0){if((u&36)!==0)throw new Error("Cycle detected");if((u&2)===0){r.flags=u&-2,i[f++]=o,l=r,o=r.deps;continue}c(r)}if(r.ver!==o.ver){l.fn!==null&&c(l);break e}o=o.next}for(;;){if(f===t){i.length>M&&t===0&&ie();return}let r=i[--f];if(i[f]=null,l=r.to,r.from.ver===r.ver){o=r.next;break}l.fn!==null&&c(l)}}}catch(r){for(l.fn!==null&&(l.flags=l.flags|2);f>t;){let u=i[--f].to;u.fn!==null&&(u.flags=u.flags|2),i[f]=null}throw r}}function L(e){let n=e.gen+1&d,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&&D(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&&D(l)}}function p(e){let n=s;s=null;try{return e()}finally{s=n}}function c(e){let n=s,t=f;e.flags=e.flags&-4|4,e.last=null,s=e;let l;try{l=e.fn(e.value)}catch(u){throw s=n,ae(e,t),u}s=n;let o=e.flags;if((o&8)!==0&&H(e,t),e.flags=o&-269,e.fn===null)return w(e);let r=e.last;(r===null?e.deps:r.next)!==null&&h(e,r),e.gen=e.gen+1&d,(e.gen&V)===0&&q(e),Object.is(l,e.value)===!1&&(e.value=l,e.ver=e.ver+1&d)}function ae(e,n){if((e.flags&8)!==0&&H(e,n),e.flags&=-269,e.fn===null)return w(e);e.flags|=2;let t=e.gen+1&d,l=e.last;for(let o=l===null?e.deps:l.next;o!==null;){let r=o.next;if(o.gen===t){let u=o.prev;u!==null?u.next=r:e.deps=r,r!==null&&(r.prev=u),N(o),o.prev=null,o.next=null}o=r}}function E(e){e.fn!==null&&(e.fn=null,(e.flags&4)===0&&w(e))}function w(e){h(e,null),e.flags&=-36}function j(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=x),z(l.from,l)}function z(e,n){e.slot!==null&&(i[f++]=e,i[f++]=e.slot),e.slot=n}function H(e,n){for(let t=e.deps;t!==null;t=t.next)t.from.slot=null;for(;f>n;){let t=i[--f];i[f]=null;let l=i[--f];i[f]=null,l.slot=t}}function A(e,n,t,l){let o=n.lastSub,r={from:n,to:e,ver:n.ver,gen:e.gen+1&d,prev:t,next:l,prevSub:o,nextSub:null};t!==null?t.next=r:e.deps=r,l!==null&&(l.prev=r),o!==null?o.nextSub=r:n.subs=r,n.lastSub=r,e.last=r}function de(e,n,t,l){if(t!==null&&t.from===n)return;let o=e.flags;if((o&8)!==0)return K(e,n,t,l);let r;if((o&256)===0){if(n.subs===null)return A(e,n,t,l);let u=l!==null?l.next:null,y=T;for(;u!==null&&u.from!==n&&--y!==0;)u=u.next;if(u!==null&&u.from===n)return u.ver=n.ver,m(e,u,t,l);let a=t!==null?t.prev:null;for(y=T;a!==null&&a.from!==n&&--y!==0;)a=a.prev;if(a!==null&&a.from===n)return;r=u===null&&a===null?null:O(n,e),r!=null&&ce(e,n,r,l)}else r=O(n,e);if(r===null)return A(e,n,t,l);if(r!==void 0)return J(e,r,n,t,l);j(e,l),K(e,n,t,l)}function K(e,n,t,l){let o=n.slot;if(o!==null&&o.to===e)return J(e,o,n,t,l);A(e,n,t,l),z(n,e.last)}function O(e,n){let t=T;for(let l=e.subs;l!==null;l=l.nextSub){if(l.to===n)return l;if(--t===0)return}return null}function ce(e,n,t,l){if(t!==n.subs)return j(e,l);e.flags|=256;for(let o=l;o!==null;o=o.next)o.ver=x}function J(e,n,t,l,o){n.ver===x&&(n.ver=t.ver,m(e,n,l,o))}var F=[],g=0,S=0,_=!1;function X(e){(e.flags&192)===64&&(e.flags|=128,F[g++]=e)}P(X);function Y(e,n){return v(e,void 0,19|(n?64:0))}function pe(e){let n=e.flags;if(e.fn===null||(n&33)===0)return;if(e.flags=n&-34,(n&1)!==0)try{L(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,p(t),e.fn===null)||c(e)}function R(e){if(e.fn===null)return;let n=e.value;e.value=void 0,E(e),typeof n=="function"&&p(n)}function Z(){g===0||_||S!==0||B()}function B(){_=!0,W(!1,void 0)}function $(e){if(_||S!==0||g!==0)return X(e),Z();_=!0;try{c(e)}catch(n){return W(!0,n)}W(!1,void 0)}function W(e,n){for(let t=0;t<g;t++){let l=F[t];F[t]=null,l.flags&=-129;try{pe(l)}catch(o){e||(n=o),e=!0}}if(g=0,_=!1,e)throw n}function ee(e){S++;try{return e()}finally{--S===0&&Z()}}function ge(e){return v(null,e,0)}function _e(e,n){return v(e,n,2)}function ve(e){let n=Y(e,!0);try{$(n)}catch(t){throw R(n),t}return n}var Se=G,xe=C;function ne(e,n){Q(e,n),g!==0&&!_&&S===0&&B()}function Te(e,n){ne(e,p(()=>n(e.value)))}function ke(e){let n=e;(n.flags&16)!==0?R(n):E(n)}
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 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 {\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,KAAW,EACzBD,EAAE,MAAQC,EAAI,GAEdD,EAAE,MAAQC,EAAI,EAAQ,EAElBD,EAAE,OAAS,MAAMG,EAAKH,EAAE,IAAI,EAEpC,EACF,CAGA,SAASG,EAAKC,EAAmB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AAO,CACd,OAAOO,EAAM,GAAMP,CAAK,CAC1B,CACAO,EAAM,GAAO,MAAS,CACxB,CAGA,SAASA,EAAME,EAAiBC,EAAsB,CACpD,QAASC,EAAI,EAAGA,EAAIxB,EAAMwB,IAAK,CAC7B,IAAMC,EAAgB1B,EAAMyB,CAAC,EAC7BzB,EAAMyB,CAAC,EAAI,KACXC,EAAc,OAAS,CAAC3B,EAExB,GAAI,CACFY,GAAIe,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,CFjHA,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": ["index_exports", "__export", "batch", "computed", "dispose", "effect", "get", "set", "signal", "untracked", "__toCommonJS", "
|
|
3
|
+
"sources": ["../src/index.ts", "../src/graph.ts", "../src/kernel.ts", "../src/host.ts"],
|
|
4
|
+
"sourcesContent": ["// Public API: signal, computed, effect, get, peek, set, update, dispose,\n// batch, untracked. A handle is the node itself: nothing is allocated besides\n// the node, and handle <-> node conversions are type assertions only (no\n// runtime wrapper). Every operation here exposes existing kernel and host\n// semantics; none adds state.\nimport { Flag } from \"./graph\";\nimport { depth, drainNow, draining, size, start, stop, watcher } from \"./host\";\nimport {\n dispose as detach,\n node,\n peek as refresh,\n read,\n untracked,\n write,\n} from \"./kernel\";\nimport type { Fn, Node } from \"./graph\";\n\nexport { batch } from \"./host\";\nexport { untracked } from \"./kernel\";\n\n// Public handles: compile-time brands over the node, which is the runtime\n// handle. Variance is explicit: reading is covariant, a signal is also\n// written, so it is invariant.\ndeclare const READ: unique symbol;\ndeclare const WRITE: unique symbol;\ndeclare const COMPUTED: unique symbol;\ndeclare const EFFECT: unique symbol;\n\n/** Anything `get` accepts. */\nexport interface Readable<out T> {\n readonly [READ]: T;\n}\n\nexport interface Signal<in out T> extends Readable<T> {\n readonly [WRITE]: (value: T) => void;\n}\n\nexport interface Computed<out T> extends Readable<T> {\n readonly [COMPUTED]: true;\n}\n\nexport interface Effect {\n readonly [EFFECT]: true;\n}\n\nexport function signal<T>(value: T): Signal<T> {\n return node(null, value, 0) as unknown as Signal<T>;\n}\n\n/**\n * `fn` receives `prev`, the last successfully committed value: `initial`\n * (or undefined) before the first commit, unchanged by a failed run.\n * Returning `prev` itself is a cutoff: consumers do not rerun, so custom\n * equality is `(prev) => (equal(prev, next) ? prev : next)`.\n */\nexport function computed<T>(fn: (prev: T | undefined) => T): Computed<T>;\nexport function computed<T>(fn: (prev: T) => T, initial: T): Computed<T>;\nexport function computed<T>(\n fn: (prev: T | undefined) => T,\n initial?: T,\n): Computed<T> {\n return node(fn as Fn, initial, Flag.Dirty) as unknown as Computed<T>;\n}\n\n/** Runs now (or when the enclosing batch or drain ends), then on every change it read. */\nexport function effect(fn: () => void | (() => void)): Effect {\n const effectNode = watcher(fn, true);\n\n try {\n start(effectNode);\n } catch (error) {\n stop(effectNode);\n throw error;\n }\n\n return effectNode as unknown as Effect;\n}\n\n/**\n * Kernel `read` itself under the public type: a signal's flags are 0, so one\n * read path serves signals and computeds, and no surface function stands in\n * front of it.\n */\nexport const get: <T>(source: Readable<T>) => T = read as never;\n\n/** `get` without observation: up to date, but the running consumer does not depend on it. */\nexport const peek: <T>(source: Readable<T>) => T = refresh as never;\n\n/** Writes, then runs what the write scheduled; a write that woke nothing stops at one load. */\nexport function set<T>(source: Signal<T>, value: T): void {\n write(source as unknown as Node, value);\n\n if (size !== 0 && !draining && depth === 0) drainNow();\n}\n\n/**\n * `set(source, fn(current))`. An update is a write, not an observation:\n * `fn` runs untracked, so neither `source` nor what `fn` reads becomes a\n * dependency of the running consumer. If `fn` throws, nothing is written.\n */\nexport function update<T>(source: Signal<T>, fn: (value: T) => T): void {\n set(\n source,\n untracked(() => fn((source as unknown as Node).value as T)),\n );\n}\n\nexport function dispose(target: Computed<unknown> | Effect): void {\n const targetNode = target as unknown as Node;\n\n if ((targetNode.flags & Flag.Watch) !== 0) stop(targetNode);\n else detach(targetNode);\n}\n", "import { stats } from \"./instrumentation\";\n\n// Compile-time vocabulary of the runtime graph. Nothing here exists at runtime.\n\n// The aliases name different facts but do not brand numbers: counters wrap at\n// MASK and edge versions can carry UNREAD.\nexport type Flags = number;\nexport type Version = number;\nexport type Generation = number;\n\n/** A computed's or watcher's body; a signal has none. */\nexport type Fn = (prev: unknown) => unknown;\n/** A watcher's cleanup, run untracked. */\nexport type Cleanup = () => unknown;\n\nexport interface Node {\n flags: Flags;\n value: unknown;\n /** Bumps on every Object.is-visible change of value. */\n ver: Version;\n /** null for a signal and for a disposed node. */\n fn: Fn | null;\n /** Sources, in read order. */\n deps: Edge | null;\n /** The running pass's last accepted source edge. */\n last: Edge | null;\n /** Subscribers, in birth order. */\n subs: Edge | null;\n lastSub: Edge | null;\n /** While a reader indexes: this node's edge into that reader. */\n slot: Edge | null;\n /** Successful runs; edges born since the last one are tentative. */\n gen: Generation;\n}\n\nexport interface Edge {\n readonly from: Node;\n readonly to: Node;\n /** from.ver when `to` read it; UNREAD while indexed and not yet read. */\n ver: Version | typeof UNREAD;\n /** to.gen + 1 at birth: tentative while it still equals to.gen + 1. */\n gen: Generation;\n prev: Edge | null;\n next: Edge | null;\n prevSub: Edge | null;\n nextSub: Edge | null;\n}\n\n/** Shared LIFO of the kernel's walks: edges, and (source, cell) pairs. */\nexport type WorkStack = (Node | Edge | null)[];\n\n// Flag layout and constants used by the kernel and host.\nexport const enum Flag {\n /** A source below may have changed; a watcher must validate. */\n Stale = 1 << 0,\n /** Must run: first run, failed run, or a direct source changed. */\n Dirty = 1 << 1,\n /** Its run is on the stack. */\n Busy = 1 << 2,\n /** Its run put its edges into the sources' slots. */\n Indexed = 1 << 3,\n /** A watcher: handed to the host, never read. */\n Watch = 1 << 4,\n /** Woken by signals only; computed sources need no validation. */\n Woken = 1 << 5,\n /**\n * Development builds only (C1 is a precondition): a computed whose\n * validation is on the stack, no longer STALE and not yet clean. WOKEN's\n * bit, so every mask stays a byte: WOKEN is a watcher's, CHECKING a\n * computed's, and no node is both. A production build never sets it, so\n * a mask that tests it is inert there.\n */\n Checking = 1 << 5,\n /** Host policy: scheduled by the reference host. */\n Auto = 1 << 6,\n /** Host policy: already in its queue. */\n Queued = 1 << 7,\n /** Its run marked its unread edges UNREAD; pairs are found through subscribers. */\n Marked = 1 << 8,\n}\n\n/** edge.ver of an edge the marked or indexed run has not read yet. */\nexport const UNREAD = -1;\n/** Counters wrap inside the SMI range. */\nexport const MASK = 0x3fffffff;\n/**\n * A committed edge keeps its birth generation, and generations wrap: an edge\n * 2^30 - 1 commits old would read as born in the next run (tentative). So a\n * node restamps its edges with its own generation whenever a commit makes\n * `gen & SETTLE` zero, twice a cycle, and no edge falls that far behind.\n */\nexport const SETTLE = 0x1fffffff;\n/** Scan budget of every bounded search: a constant keeps a run linear. */\nexport const SCAN = 8;\n/**\n * Work-stack slots kept at rest, and the length past which the outermost\n * walk's end cuts the stack back to them. The gap keeps walks only a little\n * deeper than KEEP from cutting and regrowing on every pass.\n */\nexport const KEEP = 256;\nexport const TRIM = 512;\n\n// Intrusive graph operations independent of the execution context.\n\nexport function node(fn: Node[\"fn\"], value: unknown, flags: Flags): Node {\n return {\n flags,\n value,\n ver: 0,\n fn,\n deps: null,\n last: null,\n subs: null,\n lastSub: null,\n slot: null,\n gen: 0,\n };\n}\n\n/** Move an unread edge (strictly after `next`) up to the cursor. */\nexport function move(c: Node, e: Edge, last: Edge | null, next: Edge): void {\n const before = e.prev!;\n const after = e.next;\n before.next = after;\n\n if (after !== null) after.prev = before;\n\n e.prev = last;\n e.next = next;\n\n if (last !== null) last.next = e;\n else c.deps = e;\n next.prev = e;\n c.last = e;\n}\n\n/** Remove every edge of `c` after `last` (all of them when null). */\nexport function cut(c: Node, last: Edge | null): void {\n let e = last === null ? c.deps : last.next;\n\n if (e === null) return;\n\n if (last === null) c.deps = null;\n else last.next = null;\n // The cut head's back pointer would tie this garbage to the live list, and\n // so to garbage cut later: old garbage keeps newer garbage alive.\n e.prev = null;\n\n for (; e !== null; e = e.next) unsub(e);\n}\n\n/** Restamp every edge of `c`, all of them committed, with its generation (SETTLE). */\nexport function settle(c: Node): void {\n const gen = c.gen;\n\n for (let e = c.deps; e !== null; e = e.next) e.gen = gen;\n}\n\n/** Take an edge out of its source's subscriber list. */\nexport function unsub(e: Edge): void {\n if (__STATS__) stats.unlinks++;\n\n const s = e.from;\n const before = e.prevSub;\n const after = e.nextSub;\n\n if (before !== null) before.nextSub = after;\n else s.subs = after;\n\n if (after !== null) after.prevSub = before;\n else s.lastSub = before;\n // Subscribers are in birth order, so an old removed edge would point at a\n // much younger one and keep it (and its watcher) alive across scavenges.\n e.prevSub = null;\n e.nextSub = null;\n}\n", "// Reactive kernel: tracking, propagation, validation and execution.\n// Detached edges must not retain pointers into live lists.\n// See research/LOG.md for representation rationale.\n\nimport {\n Flag,\n KEEP,\n MASK,\n SCAN,\n SETTLE,\n TRIM,\n UNREAD,\n cut,\n move,\n settle,\n unsub,\n} from \"./graph\";\nimport type { Edge, Flags, Node, WorkStack } from \"./graph\";\nimport { stats } from \"./instrumentation\";\n\nexport { node } from \"./graph\";\nexport { stats } from \"./instrumentation\";\n\n/** The running consumer, or null outside a tracking pass. */\nlet active: Node | null = null;\n/**\n * One LIFO work stack shared by all walks: sibling edges to resume (mark),\n * edges to climb back through (pull), and (source, cell) pairs an indexed run\n * displaced. Every walk pops back to its own base before returning, and the\n * outermost one gives back what a deep walk grew (`trim`).\n */\nconst stack: WorkStack = new Array(256).fill(null);\n/** First free index of `stack`. */\nlet top = 0;\n/** Longest the work stack has been when `trim` cut it (research). */\nlet peak = 0;\nlet wake: (w: Node) => void = () => {};\n\n/**\n * The host is told about every watcher a write or mark reaches. It owns the\n * deduplication (a queued watcher is not queued again), so a watcher left\n * STALE by a failed check is delivered again by the next push.\n */\nexport function onWake(fn: (w: Node) => void): void {\n wake = fn;\n}\n\n/** Nothing running, the work stack empty (tests). */\nexport function idle(): boolean {\n return active === null && top === 0;\n}\n\n/**\n * Deepest work-stack top reached so far (research). Walks null the slots\n * they pop and only `trim` shrinks the array, so the high-water mark is its\n * length or the longest length `trim` cut.\n */\nexport function stackHighWater(): number {\n return stack.length > peak ? stack.length : peak;\n}\n\n/** Slots the work stack holds now (tests). */\nexport function stackLength(): number {\n return stack.length;\n}\n\n/**\n * At rest, cut a stack that a deep walk grew past TRIM back to KEEP slots,\n * so one deep walk does not hold its depth in memory for good. Setting the\n * length right-trims V8's backing store in place: no allocation, and the\n * array stays packed.\n *\n * Only the end of an outermost validation calls it (top 0), whichever walk\n * grew the stack. A deep mark walk runs through computeds that are STALE\n * afterwards, so their next demand is such a validation; `write` and `mark`\n * stay as they were. The guard costs one length load and one compare per\n * validation, nothing per node.\n */\nfunction trim(): void {\n if (__STATS__ && stack.length > peak) peak = stack.length;\n\n stack.length = KEEP;\n}\n\n// ------------------------------------------------------------------- write\n\nexport function write(s: Node, value: unknown): void {\n if (Object.is(s.value, value)) return;\n\n s.value = value;\n s.ver = (s.ver + 1) & MASK;\n\n for (let e = s.subs; e !== null; e = e.nextSub) {\n const n = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n\n if ((f & Flag.Busy) !== 0) {\n // Running: it may not have read s yet, so versions decide.\n if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n if ((f & Flag.Watch) !== 0) wake(n);\n else if (n.subs !== null) mark(n.subs);\n }\n } else if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Dirty | Flag.Woken;\n\n wake(n);\n } else if ((f & Flag.Stale) !== 0) {\n n.flags = f | Flag.Dirty;\n } else {\n n.flags = f | Flag.Dirty | Flag.Stale;\n\n if (n.subs !== null) mark(n.subs);\n }\n }\n}\n\n/** Mark everything below the subscriber list `first` STALE, depth first. */\nfunction mark(first: Edge): void {\n const base = top;\n let e: Edge | null = first;\n\n for (;;) {\n while (e !== null) {\n const n: Node = e.to;\n const f = n.flags;\n\n if (__STATS__) stats.marks++;\n // A watcher has no subscribers; the host deduplicates, so it is always told.\n if ((f & Flag.Watch) !== 0) {\n n.flags = f | Flag.Stale;\n wake(n);\n } else if ((f & Flag.Stale) === 0) {\n n.flags = f | Flag.Stale;\n\n if (n.subs !== null) {\n if (e.nextSub !== null) stack[top++] = e.nextSub;\n\n e = n.subs;\n continue;\n }\n }\n e = e.nextSub;\n }\n\n if (top === base) return;\n\n e = stack[--top] as Edge;\n stack[top] = null;\n }\n}\n\n// -------------------------------------------------------------------- read\n\n/** Bring `n` up to date if it is a stale computed, then track it. */\nexport function read(n: Node): unknown {\n const value = peek(n);\n const c = active;\n\n if (c !== null) {\n if (__STATS__) stats.reads++;\n\n const last = c.last;\n const next = last === null ? c.deps : last.next;\n\n if (next !== null && next.from === n) {\n if (__STATS__) stats.hits++;\n\n c.last = next;\n next.ver = n.ver;\n } else miss(c, n, last, next);\n }\n return value;\n}\n\n/** `read` without its tracking half: bring `n` up to date, record no relation. */\nexport function peek(n: Node): unknown {\n if ((n.flags & (Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking)) !== 0)\n pull(n);\n\n return n.value;\n}\n\n/**\n * Pull a computed up to date. Existential, in read order: sources are\n * checked first to last and the first changed one makes the node run. A\n * STALE source is descended into (the stack keeps the edge to climb back\n * through); a DIRTY one runs. A throw leaves every node on the walk DIRTY.\n * A node whose run is on the stack is a cycle. A development build also\n * diagnoses a node whose validation is on the stack (CHECKING): C1 is a\n * precondition, so its committed value never answers such a demand there.\n */\nfunction pull(root: Node): void {\n const f = root.flags;\n\n if ((f & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((f & Flag.Dirty) !== 0) return exec(root);\n\n verify(root, f);\n}\n\n/**\n * The walk of `pull` for a STALE node that is not DIRTY, BUSY or CHECKING;\n * kept apart so `pull` stays small. A node disposed during the walk is not\n * run (W3), nor left DIRTY.\n *\n * Development builds diagnose C1: a node on the walk is CHECKING until it is\n * clean or runs, and a source on the stack (BUSY or CHECKING) is a cycle\n * even when no push marked it again, since its version is not yet a fact.\n * CHECKING is set by the store that clears STALE and cleared by `exec` as it\n * sets BUSY; only a node found clean pays a store of its own. Production\n * builds compile all of it away.\n */\nfunction verify(root: Node, f: Flags): void {\n root.flags = __DEV__ ? (f & ~Flag.Stale) | Flag.Checking : f & ~Flag.Stale;\n const base = top;\n let n = root;\n let e = root.deps;\n\n try {\n for (;;) {\n settled: {\n while (e !== null) {\n const s: Node = e.from;\n const sf = s.flags;\n\n if (__STATS__) stats.checks++;\n\n if (\n (sf &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0\n ) {\n if ((sf & (Flag.Busy | Flag.Checking)) !== 0)\n throw new Error(\"Cycle detected\");\n\n if ((sf & Flag.Dirty) === 0) {\n s.flags = __DEV__\n ? (sf & ~Flag.Stale) | Flag.Checking\n : sf & ~Flag.Stale;\n stack[top++] = e;\n n = s;\n e = s.deps;\n continue;\n }\n exec(s);\n }\n\n if (s.ver !== e.ver) {\n if (n.fn !== null) exec(n);\n break settled;\n }\n e = e.next;\n }\n // Every source unchanged: clean, and no longer on the walk.\n if (__DEV__) n.flags &= ~Flag.Checking;\n }\n\n // Climb; a parent whose child changed runs as well.\n for (;;) {\n if (top === base) {\n if (stack.length > TRIM && base === 0) trim();\n\n return;\n }\n\n const up = stack[--top] as Edge;\n stack[top] = null;\n n = up.to;\n\n if (up.from.ver === up.ver) {\n e = up.next;\n break;\n }\n\n if (n.fn !== null) exec(n);\n }\n }\n } catch (error) {\n if (n.fn !== null)\n n.flags = __DEV__\n ? (n.flags | Flag.Dirty) & ~Flag.Checking\n : n.flags | Flag.Dirty;\n\n while (top > base) {\n const p = (stack[--top] as Edge).to;\n\n if (p.fn !== null)\n p.flags = __DEV__\n ? (p.flags | Flag.Dirty) & ~Flag.Checking\n : p.flags | Flag.Dirty;\n stack[top] = null;\n }\n throw error;\n }\n}\n\n/**\n * Universal check of a watcher: pull every committed source, so a throwing\n * one surfaces before any irreversible step. Marks DIRTY on change. Tentative\n * edges (reads of a failed run) only wake; they exist only while a run is\n * owed, and then versions need no comparison.\n */\nexport function check(w: Node): void {\n const tentative = (w.gen + 1) & MASK;\n let e = w.deps;\n\n // Until a run is owed, versions decide; then only the pulls remain. A\n // development build pulls a source on the stack too, so the C1 diagnosis\n // fails here, before the cleanup (P4).\n if ((w.flags & Flag.Dirty) === 0) {\n for (; e !== null; e = e.next) {\n const s = e.from;\n\n if (__STATS__) stats.checks++;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n\n if (s.ver !== e.ver) {\n w.flags |= Flag.Dirty;\n e = e.next;\n break;\n }\n }\n }\n\n for (; e !== null; e = e.next) {\n if (__STATS__) stats.checks++;\n\n const s = e.from;\n\n if (\n (s.flags &\n (__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Busy | Flag.Checking\n : Flag.Stale | Flag.Dirty)) !==\n 0 &&\n e.gen !== tentative\n )\n pull(s);\n }\n}\n\nexport function untracked<T>(fn: () => T): T {\n const prev = active;\n active = null;\n\n try {\n return fn();\n } finally {\n active = prev;\n }\n}\n\n// --------------------------------------------------------------------- run\n\n/** Run `n.fn` as a tracking pass; commit value and edges on success. */\nexport function exec(n: Node): void {\n const prev = active;\n const base = top;\n\n if (__STATS__) stats.runs++;\n // Cleared before the run: a push during it marks the node again. In a\n // development build a computed's validation ends here, BUSY taking over\n // from CHECKING; on a watcher the bit is WOKEN, which this run discharges.\n n.flags =\n (n.flags &\n ~(__DEV__\n ? Flag.Stale | Flag.Dirty | Flag.Checking\n : Flag.Stale | Flag.Dirty)) |\n Flag.Busy;\n n.last = null;\n active = n;\n let value: unknown;\n\n try {\n value = n.fn!(n.value);\n } catch (error) {\n active = prev;\n fail(n, base);\n throw error;\n }\n active = prev;\n const f = n.flags;\n\n if ((f & Flag.Indexed) !== 0) unindex(n, base);\n\n n.flags = f & ~(Flag.Busy | Flag.Indexed | Flag.Marked);\n\n if (n.fn === null) return detach(n);\n // Commit only after a successful run. Dropping the unread suffix removes\n // sources that this pass no longer depends on.\n const last = n.last as Edge | null;\n\n if ((last === null ? n.deps : last.next) !== null) cut(n, last);\n\n n.gen = (n.gen + 1) & MASK;\n\n // Every edge is committed now; see SETTLE.\n if ((n.gen & SETTLE) === 0) settle(n);\n\n if (Object.is(value, n.value) === false) {\n n.value = value;\n n.ver = (n.ver + 1) & MASK;\n }\n}\n\n/**\n * A failed run: keep committed edges and this run's reads as the wake set,\n * drop unread edges earlier failed runs left behind, owe a run.\n */\nfunction fail(n: Node, base: number): void {\n if ((n.flags & Flag.Indexed) !== 0) unindex(n, base);\n\n n.flags &= ~(Flag.Busy | Flag.Indexed | Flag.Marked);\n\n if (n.fn === null) return detach(n);\n\n n.flags |= Flag.Dirty;\n const tentative = (n.gen + 1) & MASK;\n const last = n.last;\n\n for (let e = last === null ? n.deps : last.next; e !== null;) {\n const after: Edge | null = e.next;\n\n if (e.gen === tentative) {\n const before = e.prev;\n\n if (before !== null) before.next = after;\n else n.deps = after;\n\n if (after !== null) after.prev = before;\n\n unsub(e);\n e.prev = null;\n e.next = null;\n }\n e = after;\n }\n}\n\n/** Detach `n` from its sources for good; inside its own run, at its end. */\nexport function dispose(n: Node): void {\n if (n.fn === null) return;\n\n n.fn = null;\n\n if ((n.flags & Flag.Busy) === 0) detach(n);\n}\n\nfunction detach(n: Node): void {\n cut(n, null);\n n.flags &= ~(Flag.Stale | Flag.Dirty | Flag.Woken);\n}\n\n// ---------------------------------------------------------------- tracking\n\n/** Put every edge of `c` into its source's slot; mark the unread ones. */\nexport function index(c: Node, next: Edge | null): void {\n if (__STATS__) stats.indexes++;\n\n c.flags |= Flag.Indexed;\n let unread = false;\n\n for (let e = c.deps; e !== null; e = e.next) {\n if (__STATS__) stats.indexed++;\n\n if (e === next) unread = true;\n\n if (unread) e.ver = UNREAD;\n\n claim(e.from, e);\n }\n}\n\n/** Point the source's slot at `e`, keeping a displaced cell (an enclosing run's) to give back. */\nexport function claim(s: Node, e: Edge): void {\n if (s.slot !== null) {\n stack[top++] = s;\n stack[top++] = s.slot;\n }\n s.slot = e;\n}\n\n/** Clear the run's slots, give back the cells it displaced. */\nfunction unindex(n: Node, base: number): void {\n for (let e = n.deps; e !== null; e = e.next) e.from.slot = null;\n\n while (top > base) {\n const cell = stack[--top] as Edge;\n stack[top] = null;\n const s = stack[--top] as Node;\n stack[top] = null;\n s.slot = cell;\n }\n}\n\n/** A new edge at the cursor, appended to the source's subscribers. */\nexport function link(\n c: Node,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (__STATS__) stats.links++;\n\n const tail = s.lastSub;\n const e: Edge = {\n from: s,\n to: c,\n ver: s.ver,\n gen: (c.gen + 1) & MASK,\n prev: last,\n next,\n prevSub: tail,\n nextSub: null,\n };\n\n if (last !== null) last.next = e;\n else c.deps = e;\n\n if (next !== null) next.prev = e;\n\n if (tail !== null) tail.nextSub = e;\n else s.subs = e;\n s.lastSub = e;\n c.last = e;\n}\n\n// Exact dependency tracking: resolve a read outside the stable cursor path.\n// Proofs come cheapest first: the cursor (in `read`), the previous read, a\n// source nobody reads, bounded scans of this reader's list, then the source's\n// first subscribers. Only a source with more subscribers than the budget needs\n// the slot index. Slot walks and edge creation remain in the kernel because\n// they mutate its shared stack.\n\n/**\n * A read that is not the next edge in read order. Every answer is exact. A\n * repeated read keeps the version of the pass's first observation (P5): a\n * change between the two reads stays visible to the next validation.\n */\nfunction miss(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (last !== null && last.from === s) {\n if (__STATS__) stats.repeats++;\n\n return;\n }\n\n const f = c.flags;\n\n if ((f & Flag.Indexed) !== 0) return slot(c, s, last, next);\n /** The pair's edge through the source's subscribers; undefined: unknown. */\n let found: Edge | null | undefined;\n\n if ((f & Flag.Marked) === 0) {\n // A source without subscribers is absent.\n if (s.subs === null) return link(c, s, last, next);\n // An edge found by a scan is the pair's only one; absence needs one side\n // scanned completely (this reader's list, or the source's subscribers).\n let e = next !== null ? next.next : null;\n let budget = SCAN;\n\n while (e !== null && e.from !== s && --budget !== 0) e = e.next;\n\n if (e !== null && e.from === s) {\n if (__STATS__) stats.scans++;\n\n e.ver = s.ver;\n return move(c, e, last, next!);\n }\n let p = last !== null ? last.prev : null;\n budget = SCAN;\n\n while (p !== null && p.from !== s && --budget !== 0) p = p.prev;\n\n if (p !== null && p.from === s) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n\n // Absent if this reader's whole list was scanned; otherwise the source's\n // first subscribers decide. Found as the first one: a lookup through the\n // subscribers costs no load beyond the read itself, so mark the unread\n // suffix and let the rest of the pass find its pairs that way, with no\n // slot to fill or clear. Found deeper: every lookup would walk other\n // readers' edges, so index; either way the found edge is taken at once.\n found = e === null && p === null ? null : pair(s, c);\n\n if (found !== null && found !== undefined) begin(c, s, found, next);\n } else found = pair(s, c);\n\n if (found === null) return link(c, s, last, next);\n\n if (found !== undefined) return take(c, found, s, last, next);\n // Past the budget: index the pass, and the slot decides from here on.\n index(c, next);\n slot(c, s, last, next);\n}\n\n/** An indexed pass's lookup: the source's slot holds the pair's edge, or the pair is absent. */\nfunction slot(c: Node, s: Node, last: Edge | null, next: Edge | null): void {\n if (__STATS__) stats.lookups++;\n\n const cell = s.slot;\n\n if (cell !== null && cell.to === c) return take(c, cell, s, last, next);\n link(c, s, last, next);\n claim(s, c.last as Edge);\n}\n\n/**\n * The pair's edge among the source's first SCAN subscribers: null if the list\n * ends first (absent), undefined if the budget runs out (unknown).\n */\nfunction pair(s: Node, c: Node): Edge | null | undefined {\n if (__STATS__) stats.lookups++;\n\n let budget = SCAN;\n\n for (let e = s.subs; e !== null; e = e.nextSub) {\n if (e.to === c) return e;\n\n if (--budget === 0) return undefined;\n }\n return null;\n}\n\n/**\n * The pass's lookup from here on, decided by the found edge's place: the\n * source's first subscriber marks the unread suffix, from `next` on (a walk\n * over edges, not sources); deeper, index.\n */\nfunction begin(c: Node, s: Node, e: Edge, next: Edge | null): void {\n if (e !== s.subs) return index(c, next);\n\n if (__STATS__) stats.sweeps++;\n\n c.flags |= Flag.Marked;\n\n for (let u = next; u !== null; u = u.next) {\n if (__STATS__) stats.indexed++;\n\n u.ver = UNREAD;\n }\n}\n\n/**\n * A found edge: an unread one is observed and moves to the cursor; a repeated\n * read keeps its place and its first observation's version (P5).\n */\nfunction take(\n c: Node,\n e: Edge,\n s: Node,\n last: Edge | null,\n next: Edge | null,\n): void {\n if (e.ver !== UNREAD) {\n if (__STATS__) stats.dups++;\n\n return;\n }\n if (__STATS__) stats.moves++;\n\n e.ver = s.ver;\n move(c, e, last, next!);\n}\n", "// Policy around the kernel: the watcher lifecycle (check, consume the\n// cleanup, run), a FIFO queue for scheduled watchers, and batching.\nimport { Flag } from \"./graph\";\nimport type { Cleanup, Fn, Node } from \"./graph\";\nimport { check, dispose, exec, node, onWake, untracked } from \"./kernel\";\n\nconst queue: (Node | null)[] = [];\n// The host's registers. Exported so a write's caller can test for work\n// without a call into the host; ES bindings are read-only to importers, so\n// only this module writes them.\n/** Queued watchers. */\nexport let size = 0;\n/** Open batches. */\nexport let depth = 0;\n/** A drain loop is open. */\nexport let draining = false;\n\n/** Queue a scheduled watcher once; the kernel calls this when it wakes one. */\nexport function wake(w: Node): void {\n if ((w.flags & (Flag.Auto | Flag.Queued)) !== Flag.Auto) return;\n\n w.flags |= Flag.Queued;\n queue[size++] = w;\n}\n\nonWake(wake);\n\n/** A watcher runs `fn` and keeps a returned function as its cleanup. */\nexport function watcher(fn: Fn, auto: boolean): Node {\n return node(\n fn,\n undefined,\n Flag.Watch | Flag.Stale | Flag.Dirty | (auto ? Flag.Auto : 0),\n );\n}\n\n/**\n * Check every committed source unless only signals woke it, then, if a run\n * is owed, consume the previous cleanup and run. A watcher neither STALE,\n * WOKEN is clean, or cold after a failure until a source changes. A failed\n * check leaves it STALE, so the next run checks again (V1) and the next push\n * finds it queued or delivers it again.\n * Precondition: its own run is not on the stack (flush is not reentrant).\n */\nexport function run(w: Node): void {\n const flags = w.flags;\n\n if (w.fn === null || (flags & (Flag.Stale | Flag.Woken)) === 0) return;\n\n w.flags = flags & ~(Flag.Stale | Flag.Woken);\n\n if ((flags & Flag.Stale) !== 0) {\n try {\n check(w);\n } catch (error) {\n w.flags |= Flag.Stale;\n throw error;\n }\n }\n\n if ((w.flags & Flag.Dirty) === 0 || w.fn === null) return;\n\n const cleanup = w.value;\n\n if (typeof cleanup === \"function\") {\n w.value = undefined;\n untracked(cleanup as Cleanup);\n\n if (w.fn === null) return;\n }\n exec(w);\n}\n\n/** Terminal: detach, then run the last cleanup outside tracking. */\nexport function stop(w: Node): void {\n if (w.fn === null) return;\n\n const cleanup = w.value;\n w.value = undefined;\n dispose(w);\n\n if (typeof cleanup === \"function\") untracked(cleanup as Cleanup);\n}\n\n/** Run queued watchers until the queue drains; rethrow the first error. */\nexport function flush(): void {\n if (size === 0 || draining || depth !== 0) return;\n\n drainNow();\n}\n\n/**\n * `flush` with its guard at the call site: a write's caller tests\n * `size !== 0 && !draining && depth === 0` first (the queue first, so a write\n * that woke nothing costs one load), and calls this only when there is work.\n */\nexport function drainNow(): void {\n draining = true;\n drain(false, undefined);\n}\n\n/**\n * A new scheduled watcher's first run. With no drain open, no batch and an\n * empty queue this is `wake(w); flush()` with the queue slot skipped: the\n * drain opens on `w` (so its run stays inside the drain loop, LOG 11), and a\n * fresh watcher has no sources to check and no cleanup, so `run` is `exec`.\n */\nexport function start(w: Node): void {\n if (draining || depth !== 0 || size !== 0) {\n wake(w);\n return flush();\n }\n draining = true;\n\n try {\n exec(w);\n } catch (error) {\n return drain(true, error);\n }\n drain(false, undefined);\n}\n\n/** The drain loop, opened by the caller: FIFO, the first error rethrown at the end. */\nfunction drain(failed: boolean, first: unknown): void {\n for (let i = 0; i < size; i++) {\n const queuedWatcher = queue[i]!;\n queue[i] = null;\n queuedWatcher.flags &= ~Flag.Queued;\n\n try {\n run(queuedWatcher);\n } catch (error) {\n if (!failed) first = error;\n\n failed = true;\n }\n }\n size = 0;\n draining = false;\n\n if (failed) throw first;\n}\n\nexport function batch<T>(fn: () => T): T {\n depth++;\n\n try {\n return fn();\n } finally {\n if (--depth === 0) flush();\n }\n}\n"],
|
|
5
|
+
"mappings": ";mbAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,WAAAE,GAAA,aAAAC,GAAA,YAAAC,GAAA,WAAAC,GAAA,QAAAC,GAAA,SAAAC,GAAA,QAAAC,GAAA,WAAAC,GAAA,cAAAC,EAAA,WAAAC,KAAA,eAAAC,GAAAZ,ICkFO,IAAMa,EAAS,GAETC,EAAO,WAOPC,EAAS,UAETC,EAAO,EAMPC,EAAO,IACPC,EAAO,IAIb,SAASC,EAAKC,EAAgBC,EAAgBC,EAAoB,CACvE,MAAO,CACL,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AgB,CACvCN,IAEA,GAAI,CACF,OAAOM,EAAG,CACZ,QAAE,CACI,EAAEN,IAAU,GAAGkB,EAAM,CAC3B,CACF,CH1GO,SAASS,GAAUC,EAAqB,CAC7C,OAAOC,EAAK,KAAMD,EAAO,CAAC,CAC5B,CAUO,SAASE,GACdC,EACAC,EACa,CACb,OAAOH,EAAKE,EAAUC,GAAmB,CAC3C,CAGO,SAASC,GAAOF,EAAuC,CAC5D,IAAMG,EAAaC,EAAQJ,EAAI,EAAI,EAEnC,GAAI,CACFK,EAAMF,CAAU,CAClB,OAASG,EAAO,CACd,MAAAC,EAAKJ,CAAU,EACTG,CACR,CAEA,OAAOH,CACT,CAOO,IAAMK,GAAqCC,EAGrCC,GAAsCA,EAG5C,SAASC,GAAOC,EAAmBf,EAAgB,CACxDgB,EAAMD,EAA2Bf,CAAK,EAElCiB,IAAS,GAAK,CAACC,GAAYC,IAAU,GAAGC,EAAS,CACvD,CAOO,SAASC,GAAUN,EAAmBZ,EAA2B,CACtEW,GACEC,EACAO,EAAU,IAAMnB,EAAIY,EAA2B,KAAU,CAAC,CAC5D,CACF,CAEO,SAASQ,GAAQC,EAA0C,CAChE,IAAMC,EAAaD,GAEdC,EAAW,MAAQ,MAAgB,EAAGf,EAAKe,CAAU,EACrDF,EAAOE,CAAU,CACxB",
|
|
6
|
+
"names": ["index_exports", "__export", "batch", "computed", "dispose", "effect", "get", "peek", "set", "signal", "untracked", "update", "__toCommonJS", "UNREAD", "MASK", "SETTLE", "SCAN", "KEEP", "TRIM", "node", "fn", "value", "flags", "move", "c", "e", "last", "next", "before", "after", "cut", "unsub", "settle", "gen", "s", "active", "stack", "top", "wake", "onWake", "fn", "trim", "stack", "KEEP", "write", "s", "value", "MASK", "e", "n", "f", "wake", "mark", "first", "base", "top", "read", "peek", "c", "active", "last", "next", "miss", "pull", "root", "exec", "verify", "settled", "sf", "TRIM", "up", "error", "p", "check", "w", "tentative", "untracked", "fn", "prev", "fail", "unindex", "detach", "cut", "SETTLE", "settle", "after", "before", "unsub", "dispose", "index", "unread", "UNREAD", "claim", "cell", "link", "tail", "slot", "found", "budget", "SCAN", "move", "pair", "begin", "take", "u", "queue", "size", "depth", "draining", "wake", "w", "onWake", "watcher", "fn", "auto", "node", "run", "flags", "check", "error", "cleanup", "untracked", "exec", "stop", "dispose", "flush", "drainNow", "drain", "start", "failed", "first", "i", "queuedWatcher", "batch", "signal", "value", "node", "computed", "fn", "initial", "effect", "effectNode", "watcher", "start", "error", "stop", "get", "read", "peek", "set", "source", "write", "size", "draining", "depth", "drainNow", "update", "untracked", "dispose", "target", "targetNode"]
|
|
7
7
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,23 +1,47 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
declare const
|
|
1
|
+
export declare function batch<T>(fn: () => T): T;
|
|
2
|
+
export declare function untracked<T>(fn: () => T): T;
|
|
3
|
+
declare const READ: unique symbol;
|
|
4
|
+
declare const WRITE: unique symbol;
|
|
4
5
|
declare const COMPUTED: unique symbol;
|
|
5
6
|
declare const EFFECT: unique symbol;
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
/** Anything `get` accepts. */
|
|
8
|
+
export interface Readable<out T> {
|
|
9
|
+
readonly [READ]: T;
|
|
8
10
|
}
|
|
9
|
-
export interface
|
|
10
|
-
readonly [
|
|
11
|
+
export interface Signal<in out T> extends Readable<T> {
|
|
12
|
+
readonly [WRITE]: (value: T) => void;
|
|
13
|
+
}
|
|
14
|
+
export interface Computed<out T> extends Readable<T> {
|
|
15
|
+
readonly [COMPUTED]: true;
|
|
11
16
|
}
|
|
12
17
|
export interface Effect {
|
|
13
18
|
readonly [EFFECT]: true;
|
|
14
19
|
}
|
|
15
|
-
export type Readable<T> = Signal<T> | Computed<T>;
|
|
16
20
|
export declare function signal<T>(value: T): Signal<T>;
|
|
21
|
+
/**
|
|
22
|
+
* `fn` receives `prev`, the last successfully committed value: `initial`
|
|
23
|
+
* (or undefined) before the first commit, unchanged by a failed run.
|
|
24
|
+
* Returning `prev` itself is a cutoff: consumers do not rerun, so custom
|
|
25
|
+
* equality is `(prev) => (equal(prev, next) ? prev : next)`.
|
|
26
|
+
*/
|
|
17
27
|
export declare function computed<T>(fn: (prev: T | undefined) => T): Computed<T>;
|
|
28
|
+
export declare function computed<T>(fn: (prev: T) => T, initial: T): Computed<T>;
|
|
18
29
|
/** Runs now (or when the enclosing batch or drain ends), then on every change it read. */
|
|
19
30
|
export declare function effect(fn: () => void | (() => void)): Effect;
|
|
20
|
-
/**
|
|
21
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Kernel `read` itself under the public type: a signal's flags are 0, so one
|
|
33
|
+
* read path serves signals and computeds, and no surface function stands in
|
|
34
|
+
* front of it.
|
|
35
|
+
*/
|
|
36
|
+
export declare const get: <T>(source: Readable<T>) => T;
|
|
37
|
+
/** `get` without observation: up to date, but the running consumer does not depend on it. */
|
|
38
|
+
export declare const peek: <T>(source: Readable<T>) => T;
|
|
39
|
+
/** Writes, then runs what the write scheduled; a write that woke nothing stops at one load. */
|
|
22
40
|
export declare function set<T>(source: Signal<T>, value: T): void;
|
|
41
|
+
/**
|
|
42
|
+
* `set(source, fn(current))`. An update is a write, not an observation:
|
|
43
|
+
* `fn` runs untracked, so neither `source` nor what `fn` reads becomes a
|
|
44
|
+
* dependency of the running consumer. If `fn` throws, nothing is written.
|
|
45
|
+
*/
|
|
46
|
+
export declare function update<T>(source: Signal<T>, fn: (value: T) => T): void;
|
|
23
47
|
export declare function dispose(target: Computed<unknown> | Effect): void;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,47 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
declare const
|
|
1
|
+
export declare function batch<T>(fn: () => T): T;
|
|
2
|
+
export declare function untracked<T>(fn: () => T): T;
|
|
3
|
+
declare const READ: unique symbol;
|
|
4
|
+
declare const WRITE: unique symbol;
|
|
4
5
|
declare const COMPUTED: unique symbol;
|
|
5
6
|
declare const EFFECT: unique symbol;
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
/** Anything `get` accepts. */
|
|
8
|
+
export interface Readable<out T> {
|
|
9
|
+
readonly [READ]: T;
|
|
8
10
|
}
|
|
9
|
-
export interface
|
|
10
|
-
readonly [
|
|
11
|
+
export interface Signal<in out T> extends Readable<T> {
|
|
12
|
+
readonly [WRITE]: (value: T) => void;
|
|
13
|
+
}
|
|
14
|
+
export interface Computed<out T> extends Readable<T> {
|
|
15
|
+
readonly [COMPUTED]: true;
|
|
11
16
|
}
|
|
12
17
|
export interface Effect {
|
|
13
18
|
readonly [EFFECT]: true;
|
|
14
19
|
}
|
|
15
|
-
export type Readable<T> = Signal<T> | Computed<T>;
|
|
16
20
|
export declare function signal<T>(value: T): Signal<T>;
|
|
21
|
+
/**
|
|
22
|
+
* `fn` receives `prev`, the last successfully committed value: `initial`
|
|
23
|
+
* (or undefined) before the first commit, unchanged by a failed run.
|
|
24
|
+
* Returning `prev` itself is a cutoff: consumers do not rerun, so custom
|
|
25
|
+
* equality is `(prev) => (equal(prev, next) ? prev : next)`.
|
|
26
|
+
*/
|
|
17
27
|
export declare function computed<T>(fn: (prev: T | undefined) => T): Computed<T>;
|
|
28
|
+
export declare function computed<T>(fn: (prev: T) => T, initial: T): Computed<T>;
|
|
18
29
|
/** Runs now (or when the enclosing batch or drain ends), then on every change it read. */
|
|
19
30
|
export declare function effect(fn: () => void | (() => void)): Effect;
|
|
20
|
-
/**
|
|
21
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Kernel `read` itself under the public type: a signal's flags are 0, so one
|
|
33
|
+
* read path serves signals and computeds, and no surface function stands in
|
|
34
|
+
* front of it.
|
|
35
|
+
*/
|
|
36
|
+
export declare const get: <T>(source: Readable<T>) => T;
|
|
37
|
+
/** `get` without observation: up to date, but the running consumer does not depend on it. */
|
|
38
|
+
export declare const peek: <T>(source: Readable<T>) => T;
|
|
39
|
+
/** Writes, then runs what the write scheduled; a write that woke nothing stops at one load. */
|
|
22
40
|
export declare function set<T>(source: Signal<T>, value: T): void;
|
|
41
|
+
/**
|
|
42
|
+
* `set(source, fn(current))`. An update is a write, not an observation:
|
|
43
|
+
* `fn` runs untracked, so neither `source` nor what `fn` reads becomes a
|
|
44
|
+
* dependency of the running consumer. If `fn` throws, nothing is written.
|
|
45
|
+
*/
|
|
46
|
+
export declare function update<T>(source: Signal<T>, fn: (value: T) => T): void;
|
|
23
47
|
export declare function dispose(target: Computed<unknown> | Effect): void;
|