@solidjs/signals 2.0.0-rc.7 → 2.0.0-rc.9

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.
Files changed (134) hide show
  1. package/dist/dev-shared.js +6357 -0
  2. package/dist/dev.attribution.js +2485 -0
  3. package/dist/dev.js +2851 -7027
  4. package/dist/observe/affects.js +129 -0
  5. package/dist/observe/attribution.js +7 -0
  6. package/dist/observe/boundaries.js +641 -0
  7. package/dist/observe/core/action.js +177 -0
  8. package/dist/observe/core/async.js +846 -0
  9. package/dist/observe/core/attribution-costs.js +66 -0
  10. package/dist/observe/core/attribution-feedback.js +282 -0
  11. package/dist/observe/core/attribution-hooks.js +82 -0
  12. package/dist/observe/core/attribution-queries.js +28 -0
  13. package/dist/observe/core/attribution.js +2095 -0
  14. package/dist/observe/core/constants.js +248 -0
  15. package/dist/{types-cjs/core/context.d.cts → observe/core/context.js} +37 -12
  16. package/dist/observe/core/core.js +1789 -0
  17. package/dist/observe/core/dev.js +293 -0
  18. package/dist/observe/core/effect.js +195 -0
  19. package/dist/observe/core/error-hooks.js +71 -0
  20. package/dist/observe/core/error.js +80 -0
  21. package/dist/observe/core/external.js +98 -0
  22. package/dist/observe/core/graph.js +155 -0
  23. package/dist/observe/core/heap.js +147 -0
  24. package/dist/observe/core/invariants.js +45 -0
  25. package/dist/observe/core/lanes.js +205 -0
  26. package/dist/observe/core/optimistic.js +585 -0
  27. package/dist/observe/core/owner.js +322 -0
  28. package/dist/observe/core/scheduler.js +1428 -0
  29. package/dist/observe/core/verdict.js +669 -0
  30. package/dist/observe/index.js +59 -0
  31. package/dist/observe/map.js +484 -0
  32. package/dist/{types-cjs/signals.d.cts → observe/signals.js} +311 -317
  33. package/dist/observe/store/index.js +40 -0
  34. package/dist/observe/store/next/optimistic.js +660 -0
  35. package/dist/observe/store/next/projection.js +255 -0
  36. package/dist/observe/store/next/reconcile.js +375 -0
  37. package/dist/observe/store/next/store.js +2327 -0
  38. package/dist/observe/store/next/target.js +51 -0
  39. package/dist/observe/store/store.js +325 -0
  40. package/dist/observe/store/storePath.js +103 -0
  41. package/dist/observe/store/utils.js +1023 -0
  42. package/dist/prod/attribution.js +41 -0
  43. package/dist/prod/boundaries.js +128 -76
  44. package/dist/prod/core/action.js +65 -37
  45. package/dist/prod/core/async.js +324 -172
  46. package/dist/prod/core/constants.js +57 -1
  47. package/dist/prod/core/context.js +3 -3
  48. package/dist/prod/core/core.js +1003 -364
  49. package/dist/prod/core/dev.js +17 -1
  50. package/dist/prod/core/effect.js +76 -42
  51. package/dist/prod/core/error-hooks.js +71 -0
  52. package/dist/prod/core/external.js +4 -4
  53. package/dist/prod/core/graph.js +37 -37
  54. package/dist/prod/core/heap.js +61 -56
  55. package/dist/prod/core/lanes.js +107 -63
  56. package/dist/prod/core/optimistic.js +374 -93
  57. package/dist/prod/core/owner.js +62 -46
  58. package/dist/prod/core/scheduler.js +653 -286
  59. package/dist/prod/core/verdict.js +245 -127
  60. package/dist/prod/index.js +15 -5
  61. package/dist/prod/map.js +292 -141
  62. package/dist/prod/signals.js +29 -16
  63. package/dist/prod/store/next/optimistic.js +77 -55
  64. package/dist/prod/store/next/projection.js +31 -20
  65. package/dist/prod/store/next/reconcile.js +87 -84
  66. package/dist/prod/store/next/store.js +718 -401
  67. package/dist/prod/store/next/target.js +32 -10
  68. package/dist/prod/store/store.js +12 -14
  69. package/dist/prod/store/utils.js +950 -130
  70. package/dist/types/attribution.d.ts +20 -0
  71. package/dist/types/attribution.prod.d.ts +22 -0
  72. package/dist/types/boundaries.d.ts +10 -1
  73. package/dist/types/core/action.d.ts +12 -5
  74. package/dist/types/core/async.d.ts +1 -1
  75. package/dist/types/core/attribution-costs.d.ts +35 -0
  76. package/dist/types/core/attribution-feedback.d.ts +133 -0
  77. package/dist/types/core/attribution-hooks.d.ts +147 -9
  78. package/dist/types/core/attribution-queries.d.ts +10 -0
  79. package/dist/types/core/attribution.d.ts +216 -182
  80. package/dist/types/core/constants.d.ts +56 -0
  81. package/dist/types/core/core.d.ts +190 -2
  82. package/dist/types/core/dev.d.ts +246 -28
  83. package/dist/types/core/effect.d.ts +8 -1
  84. package/dist/types/core/error-hooks.d.ts +71 -0
  85. package/dist/types/core/index.d.ts +4 -1
  86. package/dist/types/core/invariants.d.ts +4 -0
  87. package/dist/types/core/lanes.d.ts +39 -5
  88. package/dist/types/core/scheduler.d.ts +153 -0
  89. package/dist/types/core/types.d.ts +32 -9
  90. package/dist/types/index.d.ts +10 -3
  91. package/dist/types/map.d.ts +2 -0
  92. package/dist/types/signals.d.ts +8 -0
  93. package/dist/types/store/index.d.ts +2 -0
  94. package/dist/types/store/next/optimistic.d.ts +1 -1
  95. package/dist/types/store/next/store.d.ts +11 -5
  96. package/dist/types/store/next/target.d.ts +37 -10
  97. package/dist/types/store/utils.d.ts +178 -0
  98. package/package.json +21 -17
  99. package/dist/node.cjs +0 -10541
  100. package/dist/node.dev.cjs +0 -13724
  101. package/dist/types-cjs/affects.d.cts +0 -47
  102. package/dist/types-cjs/boundaries.d.cts +0 -175
  103. package/dist/types-cjs/core/action.d.cts +0 -64
  104. package/dist/types-cjs/core/async.d.cts +0 -23
  105. package/dist/types-cjs/core/attribution-hooks.d.cts +0 -127
  106. package/dist/types-cjs/core/attribution.d.cts +0 -495
  107. package/dist/types-cjs/core/constants.d.cts +0 -159
  108. package/dist/types-cjs/core/core.d.cts +0 -185
  109. package/dist/types-cjs/core/dev.d.cts +0 -136
  110. package/dist/types-cjs/core/effect.d.cts +0 -30
  111. package/dist/types-cjs/core/error.d.cts +0 -56
  112. package/dist/types-cjs/core/external.d.cts +0 -15
  113. package/dist/types-cjs/core/graph.d.cts +0 -28
  114. package/dist/types-cjs/core/heap.d.cts +0 -24
  115. package/dist/types-cjs/core/index.d.cts +0 -13
  116. package/dist/types-cjs/core/invariants.d.cts +0 -59
  117. package/dist/types-cjs/core/lanes.d.cts +0 -54
  118. package/dist/types-cjs/core/optimistic.d.cts +0 -6
  119. package/dist/types-cjs/core/owner.d.cts +0 -124
  120. package/dist/types-cjs/core/scheduler.d.cts +0 -236
  121. package/dist/types-cjs/core/types.d.cts +0 -204
  122. package/dist/types-cjs/core/verdict.d.cts +0 -2
  123. package/dist/types-cjs/index.d.cts +0 -10
  124. package/dist/types-cjs/map.d.cts +0 -69
  125. package/dist/types-cjs/package.json +0 -3
  126. package/dist/types-cjs/store/index.d.cts +0 -18
  127. package/dist/types-cjs/store/next/optimistic.d.cts +0 -25
  128. package/dist/types-cjs/store/next/projection.d.cts +0 -8
  129. package/dist/types-cjs/store/next/reconcile.d.cts +0 -10
  130. package/dist/types-cjs/store/next/store.d.cts +0 -121
  131. package/dist/types-cjs/store/next/target.d.cts +0 -166
  132. package/dist/types-cjs/store/store.d.cts +0 -143
  133. package/dist/types-cjs/store/storePath.d.cts +0 -58
  134. package/dist/types-cjs/store/utils.d.cts +0 -74
@@ -0,0 +1,585 @@
1
+ import { NOT_PENDING, unwrapOverride, STATUS_UNINITIALIZED, REACTIVE_DIRTY, REACTIVE_CHECK, CONFIG_HAS_LANE, OVERRIDE_UNDEFINED, STATUS_PENDING, CONFIG_DERIVED_OVERRIDE, CONFIG_OPTIMISTIC, CONFIG_OVERRIDE_SUPERSEDED, EFFECT_RENDER, CONFIG_AUTHORITATIVE_OBSERVED, REACTIVE_MANUAL_WRITE, REACTIVE_OPTIMISTIC_DIRTY, LANE_RUN, EFFECT_USER } from "./constants.js";
2
+
3
+ import { attrHooks } from "./attribution-hooks.js";
4
+
5
+ import { ext, hasActiveOverride, stale, enterStagedRead, latestReadActive, currentOptimisticLane } from "./core.js";
6
+
7
+ import { NotReadyError } from "./error.js";
8
+
9
+ import "./invariants.js";
10
+
11
+ import { resolveTransition, getOrCreateLane, activeLanes, signalLanes, laneHeld, findLane, resolveLane, readsHeldCommitted, assignOrMergeLane } from "./lanes.js";
12
+
13
+ import { GlobalQueue, activeTransition, globalQueue, origin, clock, insertSubs, schedule, sourceObserved, currentTransition, queuePendingNode } from "./scheduler.js";
14
+
15
+ /**
16
+ * The optimistic write engine, moved out of core.ts/scheduler.ts. Everything
17
+ * here serves only optimistic overrides — createOptimistic,
18
+ * createOptimisticStore (and its store-node writes), and the verdict layer's
19
+ * companions (which are optimistic nodes). Modules that can create optimistic
20
+ * state call `installOptimisticEngine()` before creating it; apps that never
21
+ * import one of those APIs never retain any of this.
22
+ *
23
+ * Core call sites fire the hooks behind guards on state only this module can
24
+ * create (`_overrideValue !== undefined`, `currentOptimisticLane !== null`,
25
+ * `_optimisticNodes.length`, `activeLanes.size`), so `!` invocations are safe
26
+ * once the gate holds — the same late-binding contract as verdict.ts.
27
+ */
28
+ /** The optimistic half of setSignal, fired when `_overrideValue !== undefined`. */ function optimisticWrite(e, n) {
29
+ const i = e.o?.Ne !== NOT_PENDING;
30
+ const t = i ? unwrapOverride(e.o?.Ne) : e.Ae;
31
+ if (typeof n === "function") n = n(t);
32
+ const r = !!(e.S & STATUS_UNINITIALIZED) ||
33
+ // A dirty node's _value is stale (its queued recompute hasn't run — e.g.
34
+ // a latest() shadow marked by the previous landing's companion snap), so
35
+ // equality against it must not swallow the write. Without this, a sync
36
+ // push returning the shadow to that stale value was dropped, the snap
37
+ // recompute then committed the parent's old value, and the banner showed
38
+ // the previous transition's target (#3041 follow-up).
39
+ !!((e.ft ?? 0) & (REACTIVE_DIRTY | REACTIVE_CHECK)) || !e.Re || !e.Re(t, n);
40
+ if (!r) {
41
+ // Same-value write with an active override still entangles the current
42
+ // action's transition — the hold must outlast all overlapping actions —
43
+ // and renews the override's PROVENANCE: the newer action re-asks the
44
+ // question, so an older action's answer arriving later is stale to it
45
+ // too (#3331; a same-value re-prediction otherwise let the first
46
+ // action's slow source supersede and restart the downstream flight).
47
+ if (i) {
48
+ const n = resolveTransition(e);
49
+ if (n && activeTransition !== n) globalQueue.initTransition(n);
50
+ if (origin > e.o.zt) e.o.zt = origin;
51
+ }
52
+ return n;
53
+ }
54
+ if (i) globalQueue.initTransition(resolveTransition(e));
55
+ // No revert target is stashed: while the override is active every reader
56
+ // sees it (A17), so authoritative arrivals commit silently into _value and
57
+ // reverting is just dropping the override — _value is already correct.
58
+ else globalQueue.m.tn.push(e);
59
+ // Stamp ownership on the node (post-merge, so entangled writers share the
60
+ // joint root). resolveTransition prefers this over the lane's _transition,
61
+ // which a shared subscriber can merge across transactions (#2912).
62
+ ext(e).jt = activeTransition;
63
+ ext(e).Qt = clock;
64
+ // Provenance: the action asking. An answer an OLDER action's flight brings
65
+ // back is a stale question and holds silently to commit (#3331).
66
+ ext(e).zt = origin;
67
+ const u = getOrCreateLane(e);
68
+ ext(e).de = u;
69
+ // A fresh override re-masks: whatever truth is staged, this write is the
70
+ // value for the graph again until the source answers it (#3331).
71
+ e.C = (e.C | CONFIG_HAS_LANE) & -8912897;
72
+ // Literal undefined must not land raw: the slot doubles as the optimistic
73
+ // brand, and erasing it makes the write invisible and routes follow-up
74
+ // writes off the optimistic path into permanent commits (#2898).
75
+ ext(e).Ne = n === undefined ? OVERRIDE_UNDEFINED : n;
76
+ // syncCompanions only pokes _pendingSignal/_latestValueComputed — with
77
+ // neither companion present the call is a guaranteed no-op.
78
+ (e.o?.he !== undefined || e.o?.Ge !== undefined) && GlobalQueue.De !== null && GlobalQueue.De(e, n);
79
+ if (e.ht !== undefined) e.fe = clock;
80
+ // §12e: computed-only slot
81
+ insertSubs(e, true);
82
+ schedule();
83
+ return n;
84
+ }
85
+
86
+ /**
87
+ * Lanes stage (#3479): a lane pass's publish for a memo. An optimistic
88
+ * derivation is an override — the speculative result lives in the override
89
+ * slot, `_value` stays the committed truth. The whole optimistic frame is then
90
+ * in one place: the lane's readers and untracked reads see it (A17), a render
91
+ * effect off the held lane sees the committed frame whole (readsHeldCommitted,
92
+ * #3460) — the source's shadow AND its derivations — where a speculative
93
+ * `_value` beside a committed shadow tore it. The node joins the
94
+ * transaction's optimistic nodes on its first speculative publish; the revert
95
+ * drops the override and re-derives it from the truth (a derived override has
96
+ * no truth of its own — see resolveOptimisticNodes, endOptimism).
97
+ */ function laneOverride(e, n, i) {
98
+ // The wake-only channel (#3009, see recomputeLane): a plain write to a
99
+ // latest()-tracked source rides a companion-sourced lane with no
100
+ // transaction on either side only to wake the verdict companions. Nothing
101
+ // is speculative — the pass commits directly, as any plain write does.
102
+ i = findLane(i);
103
+ if (!i.Te && !activeTransition && i.Ln.o?.nn !== undefined) {
104
+ e.Ae = n;
105
+ return;
106
+ }
107
+ if (!hasActiveOverride(e)) {
108
+ // It reverts with the lane's transaction (a landing runs outside any
109
+ // flush, where the ambient batch would revert it at its own end); an
110
+ // orphan lane's falls to the batch, adopted with it (initTransition) as a
111
+ // write's is. No `_overrideOwner`: a derived override is a plain member,
112
+ // its transaction its lane's (resolveTransition), and it merges lanes
113
+ // through itself as any shared reader does (assignOrMergeLane). No
114
+ // provenance stamp either: not an intent, any truth supersedes it.
115
+ (i.Te ? currentTransition(i.Te) : globalQueue.m).tn.push(e);
116
+ }
117
+ // A lane pass's output is a derivation — also over a WRITTEN guess it
118
+ // corrects (a `createOptimistic(fn)` re-derived from fresh upstream data):
119
+ // the guess is gone, the slot holds fn's answer, and the revert promotes it
120
+ // rather than dropping to a stale `_value` and re-asking downstream (the
121
+ // next user write re-arms the guess: optimisticWrite clears the bit). No
122
+ // `_overrideTime` stamp: that marks a user WRITE unflushed until the flush
123
+ // that carries it (A28) and shields it from same-tick supersession — a pass's
124
+ // result is neither (a pulled ownerless memo publishes outside any flush).
125
+ // A fresh lane frame ends a supersession in force: the pass just dropped
126
+ // the staged truth it pointed at (recompute, INV-11 corollary) — left set,
127
+ // the flag served a `_value` never committed (fuzzer latest-1 #2481).
128
+ e.C = (e.C | CONFIG_DERIVED_OVERRIDE) & ~CONFIG_OVERRIDE_SUPERSEDED;
129
+ e.o.Ne = n === undefined ? OVERRIDE_UNDEFINED : n;
130
+ }
131
+
132
+ /**
133
+ * transitionComplete's override blockage: a settling transition stays open
134
+ * while one of its optimistic nodes holds an active override that is still
135
+ * pending on real (non-affects-sentinel) async. A derived override's flight is
136
+ * the lane's own work, never authoritative — it does not hold the settle.
137
+ * Neither is a companion's (#3494): the `latest()` shadow backfilled under the
138
+ * owner's transaction (A28 (3)) is an observation of the flight, and a
139
+ * mainline `latest(details)` after the flight's last reader unmounted held the
140
+ * released write until the orphaned request landed.
141
+ */ function transitionBlocked(e) {
142
+ for (let n = 0; n < e.tn.length; n++) {
143
+ const i = e.tn[n];
144
+ if (!(i.C & CONFIG_DERIVED_OVERRIDE) && i.o?.nn === undefined && hasActiveOverride(i) && "S" in i && i.S & STATUS_PENDING && i.o?._ instanceof NotReadyError) {
145
+ return true;
146
+ }
147
+ }
148
+ return false;
149
+ }
150
+
151
+ function resolveOptimisticNodes(e) {
152
+ // Settlement writes below (snapCompanionsToState → updatePendingSignal-style
153
+ // notifications) may push fresh optimistic nodes; only this batch settles
154
+ // now, so iterate a fixed window and splice it out at the end.
155
+ const n = e.length;
156
+ for (let i = 0; i < n; i++) {
157
+ const n = e[i];
158
+ if (n.o !== null) n.o.de = undefined;
159
+ // Revert is a pure drop: there is no revert target to commit —
160
+ // override-covered authoritative values hold in _pendingValue and
161
+ // elevate on their OWN transition's schedule (A18 as re-ruled 2026-07-07).
162
+ if (!(n.S & STATUS_PENDING)) n.S &= ~STATUS_UNINITIALIZED;
163
+ const t = n.o?.Ne;
164
+ // A derived override (lanes stage, #3479) has no truth of its own: the
165
+ // slot disarms — the memo is plain again — and the override PROMOTES to
166
+ // `_value`. Not superseded, nothing it derives from told it otherwise
167
+ // (a source override that reverts to a differing truth dirties it just
168
+ // above — sources join this list before their derivations — and its
169
+ // recompute then replaces the promotion), so by the graph's invariant
170
+ // the override IS what a recompute from the truth yields. Re-deriving
171
+ // instead re-asked an async member's flight and held the transaction on
172
+ // it — a waterfall after the reveal.
173
+ const r = n.C & CONFIG_DERIVED_OVERRIDE;
174
+ ext(n).Ne = r && !(n.C & CONFIG_OPTIMISTIC) ? undefined : NOT_PENDING;
175
+ // A superseded override's subscribers already re-derived from the truth
176
+ // when it arrived (#3331) — the drop changes nothing they read. Everyone
177
+ // else learns of the correction here: this drop IS their notification.
178
+ const u = (n.C & CONFIG_OVERRIDE_SUPERSEDED) !== 0;
179
+ n.C &= -8912897;
180
+ if (!u && t !== NOT_PENDING && n.Ae !== unwrapOverride(t)) {
181
+ if (r) n.Ae = unwrapOverride(t); else insertSubs(n, true);
182
+ }
183
+ n.Te = null;
184
+ if (n.o !== null) n.o.jt = null;
185
+ }
186
+ // Settlement checkpoint (#2838): companions caught in this batch (or owned
187
+ // by a node in it) re-derive from committed state, so verdicts survive the
188
+ // transition that produced them (A19 — pending is a property of the data).
189
+ for (let i = 0; i < n; i++) {
190
+ const n = e[i];
191
+ if (n.o?.he || n.o?.Ge) GlobalQueue.Qn(n);
192
+ const t = n.o?.nn;
193
+ if (t && (t.o?.he === n || t.o?.Ge === n)) GlobalQueue.Qn(t);
194
+ }
195
+ e.splice(0, n);
196
+ }
197
+
198
+ /**
199
+ * A18 supersession (#3331): the node's own source arrived with a value that
200
+ * differs from its active override. "Knowing otherwise" ends the optimism for
201
+ * the graph at once: the override stays only as the DISPLAYED value (untracked
202
+ * reads, the applied frame) until the owning transaction commits, while
203
+ * tracked readers see the staged truth and re-derive from it as that
204
+ * transaction's held work — so async downstream restarts now, not at the
205
+ * revert (no waterfall).
206
+ *
207
+ * The lane's job for this node is over: a lane applies an optimistic view
208
+ * ahead of its transaction, and there is no optimistic view left — the
209
+ * corrected cascade is plain transaction-held work (staged memos, effect runs
210
+ * in the stashable queues). Demote the node and every cascade member that
211
+ * rides this lane; the caller then notifies on the plain channel. Runners the
212
+ * lane still holds for demoted effects (the optimistic frame that never got to
213
+ * apply) defer to the regular queue in runEffect. In a lane merged with a
214
+ * still-optimistic source, members shared with that source lose their lane
215
+ * too and simply wait for the transaction — less optimistic, never torn.
216
+ *
217
+ * A later arrival EQUAL to the override (an earlier action's answer superseded
218
+ * this one's; now this one's answer confirms it) ends the supersession: the
219
+ * graph re-derives from the override, which is the truth again. Notifies the
220
+ * subscribers in both cases. A plain matching confirmation (no supersession
221
+ * in force) is A17-silent for ordinary subscribers and wakes only an
222
+ * authoritative-view reader (until()'s predicate, refresh()'s waiter) that
223
+ * observed this node past its override — "authoritative arrival equal to the
224
+ * override" is exactly the acknowledgment it waits for (#3164, #3303). The
225
+ * wake hook is installed by the setters of that bit; optional here because
226
+ * the bit only implies the optimistic engine was consulted.
227
+ */ function supersedeOverride(e, n) {
228
+ const i = !e.Re || !e.Re(n, unwrapOverride(e.o.Ne));
229
+ if (!i) {
230
+ if (!(e.C & CONFIG_OVERRIDE_SUPERSEDED)) {
231
+ if (e.C & CONFIG_AUTHORITATIVE_OBSERVED) GlobalQueue.Tn?.(e);
232
+ return;
233
+ }
234
+ e.C &= ~CONFIG_OVERRIDE_SUPERSEDED;
235
+ } else {
236
+ // Provenance (#3331): an answer brought back by an OLDER action than the
237
+ // one that wrote this override answers a question the user has since
238
+ // changed. It is staged like any other landing and reveals if it is
239
+ // still the truth when the transaction commits, but it does not move the
240
+ // graph now — a slow source must not leak back in over a newer intent.
241
+ // 0 is mainline (no action): always the current question.
242
+ if (origin && origin < e.o.zt) return;
243
+ e.C |= CONFIG_OVERRIDE_SUPERSEDED;
244
+ const n = e.o?.de;
245
+ if (n) {
246
+ const i = findLane(n);
247
+ const t = [ e ];
248
+ while (t.length) {
249
+ const e = t.pop();
250
+ const n = e.o?.de;
251
+ if (!n || findLane(n) !== i) continue;
252
+ e.o.de = undefined;
253
+ i.Ie.delete(e);
254
+ for (let n = e.u; n !== null; n = n.ie) t.push(n.se);
255
+ }
256
+ }
257
+ }
258
+ if (attrHooks !== null) attrHooks.asyncEnd(e, undefined, n, true);
259
+ insertSubs(e);
260
+ }
261
+
262
+ /**
263
+ * The flush's pre-verdict step once the action bodies have ended (#3427).
264
+ * The bodies were the optimism's justification; with them over, the
265
+ * overrides still in force revert at the settle — unless the transaction is
266
+ * still waiting on AUTHORITATIVE work: an override node's own source in
267
+ * flight (`transitionBlocked` — that answer supersedes or confirms on
268
+ * arrival), or a held flight that does not derive from an override (a plain
269
+ * write's load the action asked for). Through that window the optimistic
270
+ * world stands: a co-written "saving" flag stays rendered until the page it
271
+ * covers lands (A17 — the optimistic world is one).
272
+ *
273
+ * The flights that DO derive from an override — routed through a live lane —
274
+ * are obsolete: their input is the guess that is about to revert, and nobody
275
+ * will read their answer. With nothing authoritative left, each override's
276
+ * truth is already here (the staged value an A17-silent landing left, else
277
+ * the committed value) and supersedes it now, exactly as an arriving
278
+ * differing truth does (A18, #3331): the graph re-derives from it as this
279
+ * transaction's held work — a lane-derived memo re-asks with the truth, its
280
+ * other changed inputs included — and the transaction settles when THAT
281
+ * lands. Before this the settle first waited for the obsolete flight,
282
+ * revealed the obsolete optimistic frame when it landed, and only then
283
+ * started the correction — a waterfall with a flash in the middle. Returns
284
+ * whether it superseded anything (the caller re-runs the heap).
285
+ *
286
+ * The optimistic world is one, so it ends early only when all of it can. An
287
+ * optimistic STORE edit cannot yet: its truth is the base layer under an
288
+ * overlay that `_clearOptimisticStores` folds off at settlement, with no
289
+ * tracked/displayed split — superseding its tracking signals alone would
290
+ * re-derive readers against a still-displayed overlay (and a memo reading
291
+ * both a signal and the store would ask a mixed question). A transaction
292
+ * holding one keeps the settle-then-revert order throughout. Companions
293
+ * (`_parentSource` set) are optimistic nodes too — a verdict written through
294
+ * the optimistic path so it flushes ahead of the hold — but they answer for
295
+ * their owner and snap at settlement (`_snapCompanions`), not here.
296
+ */ function endOptimism(e) {
297
+ if (!e.Tt || e.dt.length || !e.tn.length || e.rn.size || transitionBlocked(e)) return false;
298
+ for (const n of e.ot.keys()) if (sourceObserved(e, n, e) && n.o?.ut?.has(n) && !resolveLane(n)) return false;
299
+ let n = false;
300
+ for (const i of e.tn) {
301
+ if (!hasActiveOverride(i) || i.o.nn ||
302
+ // A derived override re-derives when its source's is superseded.
303
+ i.C & (CONFIG_OVERRIDE_SUPERSEDED | CONFIG_DERIVED_OVERRIDE) || i.S & STATUS_UNINITIALIZED) continue;
304
+ const e = i._e !== NOT_PENDING ? i._e : i.Ae;
305
+ if (!i.Re || !i.Re(e, unwrapOverride(i.o.Ne))) {
306
+ supersedeOverride(i, e);
307
+ // Judged by the mark, not the call: a provenance refusal (an older
308
+ // action's window is still open) leaves the node for a later pass.
309
+ if (i.C & CONFIG_OVERRIDE_SUPERSEDED) n = true;
310
+ }
311
+ }
312
+ return n;
313
+ }
314
+
315
+ /** read()'s value for a tracked reader of a superseded node: the truth —
316
+ * staged, or already committed (a mainline landing commits at the head of
317
+ * its flush, ahead of the heap run, and the override drops only at the
318
+ * batch's end; in between the graph must not fall back to the override it
319
+ * has left) — or the displayed override for a stale (render) reader of some
320
+ * OTHER transaction, the same visibility a foreign transaction's staged
321
+ * write has. */
322
+ /**
323
+ * A tracked read of an active override (read()'s override arm). Lanes mirror
324
+ * transitions (#3460): a render effect OFF the override's held lane — re-run
325
+ * by a sync write, or mounted mid-hold — sees the committed value, as a stale
326
+ * reader of a held transaction does, and publishes now; the lane's release
327
+ * re-runs it (readsHeldCommitted). The lane defers the override's own readers'
328
+ * runs, so the committed value is what is on screen — the override is the
329
+ * visible value only once the lane has revealed (or, demoted at body-end,
330
+ * A18). Otherwise the override displays, unless the node's own source
331
+ * answered with a DIFFERENT value (A18 supersession, #3331): the optimism is
332
+ * over for the graph — a tracked reader sees the staged truth — while the
333
+ * override remains the DISPLAYED value for untracked reads (and for a stale
334
+ * reader of some other transaction).
335
+ */ function overrideRead(e, n) {
336
+ if (stale && readsHeldCommitted(e, n)) return e.Ae;
337
+ if (!(e.C & CONFIG_OVERRIDE_SUPERSEDED)) return unwrapOverride(e.o?.Ne);
338
+ // The owning transaction: `_overrideOwner` (#2912), not the stamp — an
339
+ // override written directly inside an action never passes the adoption
340
+ // loop that stamps `_transition`, and a body-end supersession (#3427)
341
+ // stages nothing that would queue it. Without the owner a stale reader of
342
+ // a body-ended node read the committed truth beside a display still
343
+ // showing the override.
344
+ const i = resolveTransition(e);
345
+ if (stale && i && activeTransition !== i) return unwrapOverride(e.o?.Ne);
346
+ // A superseded read is a staged read (A29) whether the truth is staged or
347
+ // already committed: the pass that derives from it derives from the
348
+ // owning transaction's world (the override is still displayed by it).
349
+ enterStagedRead(e, i);
350
+ return e._e !== NOT_PENDING ? e._e : e.Ae;
351
+ }
352
+
353
+ /**
354
+ * An authoritative store landing on an override-covered node — a derived
355
+ * optimistic store's own truth arriving over a tentative edit through the
356
+ * projection-write channel (setSignal under projectionWriteActive). The
357
+ * store twin of asyncWrite's override branch: the truth stages for its
358
+ * transaction's commit whatever its relation to the committed value (a
359
+ * landing equal to committed still differs from the override), companions
360
+ * learn of it, and supersedeOverride decides the rest — A18 supersession with
361
+ * action provenance, or an A17-silent confirmation (#3331). Before this the
362
+ * landing took setSignal's plain path: a differing truth staged silently
363
+ * under the override and the graph never moved until the commit.
364
+ */ function landOnOverride(e, n) {
365
+ const i = e._e === NOT_PENDING ? e.Ae : e._e;
366
+ if (typeof n === "function") n = n(i);
367
+ if (attrHooks !== null) attrHooks.write(e, i, n);
368
+ if (e._e === NOT_PENDING) queuePendingNode(e);
369
+ e._e = n;
370
+ GlobalQueue.De?.(e, n);
371
+ supersedeOverride(e, n);
372
+ schedule();
373
+ return n;
374
+ }
375
+
376
+ function runQueue(e, n) {
377
+ for (let i = 0; i < e.length; i++) e[i](n | LANE_RUN);
378
+ }
379
+
380
+ /**
381
+ * Run effects from all lanes that are ready (no OBSERVED pending async — see
382
+ * laneHeld).
383
+ */ function runLaneEffects(e) {
384
+ for (const n of activeLanes) {
385
+ if (n.wn || laneHeld(n)) continue;
386
+ const i = n.vn[e - 1];
387
+ if (i.length) {
388
+ n.vn[e - 1] = [];
389
+ runQueue(i, e);
390
+ }
391
+ }
392
+ // Optimistic patch applications ride the same visibility slot as lane
393
+ // effects (in-flight DOM updates); no-op unless patches registered.
394
+ if (e === EFFECT_RENDER) GlobalQueue.Vn?.();
395
+ }
396
+
397
+ function cleanupCompletedLanes(e) {
398
+ for (const n of activeLanes) {
399
+ const i = e ? n.Te === e : !n.Te;
400
+ if (!i) continue;
401
+ if (!n.wn) {
402
+ if (n.vn[0].length) runQueue(n.vn[0], EFFECT_RENDER);
403
+ if (n.vn[1].length) runQueue(n.vn[1], EFFECT_USER);
404
+ }
405
+ if (n.Ln.o?.de === n) if (n.Ln.o !== null) n.Ln.o.de = undefined;
406
+ n.Ie.clear();
407
+ n.vn[0].length = 0;
408
+ n.vn[1].length = 0;
409
+ activeLanes.delete(n);
410
+ signalLanes.delete(n.Ln);
411
+ }
412
+ }
413
+
414
+ /** read()'s per-lane suspension test (pending-throw path, lane context). */ function laneSuspends(e) {
415
+ // An UNINITIALIZED async source suspends regardless of lane (#3276): a
416
+ // lane mismatch preserves an already-committed stale value, but a source
417
+ // with no committed truth has nothing to serve — the cross-lane read
418
+ // surfaced a fabricated `undefined` where latest() itself suspends
419
+ // (latestRead rethrows NotReady for tracked uninitialized reads). Lives
420
+ // here rather than read()'s throw path so the floor bundles don't pay:
421
+ // this is only reachable under a lane, which implies the engine.
422
+ if (e.S & STATUS_UNINITIALIZED) return true;
423
+ // Per-lane suspension: only throw if in same lane as pending async
424
+ // AND the node doesn't have an active WRITTEN override (overrides are the
425
+ // visible value, downstream in the lane should read the override, not
426
+ // throw). A derived override (#3479) is a previous speculative answer, not
427
+ // an intent: the re-ask pending behind it suspends like any lane async.
428
+ const n = e.o?.de;
429
+ if (!n) return false;
430
+ return findLane(n) === findLane(currentOptimisticLane) && (!hasActiveOverride(e) || (e.C & CONFIG_DERIVED_OVERRIDE) !== 0);
431
+ }
432
+
433
+ /**
434
+ * read()'s reveal carve-out asks whether a pending node is routed through a
435
+ * LIVE lane: a lane-derived flight's inputs are already revealed through the
436
+ * lane (the override, or latest()'s fresh value), so a stale reader of another
437
+ * transaction must hold on the flight rather than show the node's committed
438
+ * value beside them (#3334). Exact, not sticky: `resolveLane` clears a lane
439
+ * reference the engine has since retired, so a node that was once lane-routed
440
+ * and is now pending under a plain hold is judged by that hold alone.
441
+ */ function laneLive(e) {
442
+ return resolveLane(e) !== undefined;
443
+ }
444
+
445
+ /**
446
+ * read()'s entanglement gate: a reader recomputing under an optimistic lane
447
+ * that reads a pending mid-transition write sees the committed value; the sub
448
+ * is recorded for replay at commit.
449
+ */ function gatedRead(e, n, i) {
450
+ if (latestReadActive || e._e === NOT_PENDING || e.ht || n !== e && !(n.ft & REACTIVE_MANUAL_WRITE)) {
451
+ return false;
452
+ }
453
+ activeTransition.Ht.add(i);
454
+ return true;
455
+ }
456
+
457
+ /**
458
+ * read()'s value selection under a lane: return the committed `_value` for
459
+ * optimistic/lane-assigned signals, stale-mode reads, and pending owners.
460
+ */ function laneReadsCommitted(e, n, i) {
461
+ if (e.o?.Ne !== undefined || !!e.o?.de || !!(n.S & STATUS_PENDING)) {
462
+ // The committed view hides a staged in-flight value that will promote
463
+ // silently (commitPendingNode never re-notifies). gatedRead records plain
464
+ // signals for replay at commit; async memos are excluded from it by the
465
+ // `_fn` check and reach here instead — a lane-assigned source whose async
466
+ // already settled (laneAsyncSettled keeps _optimisticLane) served its
467
+ // committed value to a reader that never re-ran after the landing, so a
468
+ // pending-gated branch stayed one value behind permanently (#3041
469
+ // follow-up). Record the reader under the same replay contract — when
470
+ // the commit will actually change what it read: a staged value equal to
471
+ // the committed one (a lane recompute already published it, INV-11)
472
+ // promotes to the same view, and a replay would only re-run effects
473
+ // against an unchanged frame (#3330). An override-covered node's revert
474
+ // notifies its own subscribers when the truth differs (resolveOptimistic
475
+ // Nodes), so the reader is recorded only for the staged-vs-committed gap.
476
+ if (e._e !== NOT_PENDING && e._e !== e.Ae) (activeTransition ?? globalQueue.m).Ht.add(i);
477
+ return true;
478
+ }
479
+ if (n === e && stale && i.o?.nn !== e) {
480
+ // The committed view can hide a staged write (a lane member — even just
481
+ // an isPending companion flip — puts the reader "under a lane"). The
482
+ // staged value commits with no re-delivery (commitPendingNode never
483
+ // re-notifies), so record the reader for replay at commit — the same
484
+ // contract gatedRead provides (#2963). gatedRead itself only covers
485
+ // signal reads where the reading computed differs from the source; the
486
+ // owner === el memo/self read lands here instead. With a transaction
487
+ // active the staged value promotes silently at ITS landing, so record
488
+ // into the transaction (#3041 follow-up: a pending-gated branch that
489
+ // first read its async source during the landing flush stayed one value
490
+ // behind permanently); with none, into the ambient batch.
491
+ if (e._e !== NOT_PENDING) (activeTransition ?? globalQueue.m).Ht.add(i);
492
+ return true;
493
+ }
494
+ return false;
495
+ }
496
+
497
+ /**
498
+ * recompute()'s lane posture: resolve the node's own lane (own=true), or adopt
499
+ * a dependency's optimistic lane (own=false — parent-deeper-than-owned-child
500
+ * can run before its OPT-dirty child propagates).
501
+ */ function recomputeLane(e, n) {
502
+ if (n) {
503
+ const n = resolveLane(e);
504
+ if (!n) return null;
505
+ // Wake-only lane demotion (#3009): a plain write to a latest()-tracked
506
+ // source rides the optimistic channel only to wake verdict companions —
507
+ // its lane is sourced by the companion shadow (_parentSource set) and owns
508
+ // no transaction. When such a node is pulled mid-tick by a latest()/
509
+ // isPending() probe, lane posture would direct-commit _value, leaking the
510
+ // queued write into committed reads before the flush. Return `false` so
511
+ // recompute() runs plain: the value stages and commits with the flush.
512
+ // (el's own override slot excludes companions themselves; a lane merged
513
+ // into a real optimistic lane resolves to a non-companion source.)
514
+ if (!globalQueue.Sn && !activeTransition && !n.Te && n.Ln.o?.nn !== undefined && e.o?.Ne === undefined) {
515
+ if (e.o !== null) e.o.de = undefined;
516
+ return false;
517
+ }
518
+ return n;
519
+ }
520
+ for (let n = e.ee; n; n = n.te) {
521
+ const i = n.re;
522
+ if (i.ft & REACTIVE_OPTIMISTIC_DIRTY) {
523
+ const n = resolveLane(i);
524
+ if (n) {
525
+ e.ft |= REACTIVE_OPTIMISTIC_DIRTY;
526
+ assignOrMergeLane(e, n);
527
+ return n;
528
+ }
529
+ }
530
+ }
531
+ return null;
532
+ }
533
+
534
+ /** recompute()'s catch path: record the pending async as the current lane's
535
+ * (ownership — laneHeld decides the hold). The lane source's isPending
536
+ * companion is NOT refreshed here: its verdict never read _pendingAsync, and
537
+ * the source's own write/commit/settlement paths keep it current. */ function laneAsyncPending(e) {
538
+ const n = findLane(currentOptimisticLane);
539
+ if (n.Ln !== e) {
540
+ n.Ie.add(e);
541
+ ext(e).de = n;
542
+ e.C |= CONFIG_HAS_LANE;
543
+ }
544
+ }
545
+
546
+ /** recompute()'s success path: the node's async settled, clear it from its lane. */ function laneAsyncSettled(e) {
547
+ const n = resolveLane(e);
548
+ if (n) {
549
+ n.Ie.delete(e);
550
+ }
551
+ }
552
+
553
+ function trackOptimisticStore(e) {
554
+ // After initTransition, globalQueue._batch IS activeTransition (same reference)
555
+ globalQueue.m.rn.add(e);
556
+ schedule();
557
+ }
558
+
559
+ /**
560
+ * Installs the engine's hooks. Idempotent; called by every module that can
561
+ * create optimistic state (verdict.ts at module top level, createOptimistic
562
+ * and createOptimisticStore at first call) BEFORE any optimistic node exists.
563
+ */ function installOptimisticEngine() {
564
+ if (GlobalQueue.mn !== null) return;
565
+ GlobalQueue.mn = optimisticWrite;
566
+ GlobalQueue.Un = resolveOptimisticNodes;
567
+ GlobalQueue.kn = transitionBlocked;
568
+ GlobalQueue.xn = cleanupCompletedLanes;
569
+ GlobalQueue.Mn = runLaneEffects;
570
+ GlobalQueue.Oe = supersedeOverride;
571
+ GlobalQueue.yn = endOptimism;
572
+ GlobalQueue.Dn = overrideRead;
573
+ GlobalQueue.ge = laneOverride;
574
+ GlobalQueue.Fn = landOnOverride;
575
+ GlobalQueue.hn = gatedRead;
576
+ GlobalQueue.Gn = laneSuspends;
577
+ GlobalQueue.Rn = laneLive;
578
+ GlobalQueue.An = laneReadsCommitted;
579
+ GlobalQueue.gt = recomputeLane;
580
+ GlobalQueue.kt = laneAsyncPending;
581
+ GlobalQueue.vt = laneAsyncSettled;
582
+ GlobalQueue.jn = trackOptimisticStore;
583
+ }
584
+
585
+ export { installOptimisticEngine };