@solidjs/signals 2.0.0-rc.1 → 2.0.0-rc.11

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 (150) hide show
  1. package/dist/dev-shared.js +7155 -0
  2. package/dist/dev.attribution.js +3307 -0
  3. package/dist/dev.js +5765 -4496
  4. package/dist/observe/affects.js +129 -0
  5. package/dist/observe/attribution.js +7 -0
  6. package/dist/observe/boundaries.js +793 -0
  7. package/dist/observe/core/action.js +177 -0
  8. package/dist/observe/core/async.js +849 -0
  9. package/dist/observe/core/attribution-costs.js +66 -0
  10. package/dist/observe/core/attribution-feedback.js +280 -0
  11. package/dist/observe/core/attribution-hooks.js +83 -0
  12. package/dist/observe/core/attribution-queries.js +43 -0
  13. package/dist/observe/core/attribution.js +2904 -0
  14. package/dist/observe/core/constants.js +283 -0
  15. package/dist/{types-cjs/core/context.d.cts → observe/core/context.js} +37 -12
  16. package/dist/observe/core/core.js +1999 -0
  17. package/dist/observe/core/dev.js +343 -0
  18. package/dist/observe/core/effect.js +200 -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 +669 -0
  27. package/dist/observe/core/owner.js +353 -0
  28. package/dist/observe/core/scheduler.js +1561 -0
  29. package/dist/observe/core/verdict.js +674 -0
  30. package/dist/observe/index.js +59 -0
  31. package/dist/observe/map.js +507 -0
  32. package/dist/observe/signals.js +731 -0
  33. package/dist/observe/store/index.js +44 -0
  34. package/dist/observe/store/next/optimistic.js +717 -0
  35. package/dist/observe/store/next/projection.js +266 -0
  36. package/dist/observe/store/next/reconcile.js +375 -0
  37. package/dist/observe/store/next/store.js +2480 -0
  38. package/dist/observe/store/next/target.js +51 -0
  39. package/dist/observe/store/store.js +331 -0
  40. package/dist/observe/store/storePath.js +103 -0
  41. package/dist/observe/store/utils.js +1070 -0
  42. package/dist/prod/affects.js +10 -9
  43. package/dist/prod/attribution.js +45 -0
  44. package/dist/prod/boundaries.js +388 -178
  45. package/dist/prod/core/action.js +65 -37
  46. package/dist/prod/core/async.js +378 -163
  47. package/dist/prod/core/attribution-hooks.js +3 -0
  48. package/dist/prod/core/constants.js +186 -1
  49. package/dist/prod/core/context.js +10 -16
  50. package/dist/prod/core/core.js +1546 -416
  51. package/dist/prod/core/dev.js +18 -1
  52. package/dist/prod/core/effect.js +119 -79
  53. package/dist/prod/core/error-hooks.js +71 -0
  54. package/dist/prod/core/error.js +13 -1
  55. package/dist/prod/core/external.js +4 -4
  56. package/dist/prod/core/graph.js +105 -54
  57. package/dist/prod/core/heap.js +72 -65
  58. package/dist/prod/core/invariants.js +3 -2
  59. package/dist/prod/core/lanes.js +117 -52
  60. package/dist/prod/core/optimistic.js +500 -104
  61. package/dist/prod/core/owner.js +131 -84
  62. package/dist/prod/core/scheduler.js +1012 -241
  63. package/dist/prod/core/verdict.js +417 -112
  64. package/dist/prod/index.js +20 -8
  65. package/dist/prod/map.js +355 -171
  66. package/dist/prod/signals.js +363 -32
  67. package/dist/prod/store/index.js +2 -0
  68. package/dist/prod/store/next/optimistic.js +530 -197
  69. package/dist/prod/store/next/projection.js +142 -57
  70. package/dist/prod/store/next/reconcile.js +149 -103
  71. package/dist/prod/store/next/store.js +1470 -346
  72. package/dist/prod/store/next/target.js +40 -9
  73. package/dist/prod/store/store.js +23 -19
  74. package/dist/prod/store/utils.js +997 -130
  75. package/dist/types/attribution.d.ts +19 -0
  76. package/dist/types/attribution.prod.d.ts +22 -0
  77. package/dist/types/boundaries.d.ts +78 -5
  78. package/dist/types/core/action.d.ts +12 -5
  79. package/dist/types/core/async.d.ts +3 -1
  80. package/dist/types/core/attribution-costs.d.ts +35 -0
  81. package/dist/types/core/attribution-feedback.d.ts +133 -0
  82. package/dist/types/core/attribution-hooks.d.ts +304 -0
  83. package/dist/types/core/attribution-queries.d.ts +24 -0
  84. package/dist/types/core/attribution.d.ts +892 -0
  85. package/dist/types/core/constants.d.ts +179 -0
  86. package/dist/types/core/core.d.ts +299 -25
  87. package/dist/types/core/dev.d.ts +421 -8
  88. package/dist/types/core/effect.d.ts +8 -1
  89. package/dist/types/core/error-hooks.d.ts +71 -0
  90. package/dist/types/core/error.d.ts +9 -0
  91. package/dist/types/core/graph.d.ts +23 -0
  92. package/dist/types/core/heap.d.ts +5 -3
  93. package/dist/types/core/index.d.ts +6 -3
  94. package/dist/types/core/invariants.d.ts +5 -1
  95. package/dist/types/core/lanes.d.ts +51 -19
  96. package/dist/types/core/owner.d.ts +15 -3
  97. package/dist/types/core/scheduler.d.ts +252 -1
  98. package/dist/types/core/types.d.ts +124 -41
  99. package/dist/types/index.d.ts +18 -5
  100. package/dist/types/map.d.ts +2 -0
  101. package/dist/types/signals.d.ts +145 -13
  102. package/dist/types/store/index.d.ts +7 -6
  103. package/dist/types/store/next/optimistic.d.ts +18 -13
  104. package/dist/types/store/next/projection.d.ts +5 -2
  105. package/dist/types/store/next/reconcile.d.ts +7 -0
  106. package/dist/types/store/next/store.d.ts +83 -11
  107. package/dist/types/store/next/target.d.ts +108 -11
  108. package/dist/types/store/store.d.ts +20 -11
  109. package/dist/types/store/utils.d.ts +188 -0
  110. package/package.json +31 -26
  111. package/dist/node.cjs +0 -8137
  112. package/dist/types/store/optimistic.d.ts +0 -45
  113. package/dist/types/store/projection.d.ts +0 -70
  114. package/dist/types/store/reconcile.d.ts +0 -46
  115. package/dist/types-cjs/affects.d.cts +0 -47
  116. package/dist/types-cjs/boundaries.d.cts +0 -175
  117. package/dist/types-cjs/core/action.d.cts +0 -64
  118. package/dist/types-cjs/core/async.d.cts +0 -21
  119. package/dist/types-cjs/core/constants.d.cts +0 -71
  120. package/dist/types-cjs/core/core.d.cts +0 -156
  121. package/dist/types-cjs/core/dev.d.cts +0 -64
  122. package/dist/types-cjs/core/effect.d.cts +0 -30
  123. package/dist/types-cjs/core/error.d.cts +0 -47
  124. package/dist/types-cjs/core/external.d.cts +0 -15
  125. package/dist/types-cjs/core/graph.d.cts +0 -5
  126. package/dist/types-cjs/core/heap.d.cts +0 -22
  127. package/dist/types-cjs/core/index.d.cts +0 -13
  128. package/dist/types-cjs/core/invariants.d.cts +0 -59
  129. package/dist/types-cjs/core/lanes.d.cts +0 -56
  130. package/dist/types-cjs/core/optimistic.d.cts +0 -6
  131. package/dist/types-cjs/core/owner.d.cts +0 -124
  132. package/dist/types-cjs/core/scheduler.d.cts +0 -172
  133. package/dist/types-cjs/core/types.d.cts +0 -148
  134. package/dist/types-cjs/core/verdict.d.cts +0 -2
  135. package/dist/types-cjs/index.d.cts +0 -10
  136. package/dist/types-cjs/map.d.cts +0 -69
  137. package/dist/types-cjs/package.json +0 -3
  138. package/dist/types-cjs/signals.d.cts +0 -624
  139. package/dist/types-cjs/store/index.d.cts +0 -19
  140. package/dist/types-cjs/store/next/optimistic.d.cts +0 -20
  141. package/dist/types-cjs/store/next/projection.d.cts +0 -24
  142. package/dist/types-cjs/store/next/reconcile.d.cts +0 -3
  143. package/dist/types-cjs/store/next/store.d.cts +0 -71
  144. package/dist/types-cjs/store/next/target.d.cts +0 -99
  145. package/dist/types-cjs/store/optimistic.d.cts +0 -45
  146. package/dist/types-cjs/store/projection.d.cts +0 -70
  147. package/dist/types-cjs/store/reconcile.d.cts +0 -46
  148. package/dist/types-cjs/store/store.d.cts +0 -138
  149. package/dist/types-cjs/store/storePath.d.cts +0 -58
  150. package/dist/types-cjs/store/utils.d.cts +0 -74
@@ -0,0 +1,1561 @@
1
+ import { EFFECT_RENDER, EFFECT_USER, STATUS_PENDING, CONFIG_ADOPTED_UNFLUSHED, NOT_PENDING, REACTIVE_MANUAL_WRITE, STATUS_UNINITIALIZED, REACTIVE_OPTIMISTIC_DIRTY, REACTIVE_IN_HEAP_HEIGHT, REACTIVE_DISPOSED, CONFIG_HELD_TRUTH, CONFIG_HAS_COMPANIONS, EFFECT_TRACKED, CONFIG_INPUTS_PUBLISHED, REACTIVE_ZOMBIE, CONFIG_LANE_FRAME, CONFIG_HAS_LANE, CONFIG_HAS_SNAPSHOT, REACTIVE_RECOMPUTING_DEPS, REACTIVE_MISSED_WAKE, CONFIG_IN_SNAPSHOT_SCOPE, REACTIVE_SNAPSHOT_STALE, CONFIG_SLOT_NODE, CONFIG_HELD_CHILDREN, REACTIVE_REASK, CONFIG_AUTHORITATIVE_READ } from "./constants.js";
2
+
3
+ import { attrHooks } from "./attribution-hooks.js";
4
+
5
+ import { resyncUnflushedCompanions, currentOptimisticLane, slotUnobservedHook, markUnflushedStaged, hasActiveOverride } from "./core.js";
6
+
7
+ import { DEV, GRAPH_SIZE_WARN_AT, noteFanOut } from "./dev.js";
8
+
9
+ import { NotReadyError } from "./error.js";
10
+
11
+ import { sweepDormant, trimStaleDeps } from "./graph.js";
12
+
13
+ import { runHeap, deleteFromHeap, enqueueSub } from "./heap.js";
14
+
15
+ import { activeLanes, assignOrMergeLane, findLane } from "./lanes.js";
16
+
17
+ export { getOrCreateLane, mergeLanes, resolveLane } from "./lanes.js";
18
+
19
+ import { devCheckFlushStart, devCheckActiveOverrides, devCensusCompanions, devCheckQuiescent } from "./invariants.js";
20
+
21
+ const transitions = new Set;
22
+
23
+ const dirtyQueue = {
24
+ eE: new Array(2e3).fill(undefined),
25
+ tE: false,
26
+ st: 0,
27
+ EE: 0
28
+ };
29
+
30
+ const zombieQueue = {
31
+ eE: new Array(2e3).fill(undefined),
32
+ tE: false,
33
+ st: 0,
34
+ EE: 0
35
+ };
36
+
37
+ /** runHeap callback that discards a queued zombie recompute instead of running
38
+ * it: unlink pure recompute entries; strip just the recompute bit from dirtied
39
+ * height-adjust entries so their height work still happens. A zombie dirtied
40
+ * through the lane channel (OPTIMISTIC_DIRTY — an override or a `latest()`
41
+ * companion) runs instead (#3444): a zombie renders mainline until the commit
42
+ * disposes it, and the lane's values ARE the mainline frame — the still-visible
43
+ * branch a held `Show` is removing showed the old `latest(count)` beside the
44
+ * new one outside. Its pass runs under the lane and its run lands on the
45
+ * lane's effect queue, so a held lane defers it exactly as it defers every
46
+ * other reader's. */ function cancelZombieRecompute(e) {
47
+ if (e.oe & REACTIVE_OPTIMISTIC_DIRTY && !laneZombie(e)) return GlobalQueue.nt(e);
48
+ if (e.oe & REACTIVE_IN_HEAP_HEIGHT) e.oe &= -140; else {
49
+ deleteFromHeap(e, zombieQueue);
50
+ e.oe &= -132;
51
+ }
52
+ }
53
+
54
+ /** A member of a parked LANE frame (CONFIG_LANE_FRAME on the owner whose pass
55
+ * parked it, #3662). The #3444 exception is for transaction zombies; a lane
56
+ * frame's member is retired by the run that carries the lane's values, so its
57
+ * lane-channel recompute is cancelled with the rest — run, it republished the
58
+ * retired frame under those values. Mainline writes still reach it (#3463). */ function laneZombie(e) {
59
+ let t = e;
60
+ while (t !== null && t.oe & REACTIVE_ZOMBIE) t = t._parent;
61
+ return t !== null && (t.C & CONFIG_LANE_FRAME) !== 0;
62
+ }
63
+
64
+ let clock = 0;
65
+
66
+ let activeTransition = null;
67
+
68
+ let scheduled = false;
69
+
70
+ let halted = false;
71
+
72
+ let haltNotified = false;
73
+
74
+ let syncDepth = 0;
75
+
76
+ let projectionWriteActive = false;
77
+
78
+ /** > 0 while an action's generator body is on the stack (the synchronous
79
+ * slice between yields). Maintained by action.ts around `it.next()`. */ let actionStepDepth = 0;
80
+
81
+ function enterActionStep() {
82
+ actionStepDepth++;
83
+ }
84
+
85
+ function exitActionStep() {
86
+ actionStepDepth--;
87
+ }
88
+
89
+ // Store property nodes whose last subscriber left while they carried state
90
+ // the backing cannot reconstruct — an optimistic override (overrides live on
91
+ // nodes, over a clone the setter discards) or a staged write. Releasing the
92
+ // slot then would drop the override: an optimistic store key read `0` the
93
+ // moment its only reader gated away while the action was live (S7). Swept
94
+ // after each flush — a node still without subs whose override and staging
95
+ // have resolved is released through the slot hook; one that regained a
96
+ // subscriber leaves the set.
97
+ const transientStoreNodes = new Set;
98
+
99
+ /** Slot hook's deferral: release this node when its carried state resolves. */ function deferSlotRelease(e) {
100
+ transientStoreNodes.add(e);
101
+ }
102
+
103
+ function canUseSimpleSyncFlush(e) {
104
+ const t = e.m;
105
+ return transitions.size === 0 && activeLanes.size === 0 && e.Xn.length === 0 && t.bn.length === 0 && t.A.length === 0 && t.En.size === 0 && transientStoreNodes.size === 0 && pendingRearms.size === 0;
106
+ }
107
+
108
+ function sweepTransientStoreNodes() {
109
+ if (transientStoreNodes.size === 0) return;
110
+ for (const e of transientStoreNodes) {
111
+ if (e.u !== null) {
112
+ transientStoreNodes.delete(e);
113
+ continue;
114
+ }
115
+ if (e._e !== NOT_PENDING) continue;
116
+ if (e.o?.we !== undefined && e.o?.we !== NOT_PENDING) continue;
117
+ // A live affects() mark keeps the node addressable: sweeping it would
118
+ // detach the refcount from the slot (a fresh probe would upsert a new,
119
+ // unmarked node for the same property).
120
+ if (e.o?.t) continue;
121
+ transientStoreNodes.delete(e);
122
+ if (e.C & CONFIG_SLOT_NODE) slotUnobservedHook(e); else e.o?.bt?.();
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Toggles the dev-mode "must be inside a `<Loading>` boundary" enforcement
128
+ * window. Only `render()` calls this — wrapping the initial mount so that a
129
+ * top-level uncaught async read surfaces the diagnostic. Not part of the
130
+ * user-facing API.
131
+ *
132
+ * @internal
133
+ */ function enforceLoadingBoundary(e) {}
134
+
135
+ function setProjectionWriteActive(e) {
136
+ projectionWriteActive = e;
137
+ }
138
+
139
+ /**
140
+ * Ambient work IS a transaction: the global queue always carries one
141
+ * current-transaction-shaped batch (`globalQueue._batch`). With no transition
142
+ * active, registrations (pending commits, optimistic nodes, affects marks,
143
+ * optimistic stores) land in a plain ambient batch that the plain flush
144
+ * finalizes; when a transition initializes it adopts the ambient batch's
145
+ * contents and `_batch` becomes the transition itself, so later registrations
146
+ * land there directly — no per-field aliasing.
147
+ */ function createBatch() {
148
+ return {
149
+ Ge: clock,
150
+ In: [],
151
+ ae: new Map,
152
+ bn: [],
153
+ A: [],
154
+ En: new Set,
155
+ tt: [],
156
+ ei: {
157
+ ti: [ [], [] ],
158
+ Xn: []
159
+ },
160
+ Dt: false,
161
+ Tt: new Set,
162
+ ht: null
163
+ };
164
+ }
165
+
166
+ function mergeTransitionState(e, t) {
167
+ if (attrHooks !== null) attrHooks.transitionMerged(e, t);
168
+ t.Dt = e;
169
+ e.tt.push(...t.tt);
170
+ e.et ||= t.et;
171
+ for (const n of activeLanes) if (n.me === t) n.me = e;
172
+ if (t.bn.length) {
173
+ // Move (don't copy): the global queue's batch may still be the outgoing
174
+ // transition, and the adoption pass in initTransition would re-push its
175
+ // contents into the target — duplicating every entry.
176
+ e.bn.push(...t.bn);
177
+ t.bn.length = 0;
178
+ }
179
+ if (t.A.length) {
180
+ // Move (don't copy): the global queue's batch may still be the outgoing
181
+ // transition, and the adoption pass in initTransition would re-push its
182
+ // contents into the target — double-releasing every mark.
183
+ e.A.push(...t.A);
184
+ t.A.length = 0;
185
+ }
186
+ for (const n of t.En) e.En.add(n);
187
+ for (const [n, i] of t.ae) {
188
+ let t = e.ae.get(n);
189
+ if (!t) e.ae.set(n, t = new Set);
190
+ for (const e of i) t.add(e);
191
+ }
192
+ for (const n of t.Tt) e.Tt.add(n);
193
+ if (t.ht) (e.ht ??= []).push(...t.ht);
194
+ }
195
+
196
+ /**
197
+ * Flip-entanglement (#3164 follow-up): `until()` is a declaration of
198
+ * relatedness — the predicate names the condition that confirms the awaiting
199
+ * transaction. When the predicate settles truthy, every live foreign
200
+ * transition whose staged write it read IS the confirming event by the
201
+ * user's own definition, so it merges into the awaiting transaction and
202
+ * reveals at the joint settle — the cross-primitive twin of the family fold
203
+ * (a landing on an optimism-carrying family joins the retaining
204
+ * transaction). Non-flipping updates never pass through here: falsy
205
+ * evaluations don't entangle, so unrelated traffic on the watched sources
206
+ * reveals freely on its own schedule.
207
+ *
208
+ * Runs inside the predicate's compute (pure phase) — the confirming
209
+ * transition's stamps are still live and its commit decision hasn't run, so
210
+ * the merge lands before any reveal. Only the tree-shaken graphs that call
211
+ * `until()` retain this.
212
+ */ function entangleConfirmingTransitions(e, t) {
213
+ t = currentTransition(t);
214
+ if (t.Dt === true) return;
215
+ // The confirming evidence is a dep whose value is STAGED (pending,
216
+ // uncommitted) at flip evaluation — committed deps are public already and
217
+ // carry nothing to entangle. A staged dep lives in one of two carriers: a
218
+ // stamped transition, or the queue's current batch (ambient registrations
219
+ // don't stamp; "ambient work IS a transaction" — the batch is the
220
+ // carrier). The entangle STEALS the carrier's staged cargo — its pending
221
+ // nodes move (re-stamped) into the awaiting transaction and reveal at its
222
+ // settle — but never the carrier itself: its async reporters, actions,
223
+ // and stashes are its own future (a live stream's flight must not chain
224
+ // the awaiting transaction to landings that haven't happened; a merged
225
+ // reporter deadlocked exactly that way).
226
+ let n = false;
227
+ for (let i = e.Ee; i !== null; i = i.Ie) {
228
+ const r = i.De;
229
+ if (r._e !== NOT_PENDING) {
230
+ const e = r.me;
231
+ const i = e != null ? currentTransition(e) : null;
232
+ // Skip the awaiting transaction's own cargo (t === target: a
233
+ // fold-staged landing or a write the action itself issued — hold and
234
+ // reveal already correct) and dead carriers. Ambient-batch staging
235
+ // (t === null) must leave the batch NOW — it commits at this flush's
236
+ // end, which would reveal the confirmation under the live optimism
237
+ // it just confirmed.
238
+ const s = i === null ? currentBatch.In : i !== t && i.Dt !== true ? i.In : null;
239
+ if (s !== null) n = stealEntangledCargo(s, t) || n;
240
+ }
241
+ if (i === e.Je) break;
242
+ }
243
+ // The steal never activates the awaiting transaction: the predicate can
244
+ // flip inside another transaction's finalize heap, and adopting the queue
245
+ // batch there hands the stolen cargo to that finalize's commit sweep — a
246
+ // premature reveal at a foreign settle. Subscribers that computed against
247
+ // the pre-steal world were re-dirtied by the steal itself, so this
248
+ // flush's applies paint the mid-hold view (committed for lane and stale
249
+ // readers; a deriving reader is held with the cargo); the cargo commits
250
+ // at the awaiting transaction's own settle.
251
+ }
252
+
253
+ /** Move a confirming carrier's staged nodes into the awaiting transaction:
254
+ * re-stamp and arm the held-truth mask (override-covered nodes skip it —
255
+ * the override already hides their staged value per A17, and is usually
256
+ * the very optimism this confirmation settles); the mask's commit
257
+ * registers the settle-side post-revert wake. The carrier's array is
258
+ * emptied so its own commit point commits none of the stolen cargo.
259
+ *
260
+ * EFFECT subs of stolen nodes re-run: any that recomputed against the
261
+ * staging BEFORE the steal (the carrier's landing notified them as a plain
262
+ * write) hold a private result derived from the carrier's world — one
263
+ * that the next paint gate (stash-point lane run, a foreign flush's
264
+ * completion drain) would apply as the carrier's. Re-running them now that
265
+ * the cargo is the awaiting transaction's re-routes each by its posture in
266
+ * this same heap pass: a lane pass keeps committed (the mask), a stale
267
+ * render effect keeps committed and registers for the reveal's replay
268
+ * (stale-of-foreign), and a deriving user effect is served the truth and
269
+ * held with the transaction (A29). PURE computeds are deliberately NOT
270
+ * re-run: a staged value of theirs is itself stolen cargo — held with the
271
+ * transaction, so ordinary readers already serve their committed value —
272
+ * while re-running them would re-derive the OLD world and re-stage it over
273
+ * the held truth. The reveal re-notifies (commitPendingNodes), which is
274
+ * when they re-derive for real. */ function stealEntangledCargo(e, t) {
275
+ if (e === t.In || e.length === 0) return false;
276
+ for (let n = 0; n < e.length; n++) {
277
+ const i = e[n];
278
+ i.me = t;
279
+ t.In.push(i);
280
+ // Override-covered nodes stay silent AND unmasked: the override is the
281
+ // display (A17 — its staging never notified, its revert will), so their
282
+ // subs saw nothing and re-running one would break the silence with a
283
+ // duplicate fire of an unchanged view.
284
+ if (!hasActiveOverride(i)) {
285
+ i.C |= CONFIG_HELD_TRUTH;
286
+ for (let e = i.u; e !== null; e = e.Pe) {
287
+ const t = e.Ae;
288
+ if (t.He && !(t.C & CONFIG_AUTHORITATIVE_READ)) enqueueSub(t);
289
+ }
290
+ }
291
+ }
292
+ e.length = 0;
293
+ transitions.add(t);
294
+ return true;
295
+ }
296
+
297
+ /** `schedule()` armed `scheduled` but withheld the microtask because a
298
+ * projection draft was writing (see below). Consumed by `scheduleWithheld`. */ let withheld = false;
299
+
300
+ function schedule() {
301
+ if (halted) {
302
+ notifyHalted();
303
+ return;
304
+ }
305
+ if (scheduled) return;
306
+ scheduled = true;
307
+ if (!syncDepth && !globalQueue.$t) {
308
+ // A projection draft's writes withhold the microtask: a flight's landing
309
+ // drains them (asyncWrite's flush), and a drain BEFORE the landing tears
310
+ // the pre- and post-await halves of the run apart (createProjection.async
311
+ // "notifies only changed paths"). A draft write that no landing will
312
+ // follow re-arms through scheduleWithheld (proj R37).
313
+ withheld = projectionWriteActive;
314
+ if (!withheld) queueMicrotask(flush);
315
+ }
316
+ }
317
+
318
+ /** Arm the microtask `schedule()` withheld under projectionWriteActive. The
319
+ * projection draft calls this after a write made outside its run with no
320
+ * flight up (proj R37): nothing else will drain, and leaving `scheduled`
321
+ * armed with no microtask strands the whole scheduler — every later
322
+ * `schedule()` early-returns — until something calls `flush()` by hand.
323
+ * (`withheld` was set in the same synchronous slice as this call, so the
324
+ * syncDepth/_running gates it passed still hold; a stale mark left by a
325
+ * landing's own flush arms at worst one no-op drain.) */ function scheduleWithheld() {
326
+ if (withheld) {
327
+ withheld = false;
328
+ queueMicrotask(flush);
329
+ }
330
+ }
331
+
332
+ /**
333
+ * Parked transactions whose reporter set changed without a write. A
334
+ * transaction completes when nothing live reports a flight it waits on, but
335
+ * the flush only judges the ACTIVE transaction: a parked one is re-entered by
336
+ * a stamped node's landing or an action's resume. A reporter that stops
337
+ * counting for another reason — its loading boundary flipped to the fallback
338
+ * (#3375), or it was disposed by ambient work (#3372) — is neither: the
339
+ * pruning in `reporterBlocksSource` would drop it at the next check, but no
340
+ * check comes, and the writes held with it stay staged. Such sites record the
341
+ * transaction here (deduped: one idle pass per transaction, however many
342
+ * reporters changed); the flush re-enters it on an otherwise idle pass, so
343
+ * the re-evaluation adopts no unrelated ambient work.
344
+ */ const wokenTransitions = [];
345
+
346
+ /** Wake every parked transaction — for a site that knows a reporter stopped
347
+ * counting but not whose (a boundary reset). */ function wakeParked() {
348
+ for (const e of transitions) wokenTransitions.includes(e) || wokenTransitions.push(e);
349
+ schedule();
350
+ }
351
+
352
+ /**
353
+ * Boundaries whose `on` dependencies notified this flush (#3540; a Set:
354
+ * many notifications, one re-arm). The notification arrives inside a pass,
355
+ * at the height of the `on` reads — before the readers the write put in
356
+ * flight are registered — so the re-arm waits for the heap and runs before
357
+ * the verdict (GlobalQueue.run → drainRearms), still under the write's
358
+ * transaction: the release is seen by the verdict that follows, and the
359
+ * staged fallback swap lands with the write's frame.
360
+ */ const pendingRearms = new Set;
361
+
362
+ function queueRearm(e) {
363
+ pendingRearms.add(e);
364
+ schedule();
365
+ }
366
+
367
+ /** The drain (see pendingRearms): each boundary decides for itself, from
368
+ * what is pending under it now, whether re-arming means its fallback or
369
+ * nothing. Snapshot first: a re-arm can queue another — a write it makes
370
+ * notifying an `on` that reads it — which is the next pass's. */ function drainRearms() {
371
+ const e = Array.from(pendingRearms);
372
+ pendingRearms.clear();
373
+ for (let t = 0; t < e.length; t++) e[t].se();
374
+ }
375
+
376
+ /** Transactions a mainline tick has PROPOSED against (A34, #3494): a write to a
377
+ * node one of them holds — the same value or another — is a second proposal
378
+ * on a contested node, and the tick reveals with the hold ("both are
379
+ * suggesting a value; if one finished before the other that would be odd").
380
+ * Entered at the next flush's start, where the ambient batch is adopted;
381
+ * never from the write itself, which left `activeTransition` set across the
382
+ * caller's block and made creation after the write the transaction's (A29). */ const batchJoins = [];
383
+
384
+ /**
385
+ * Permanently halts the reactive system. Called when a user error escapes
386
+ * every boundary — app state is undefined at that point, so scheduling stops
387
+ * entirely rather than limping along with a half-applied update.
388
+ */
389
+ /**
390
+ * The key a root owner carries its client error hook under (`render`'s
391
+ * `onError`) — registered, so a runtime writes it with no import of the hook
392
+ * module (core/error-hooks.ts) and no property mangling in the way. Defined
393
+ * HERE, not there: a runtime that only writes the key must not retain the
394
+ * hook machinery (pay-for-use).
395
+ */ const ROOT_ERROR_HOOK = Symbol.for("solid-js/root-error-hook");
396
+
397
+ function haltReactivity(e) {
398
+ if (halted) return;
399
+ halted = true;
400
+ let t = "[REACTIVITY_HALTED]";
401
+ // Surface the cause here too: callers rethrow it, but a creation-time throw
402
+ // unwinds through ancestor recomputes that convert it to status instead of
403
+ // surfacing it (#2884), so the rethrow alone cannot guarantee visibility.
404
+ // Where the platform has one, hand the cause to its uncaught-error channel
405
+ // (`reportError` → `error` event → window.onerror / error monitoring): a
406
+ // halt that only reaches console.error leaves a page that LOOKS alive with
407
+ // nothing an app or its telemetry can act on (#3338 — an uncaught throw
408
+ // during the hydration render). The rethrow may reach the top as well in
409
+ // the non-swallowed cases; a duplicate report beats a silent one.
410
+ const n = e !== undefined && globalThis.reportError;
411
+ n || e === undefined ? console.error(t) : console.error(t, e);
412
+ n && n(e);
413
+ }
414
+
415
+ // Logs on the first write after a halt so a frozen interaction is traceable.
416
+ function notifyHalted() {
417
+ if (haltNotified) return;
418
+ haltNotified = true;
419
+ console.error("[REACTIVITY_HALTED]");
420
+ }
421
+
422
+ /** @internal Test/dev-reload hook. Revives scheduling after a halt. */ function resetErrorHalt() {
423
+ halted = false;
424
+ haltNotified = false;
425
+ }
426
+
427
+ // Identifies one child-traversal pass in `Queue.run` so a rescan after the
428
+ // child list shifts can tell "already run this pass" from "still pending".
429
+ let queueRunToken = 0;
430
+
431
+ class Queue {
432
+ _parent=null;
433
+ ti=[ [], [] ];
434
+ Xn=[];
435
+ ni=0;
436
+ created=clock;
437
+ addChild(e) {
438
+ this.Xn.push(e);
439
+ e._parent = this;
440
+ }
441
+ removeChild(e) {
442
+ const t = this.Xn.indexOf(e);
443
+ if (t >= 0) {
444
+ this.Xn.splice(t, 1);
445
+ e._parent = null;
446
+ }
447
+ }
448
+ notify(e, t, n, i) {
449
+ if (this._parent) return this._parent.notify(e, t, n, i);
450
+ return false;
451
+ }
452
+ run(e) {
453
+ if (this.ti[e - 1].length) {
454
+ const t = this.ti[e - 1];
455
+ this.ti[e - 1] = [];
456
+ runQueue(t, e);
457
+ }
458
+ // Effects run here can dispose owners, and disposal removes queues from
459
+ // this list — the running child itself, an earlier sibling, or several at
460
+ // once. A plain index walk then skips whatever shifted into the cursor.
461
+ // Stamping each child before it runs makes the pass idempotent, so a shift
462
+ // can be recovered by rescanning from the front and every child still runs
463
+ // exactly once. Children appended mid-pass carry a stale stamp and run,
464
+ // matching the previous live-array behaviour.
465
+ const t = this.Xn;
466
+ const n = ++queueRunToken;
467
+ for (let i = 0; i < t.length; ) {
468
+ const r = t[i];
469
+ if (r.ni !== n) {
470
+ r.ni = n;
471
+ r.run?.(e);
472
+ if (t[i] !== r) {
473
+ i = 0;
474
+ continue;
475
+ }
476
+ }
477
+ i++;
478
+ }
479
+ }
480
+ enqueue(e, t) {
481
+ if (e) {
482
+ // Route to lane's effect queue if we're in an optimistic recomputation
483
+ if (currentOptimisticLane) {
484
+ const n = findLane(currentOptimisticLane);
485
+ n.It[e - 1].push(t);
486
+ } else {
487
+ this.ti[e - 1].push(t);
488
+ }
489
+ }
490
+ schedule();
491
+ }
492
+ stashQueues(e) {
493
+ // Attribution hook: the parking transition's lane effects have run; its
494
+ // queues are being stashed. Root call only (children recurse below).
495
+ if (attrHooks !== null && this === globalQueue) attrHooks.holdEnd();
496
+ e.ti[0].push(...this.ti[0]);
497
+ e.ti[1].push(...this.ti[1]);
498
+ this.ti = [ [], [] ];
499
+ for (let t = 0; t < this.Xn.length; t++) {
500
+ let n = this.Xn[t];
501
+ let i = e.Xn[t];
502
+ if (!i) {
503
+ i = {
504
+ ti: [ [], [] ],
505
+ Xn: []
506
+ };
507
+ e.Xn[t] = i;
508
+ }
509
+ n.stashQueues(i);
510
+ }
511
+ }
512
+ restoreQueues(e) {
513
+ this.ti[0].push(...e.ti[0]);
514
+ this.ti[1].push(...e.ti[1]);
515
+ for (let t = 0; t < e.Xn.length; t++) {
516
+ const n = e.Xn[t];
517
+ let i = this.Xn[t];
518
+ if (i) i.restoreQueues(n);
519
+ }
520
+ }
521
+ }
522
+
523
+ class GlobalQueue extends Queue {
524
+ $t=false;
525
+ // The current transaction-shaped batch: a plain ambient batch while no
526
+ // transition is active, the active transition itself after initTransition.
527
+ m=createBatch();
528
+ static nt;
529
+ static ut;
530
+ static Rt;
531
+ static ii=null;
532
+ // Store-side hook: drops a keyless affects() mark's identity scope when the
533
+ // carrier node's last registration releases (wired by store.ts, mirroring
534
+ // _clearOptimisticStore).
535
+ static p=null;
536
+ // affects()-side hooks (wired by affects.ts, mirroring _update): the mark
537
+ // engine — count/register/release — lives with the feature. Every call site
538
+ // is gated by state only that module creates, so `!` invocations are safe
539
+ // once the gate holds.
540
+ static G=null;
541
+ static M=null;
542
+ static j=null;
543
+ // External-source bridge (wired by enableExternalSource(); null while no
544
+ // config is active — including after _resetExternalSourceConfig()).
545
+ static Bt=null;
546
+ static Jt=null;
547
+ // Verdict-layer hooks (wired by verdict.ts when isPending()/latest() are
548
+ // imported; null in apps that never use them). Call sites either guard for
549
+ // null or sit behind state only the verdict layer can create (`!` is safe
550
+ // there: `_pendingSignal`/`_latestValueComputed` are only ever assigned by
551
+ // verdict.ts, and `pendingCheckActive`/`latestReadActive` only flip inside
552
+ // isPending()/latest()).
553
+ static Le=null;
554
+ static Ue=null;
555
+ static qe=null;
556
+ static Qn=null;
557
+ static An=null;
558
+ static Cn=null;
559
+ static Hn=null;
560
+ static At=null;
561
+ static k=null;
562
+ static ri=null;
563
+ // Re-asks probes whose verdict was provisionally suppressed by a fresh read
564
+ // of a held value, once the transaction gains an async blocker (#3028).
565
+ static si=null;
566
+ // Optimistic-engine hooks (wired by core/optimistic.ts via
567
+ // installOptimisticEngine(), called from verdict.ts / createOptimistic /
568
+ // createOptimisticStore — every module that can create optimistic state).
569
+ // Call sites are gated by state only the engine can create: an
570
+ // `_overrideValue` slot, a lane in `activeLanes`, an `_optimisticNodes`
571
+ // entry, or a non-null `currentOptimisticLane`, so `!` invocations are safe
572
+ // once the gate holds.
573
+ static Ln=null;
574
+ static Bn=null;
575
+ static Yn=null;
576
+ static Wn=null;
577
+ static Kn=null;
578
+ /** Patch-channel optimistic drain (next/patch.ts): optimistic emissions
579
+ * apply at lane-effect timing — visible in flight, unlike the regular
580
+ * effect queues an action stashes. Injected; null when unused. */
581
+ static Zn=null;
582
+ static Pn=null;
583
+ static Gn=null;
584
+ /** Is the node routed through a LIVE lane (`resolveLane`)? read()'s reveal
585
+ * carve-out asks before showing a foreign-held pending node's committed
586
+ * value: a lane-derived flight's inputs are already revealed through the
587
+ * lane (#3334). Gated on CONFIG_HAS_LANE, which only the engine sets. */
588
+ static Rn=null;
589
+ static On=null;
590
+ static _t=null;
591
+ static Ot=null;
592
+ /** Authoritative-view reader wakeup: installed by until() and refresh() before
593
+ * their first read. Call sites are gated by CONFIG_AUTHORITATIVE_OBSERVED, which
594
+ * only such a reader's carve-out read can set, so `!` invocations are safe once
595
+ * the gate holds (#3303). */
596
+ static Xt=null;
597
+ static St=null;
598
+ /** A18 supersession (#3331): own-source truth `value` landed under an active
599
+ * override. The engine decides whether the graph re-derives — the value
600
+ * differs from the override and is not a stale (older-action) answer (mark
601
+ * the node, demote its lane cascade, notify), or returns to it after an
602
+ * earlier differing arrival (clear the mark, notify) — and owns the
603
+ * authoritative-observer wake for a silent confirm. Installed with the
604
+ * optimistic engine; only reachable on a node that has an override. */
605
+ static Qe=null;
606
+ /** The flush's pre-verdict step (#3427): once the transaction's action
607
+ * bodies have all ended and nothing authoritative is left in flight, the
608
+ * engine supersedes every override still in force with the truth it
609
+ * reverts to, so the graph re-derives from it now, as the transaction's
610
+ * held work, instead of after the flights the overrides fed have landed.
611
+ * True when it superseded something: the caller re-runs the heap ahead of
612
+ * the verdict. The engine owns every gate (acted, actions drained, has
613
+ * overrides, no store edits, no authoritative flight); null without it. */
614
+ static zn=null;
615
+ /** read()'s value for a TRACKED reader of a superseded node (#3331): the
616
+ * staged truth, unless the reader is a stale (render) reader of another
617
+ * transaction — then the displayed override, as it keeps a foreign
618
+ * transaction's committed value over its staged write. */
619
+ /** A tracked read of an active override: the lane outside-view rule
620
+ * (#3460) and the A18 supersession selection (#3331) — see optimistic.ts. */
621
+ static Dn=null;
622
+ /** A lane pass's publish for a memo (#3479, lanes stage): the speculative
623
+ * result becomes a DERIVED override, `_value` stays committed — see
624
+ * optimistic.ts laneOverride. Set with the engine, which a lane implies. */
625
+ static je=null;
626
+ /** Verdict-layer recompute in progress (companion creation, latest()/
627
+ * isPending() pulls): never born held — see core.ts enterStagedRead. */
628
+ static Tn=false;
629
+ /** setSignal's authoritative (projection-write) landing on an override-
630
+ * covered node (#3331 store twin): stage the truth for its transaction's
631
+ * commit whatever its relation to the committed value — a landing equal to
632
+ * committed still differs from the override — then _supersedeOverride
633
+ * decides. Installed with the optimistic engine; only reachable on a node
634
+ * that has an override — or, from `mapArray` once a lane pass has run over
635
+ * the map, on a per-slot signal (never CONFIG_OPTIMISTIC): the slot arm
636
+ * publishes a lane pass's write as the slot's derived override, lands a
637
+ * plain pass's write over one, and is the plain `setSignal` otherwise (F1,
638
+ * see optimistic.ts landOnOverride). */
639
+ static Vn=null;
640
+ static Jn=null;
641
+ flush() {
642
+ if (this.$t) return;
643
+ // Fast drain: nothing in flight but plain pending commits — no dirty
644
+ // computeds, no queued effects, no child queues, no transitions/lanes/
645
+ // optimistic state. Commit and go; anything a commit hook schedules
646
+ // (companion snaps, store folds notifying subs) re-arms `scheduled`
647
+ // below and the outer drain loop takes the full spine next round.
648
+ if (activeTransition === null && dirtyQueue.EE < dirtyQueue.st && this.ti[0].length === 0 && this.ti[1].length === 0 && this.Xn.length === 0 && !wokenTransitions.length && !batchJoins.length && // a join must drain in its own tick (#3519 review)
649
+ canUseSimpleSyncFlush(this)) {
650
+ this.$t = true;
651
+ try {
652
+ // A28: companions of nodes written since the last flush mirror the
653
+ // flushed world — re-synced inside the running window (the write is
654
+ // "flushed" from here on).
655
+ resyncUnflushedCompanions();
656
+ // Sweep first: unobserved() pulls swept nodes out of the dirty heap,
657
+ // so a dormant memo dirtied in the same tick is reclaimed instead of
658
+ // recomputed (matching the old inline dispose-on-read counts).
659
+ sweepDormant();
660
+ commitPendingNodes();
661
+ } finally {
662
+ this.$t = false;
663
+ }
664
+ clock++;
665
+ scheduled = dirtyQueue.EE >= dirtyQueue.st || this.ti[0].length !== 0 || this.ti[1].length !== 0 || this.m.In.length !== 0;
666
+ return;
667
+ }
668
+ this.$t = true;
669
+ resyncUnflushedCompanions();
670
+ // A28, see above
671
+ try {
672
+ // The tick proposed against a hold (#3494): adopt its batch into it.
673
+ // Inside the try: the adoption runs user comparators (the no-proposal
674
+ // drop), and a throw there must not leave `_running` set.
675
+ while (batchJoins.length) this.initTransition(batchJoins.pop());
676
+ if (false) ;
677
+ // Before runHeap for the same reason as the fast drain above; late
678
+ // subscribers (an effect reading a swept memo this flush) revive it,
679
+ // which is the pay-for-use contract.
680
+ sweepDormant();
681
+ runHeap(dirtyQueue, GlobalQueue.nt);
682
+ // The action bodies are over: the overrides they leave in force
683
+ // revert at this settle, and the correction is this transaction's
684
+ // held work — re-derived here, under it, ahead of the verdict — not a
685
+ // waterfall after the flights the overrides fed (#3427). After the
686
+ // heap, not before: a synchronous body's own writes (a refresh that
687
+ // puts an override node's source in flight) are judged applied.
688
+ if (activeTransition && GlobalQueue.zn?.(activeTransition)) runHeap(dirtyQueue, GlobalQueue.nt);
689
+ // Re-arm the boundaries whose `on` notified this pass (pendingRearms):
690
+ // after the heap — what the notification put in flight is registered —
691
+ // and before the verdict, under the transaction that carried it. The
692
+ // boundary releases its hold and stages its fallback swap with the
693
+ // frame; the heap re-runs so its output pass is staged too, ahead of
694
+ // the verdict that commits or parks it (#3540).
695
+ if (pendingRearms.size) {
696
+ drainRearms();
697
+ runHeap(dirtyQueue, GlobalQueue.nt);
698
+ }
699
+ if (activeTransition) {
700
+ // A boundary whose fallback read something not ready is judged before
701
+ // the verdict, under the transaction (boundaries.ts `_judgeHeld`,
702
+ // #3540): its output is pending on that read and holds the frame,
703
+ // and only a sweep re-runs it — the commit sweep, after the verdict
704
+ // its own read keeps parking. Ready, it stages `_disabled` false with
705
+ // the frame; the heap re-runs so the output drops the read ahead of
706
+ // the verdict.
707
+ if (this.Xn.length) {
708
+ checkBoundaryChildren(this, true);
709
+ if (dirtyQueue.EE >= dirtyQueue.st) runHeap(dirtyQueue, GlobalQueue.nt);
710
+ }
711
+ const e = transitionComplete(activeTransition);
712
+ if (!e) {
713
+ const e = activeTransition;
714
+ // Parked: the unchanged passes' inputs are held; their tails stay (A30).
715
+ heldTrims.length = 0;
716
+ // When the parking batch IS the transition, all of its writes commit
717
+ // only with it — every zombie recompute they queued would run against
718
+ // a world the zombie never displays (zombies render mainline until
719
+ // commit), so cancel them instead of running them. Only an ambient
720
+ // batch's mainline writes (the #2916 shape below) legitimately reach
721
+ // zombies here. Height-adjust entries still process normally: a
722
+ // dirtied one keeps its height flag and falls through to runHeap's
723
+ // adjustHeight path on the next pass of the bucket.
724
+ runHeap(zombieQueue, this.m === e ? cancelZombieRecompute : GlobalQueue.nt);
725
+ // Detach: the stashed transition keeps its batch; ambient work that
726
+ // follows lands in a fresh one. If the batch is already a separate
727
+ // ambient one — action done() restored activeTransition without
728
+ // adopting the batch, and an ordinary write landed there before
729
+ // the scheduled flush (#2916) — keep it: replacing it would strand
730
+ // its queued pending nodes with held _pendingValues forever.
731
+ if (this.m === e) currentBatch = this.m = createBatch();
732
+ // Run lane effects immediately (before stashing) - lanes with no pending async
733
+ if (activeLanes.size) {
734
+ GlobalQueue.Kn(EFFECT_RENDER);
735
+ GlobalQueue.Kn(EFFECT_USER);
736
+ }
737
+ this.stashQueues(e.ei);
738
+ clock++;
739
+ // A kept ambient batch may hold pending nodes (#2916): stay
740
+ // scheduled so the outer drain loop commits them via the plain
741
+ // flush path instead of leaving them until the next natural flush.
742
+ scheduled = dirtyQueue.EE >= dirtyQueue.st || this.m.In.length > 0;
743
+ reassignPendingTransition(e.In);
744
+ activeTransition = null;
745
+ finalizePureQueue(null, true);
746
+ return;
747
+ }
748
+ const t = activeTransition;
749
+ const n = this.m;
750
+ n !== t && n.In.push(...t.In);
751
+ this.restoreQueues(t.ei);
752
+ transitions.delete(t);
753
+ activeTransition = null;
754
+ reassignPendingTransition(n.In);
755
+ finalizePureQueue(t);
756
+ if (n === t) {
757
+ // Drop the dead Transition wrapper but keep its (drained) containers
758
+ // as the ambient batch — late registrations during finalization live
759
+ // there and must survive to the next flush.
760
+ const e = createBatch();
761
+ e.In = n.In;
762
+ e.bn = n.bn;
763
+ e.A = n.A;
764
+ e.En = n.En;
765
+ currentBatch = this.m = e;
766
+ }
767
+ } else {
768
+ if (canUseSimpleSyncFlush(this)) {
769
+ commitPendingNodes();
770
+ if (dirtyQueue.EE >= dirtyQueue.st) {
771
+ runHeap(dirtyQueue, GlobalQueue.nt);
772
+ commitPendingNodes();
773
+ }
774
+ } else {
775
+ // Parked transactions elsewhere: their owners' zombies render
776
+ // mainline until the commit that disposes them, so a mainline write
777
+ // reaches them here (#2916, #3463). Commit THIS flush's pending nodes
778
+ // first (#3546): a zombie whose owner commits now is disposed by that
779
+ // commit and never reruns — the same fate it has when no transaction
780
+ // is parked, where this queue is not run at all. Run before the
781
+ // commit, it reran and notified its owner through the previous
782
+ // pass's dependency tail (kept by A30 until the commit trims it),
783
+ // and the owner recomputed a second time with identical inputs,
784
+ // creating and disposing one more child per write. Only the zombies
785
+ // of actually parked owners survive the commit and rerun; the
786
+ // finalize's own commit picks up whatever those reruns stage.
787
+ if (transitions.size) {
788
+ commitPendingNodes();
789
+ runHeap(zombieQueue, GlobalQueue.nt);
790
+ }
791
+ finalizePureQueue();
792
+ }
793
+ }
794
+ clock++;
795
+ // Check if finalization added items to the heap (from optimistic reversion).
796
+ // Finalization may also have ENTERED a transaction (a commit hook, boundary
797
+ // sweep or recompute wrote a node it owns): effects computed under it
798
+ // since are its to apply, not this flush's — runEffect leaves them queued
799
+ // and the next pass parks them with it (#3319). Everything computed
800
+ // mainline applies now. A write the finalize staged in the ambient
801
+ // batch with no subscriber to dirty (an optimistic store settle's
802
+ // keyset bump under a reader that never tracks the key set) is work
803
+ // too — the fast drain and the park exit already count it — so the
804
+ // next round commits it and the woken re-entry below does not adopt
805
+ // it into a parked transaction it never belonged to (matrix F6).
806
+ scheduled = dirtyQueue.EE >= dirtyQueue.st || activeTransition !== null || this.m.In.length !== 0;
807
+ // Run lane effects first (for ready lanes), then regular effects
808
+ activeLanes.size && GlobalQueue.Kn(EFFECT_RENDER);
809
+ this.run(EFFECT_RENDER);
810
+ activeLanes.size && GlobalQueue.Kn(EFFECT_USER);
811
+ this.run(EFFECT_USER);
812
+ if (false) ;
813
+ if (false && !scheduled && !activeTransition && transitions.size === 0 && activeLanes.size === 0) ;
814
+ if (false) ;
815
+ } finally {
816
+ // Re-enter a woken transaction (see wokenTransitions) only from an
817
+ // idle pass: entering adopts the ambient batch, and staged or dirty
818
+ // ambient work would be held behind flights it never read. `scheduled`
819
+ // is that test here — after the park exit as well as the normal one:
820
+ // it was recomputed from the heap and the ambient batch's staged nodes
821
+ // this pass, every write since re-armed it, and optimistic ambient
822
+ // nodes reverted with the finalize — so a wake in a pass with work
823
+ // simply falls to the next. (A staged node with no subscriber — the
824
+ // finalize's keyset bump under a length-only reader — used to be
825
+ // missed here: the wake adopted it, stamped it, and a later ambient
826
+ // write to the same node joined the parked transaction and never
827
+ // reverted; matrix F6.) Entering re-arms
828
+ // it itself; a dead (completed) wake is a bare return in
829
+ // initTransition, and the loop moves on to the next.
830
+ while (!scheduled && !activeTransition && wokenTransitions.length) this.initTransition(wokenTransitions.pop());
831
+ this.$t = false;
832
+ }
833
+ }
834
+ notify(e, t, n, i) {
835
+ // Only track async if the boundary is propagating STATUS_PENDING (not caught by boundary)
836
+ if (t & STATUS_PENDING) {
837
+ if (n & STATUS_PENDING) {
838
+ // Callers pass either nothing or this node's own `_x._error`, so `??`
839
+ // is exact (a null error falls back to the same null).
840
+ const t = i ?? e.o?._;
841
+ // A visibility-only mark notification (the affects() boundary
842
+ // channel) updates display state on its way up but must be invisible
843
+ // to completion accounting BY CONSTRUCTION: it never registers a
844
+ // reporter and never counts toward the loading-boundary diagnostic.
845
+ if (t?.l) return true;
846
+ if (t) {
847
+ // A reveal can discover a flight started in an earlier flush. Hold
848
+ // the staged writes with that reader (A15), even if the reader is
849
+ // new. Fresh/reset loading boundaries consume pending before it
850
+ // reaches here. A reader already parked in a transition must not
851
+ // open a second one.
852
+ if (!activeTransition && !e.me && currentBatch.In.length) this.initTransition();
853
+ if (activeTransition) {
854
+ const n = t.source;
855
+ let i = activeTransition.ae.get(n);
856
+ if (!i) activeTransition.ae.set(n, i = new Set);
857
+ const r = i.size;
858
+ i.add(e);
859
+ if (i.size !== r) {
860
+ schedule();
861
+ GlobalQueue.si?.(activeTransition);
862
+ }
863
+ }
864
+ }
865
+ }
866
+ return true;
867
+ }
868
+ return false;
869
+ }
870
+ initTransition(e) {
871
+ if (e) {
872
+ e = currentTransition(e);
873
+ // A finished transaction cannot be re-entered: its state is committed
874
+ // or reverted, so "rejoining" it (A26) is meaningless and re-activating
875
+ // it spins the drain loop (#3140). The refusal must be a bare return —
876
+ // redirecting the caller to a fresh batch would re-arm the loop with a
877
+ // new transaction identity each pass. Stamps are cleared at commit, so
878
+ // this is a belt for paths that hand over a chased-dead reference
879
+ // (merged chains, async settles racing completion).
880
+ if (e.Dt === true || e === activeTransition) return;
881
+ }
882
+ if (!e && activeTransition && activeTransition.Ge === clock) return;
883
+ if (!activeTransition) {
884
+ activeTransition = e ?? createBatch();
885
+ } else if (e) {
886
+ const t = activeTransition;
887
+ mergeTransitionState(e, t);
888
+ // Effects the outgoing transaction parked belong to the surviving one
889
+ // now: back onto the live queue, where this flush parks them under
890
+ // `transition` or runs them at its completion. The outgoing stash is
891
+ // never read again — the transaction is dead (#3310).
892
+ this.restoreQueues(t.ei);
893
+ transitions.delete(t);
894
+ activeTransition = e;
895
+ }
896
+ transitions.add(activeTransition);
897
+ activeTransition.Ge = clock;
898
+ const t = this.m;
899
+ if (t !== activeTransition) {
900
+ // Adopt the ambient batch into the transaction, then make the
901
+ // transaction the batch so later registrations land there directly.
902
+ // Pending and optimistic nodes are re-stamped as the transaction's;
903
+ // marks don't hijack the node's _transition — a mark on a plain signal
904
+ // must not entangle unrelated writes to it; the same rule holds one hop
905
+ // downstream: propagation never queues pended subscribers as pending
906
+ // nodes, see propagateAffectsMark, #2893.
907
+ // Adopted outside a flush: the staging is still unflushed — the stamp
908
+ // must not make it read as held-and-carried (CONFIG_ADOPTED_UNFLUSHED;
909
+ // the carrying flush clears it in reassignPendingTransition).
910
+ const e = this.$t ? 0 : CONFIG_ADOPTED_UNFLUSHED;
911
+ for (let n = 0; n < t.In.length; n++) {
912
+ const i = t.In[n];
913
+ // A tick that nets to the committed value proposed nothing (A34, #3494):
914
+ // `setShow(false); setShow(true)` beside a write that opens a hold
915
+ // left `show` staged at its own value, stamped, pending to the
916
+ // verdict, and its next mainline write held by a flight it never
917
+ // derived from. Unstage it here — its subscribers were walked at the
918
+ // write and re-derive the same value. Writes only: a signal's staging
919
+ // is always one, a computed's only under REACTIVE_MANUAL_WRITE
920
+ // (`createSignal(fn)`'s setter, #3519 review) — otherwise it is its
921
+ // pass's result, which may equal an uninitialized `undefined` (a
922
+ // born-held first pass). `_equals: false` opts out. The unstaging is
923
+ // the commit's own path (commitPendingNode with nothing staged): the
924
+ // manual-write flag, companions and the rest are cleaned up as a
925
+ // commit would, and the node is stamped nowhere. Unstamped
926
+ // only: a node already a transaction's — arriving here as a parked
927
+ // batch folds into a merge — carries a FLUSHED proposal a later
928
+ // rewrite brought back to the committed value; it is held, not
929
+ // proposal-free. Dropped, it kept the dead stamp, and the next write
930
+ // to it queued under the merged transaction a value the commit then
931
+ // skipped as another's (fuzzer latest-2 #1470, S3).
932
+ if (i.me === null && i._e !== NOT_PENDING && (!i.Se || i.oe & REACTIVE_MANUAL_WRITE && !(i.S & STATUS_UNINITIALIZED)) && i.Fe && i.Fe(i.ce, i._e)) {
933
+ i._e = NOT_PENDING;
934
+ commitPendingNode(i);
935
+ continue;
936
+ }
937
+ i.me = activeTransition;
938
+ i.C |= e;
939
+ activeTransition.In.push(i);
940
+ }
941
+ for (let e = 0; e < t.bn.length; e++) {
942
+ const n = t.bn[e];
943
+ n.me = activeTransition;
944
+ activeTransition.bn.push(n);
945
+ }
946
+ if (t.A.length) activeTransition.A.push(...t.A);
947
+ for (const e of t.En) activeTransition.En.add(e);
948
+ // Gated readers recorded against the ambient batch move with it: their
949
+ // replay-at-commit now happens at the transaction's completion.
950
+ if (t.Tt.size) {
951
+ for (const e of t.Tt) activeTransition.Tt.add(e);
952
+ t.Tt.clear();
953
+ }
954
+ currentBatch = this.m = activeTransition;
955
+ }
956
+ for (const e of activeLanes) {
957
+ if (!e.me) e.me = activeTransition;
958
+ }
959
+ // A transaction's ambient window is one flush. Entering must therefore
960
+ // guarantee a flush: a transaction opened with no writes (an action whose
961
+ // first statements only await) otherwise leaves activeTransition and the
962
+ // adopted batch armed across the async gap, and the next unrelated work
963
+ // to arrive — an optimistic store's authoritative landing, a plain async
964
+ // settle — is adopted into a transaction it has nothing to do with
965
+ // (#3141). The scheduled flush parks the incomplete transaction through
966
+ // the normal machinery and detaches the ambient slots first.
967
+ schedule();
968
+ }
969
+ }
970
+
971
+ function queuePendingNode(e) {
972
+ currentBatch.In.push(e);
973
+ if (!globalQueue.$t) markUnflushedStaged();
974
+ // A28
975
+ }
976
+
977
+ // Sticky: flips true on the first refresh() ever (the only setter of
978
+ // REACTIVE_REASK) so the hot notification loop skips the per-subscriber flag
979
+ // clear entirely in apps that never refresh.
980
+ let reaskArmed = false;
981
+
982
+ /** §12d: bumped by every recompute and every new subscriber edge. A node's
983
+ * staged-rewrite skip is sound only while NOTHING recomputed or linked since
984
+ * its last notify — a mid-batch pull can clean a marked subscriber, and a
985
+ * skipped re-write would leave it stale. */ let notifyEpoch = 0;
986
+
987
+ function bumpNotifyEpoch() {
988
+ notifyEpoch++;
989
+ }
990
+
991
+ function armReaskClear() {
992
+ reaskArmed = true;
993
+ }
994
+
995
+ /** Provenance of the work currently running (A18 supersession, #3331): the
996
+ * invocation sequence of the action whose ambient window this is — set by
997
+ * action() for each slice; the flush that ends the window clears it — or,
998
+ * inside an async landing, the sequence captured when that flight was
999
+ * registered (asyncWrite sets it for the landing's synchronous propagation,
1000
+ * so a sync recompute downstream of the landing — an optimistic wrapper over
1001
+ * the async source — derives under the flight's provenance, and flights it
1002
+ * registers inherit it). 0 is mainline: no action, always the current
1003
+ * question. An override stamps this at its write (`_overrideStamp`); an
1004
+ * answer whose flight an OLDER action issued is a stale question the user
1005
+ * has since changed — it holds silently to commit instead of superseding. A
1006
+ * slow source must not leak back in over a newer intent. Transactions merge,
1007
+ * so the transition object cannot say WHICH action asked; this can. */ let origin = 0;
1008
+
1009
+ function setOrigin(e) {
1010
+ const t = origin;
1011
+ origin = e;
1012
+ return t;
1013
+ }
1014
+
1015
+ function insertSubs(e, t = false) {
1016
+ // §12d: stamp before walking — setSignal's staged-rewrite fast path skips
1017
+ // the next walk for this node while the epoch holds (marking is idempotent).
1018
+ e.Lt = notifyEpoch;
1019
+ // Get source lane: prefer node's own lane over current context
1020
+ // This is important for isPending signals which need their own lane to flush immediately
1021
+ // Presence bits gate the optional-slot probes (see constants.ts): one
1022
+ // masked read of the always-present _config instead of missing-property
1023
+ // lookups in the hottest notify loop. Bits are sticky — the field read
1024
+ // stays authoritative when a bit is set.
1025
+ const n = e.C;
1026
+ const i = (n & CONFIG_HAS_LANE ? e.o?.Ce : undefined) || currentOptimisticLane;
1027
+ const r = (n & CONFIG_HAS_SNAPSHOT) !== 0 && e.o?.ct !== undefined;
1028
+ const s = reaskArmed;
1029
+ // Observe-tier fan-out: this walk visits every subscriber edge anyway, so
1030
+ // the graph-size count is one local increment here and no field anywhere.
1031
+ let o = 0;
1032
+ for (let n = e.u; n !== null; n = n.Pe) {
1033
+ const e = n.Ae;
1034
+ o++;
1035
+ // A value-change notification is a new question for the subscriber: any
1036
+ // pending re-ask mark (refresh) it carried is superseded.
1037
+ if (s) e.oe &= ~REACTIVE_REASK;
1038
+ // Missed-wake latch (#3037): this write is landing while the subscriber
1039
+ // is mid-recompute (a nested pull committing beneath its reads), and the
1040
+ // heap refuses RECOMPUTING nodes. A gen-current link means the pass
1041
+ // already validated this dep — the value it read is now stale — so latch
1042
+ // for recompute's tail to reschedule. Untouched links need no latch (the
1043
+ // pass either re-reads them fresh or trims them), and neither does the
1044
+ // tail link: it is the read IN FLIGHT — read() links before it pulls, so
1045
+ // this very commit is what that read returns.
1046
+ if (e.oe & REACTIVE_RECOMPUTING_DEPS && n.Ze === e.We && n !== e.Je) e.oe |= REACTIVE_MISSED_WAKE;
1047
+ if (r && e.C & CONFIG_IN_SNAPSHOT_SCOPE) {
1048
+ e.oe |= REACTIVE_SNAPSHOT_STALE;
1049
+ continue;
1050
+ }
1051
+ if (t && i) {
1052
+ e.oe |= REACTIVE_OPTIMISTIC_DIRTY;
1053
+ assignOrMergeLane(e, i);
1054
+ } else if (t) {
1055
+ e.oe |= REACTIVE_OPTIMISTIC_DIRTY;
1056
+ // No source lane means reversion - clear subscriber's lane so effects go to regular queue
1057
+ if (e.o) e.o.Ce = undefined;
1058
+ }
1059
+ enqueueSub(e);
1060
+ }
1061
+ if (o >= GRAPH_SIZE_WARN_AT) noteFanOut(e, o);
1062
+ }
1063
+
1064
+ function commitPendingNode(e) {
1065
+ const t = e;
1066
+ if (!t.Se) {
1067
+ if (e._e !== NOT_PENDING) {
1068
+ e.ce = e._e;
1069
+ e._e = NOT_PENDING;
1070
+ }
1071
+ if (e.C & CONFIG_HAS_COMPANIONS) GlobalQueue.Qn(e);
1072
+ return;
1073
+ }
1074
+ if (e._e !== NOT_PENDING) {
1075
+ e.ce = e._e;
1076
+ e._e = NOT_PENDING;
1077
+ // A node born held (recompute) initializes at this commit.
1078
+ t.S &= ~STATUS_UNINITIALIZED;
1079
+ // Set _modified for effects, but not for tracked effects (they handle their own scheduling)
1080
+ if (e.He && e.He !== EFFECT_TRACKED) e.it = true;
1081
+ // A quiet re-ask classification preserved through a held landing dies
1082
+ // with the value commit — the commit IS the reveal (#3178). Gated on the
1083
+ // staged value: status propagation queues pending nodes whose windows
1084
+ // are still OPEN (no staged value), and their live classification must
1085
+ // survive this sweep.
1086
+ if (e.o) e.o.ve = false;
1087
+ }
1088
+ // The committed hold is the first observable answer for a loading-window
1089
+ // node — the window closes here, not at compute time (#2990). Unconditional
1090
+ // store to an always-present computed slot.
1091
+ t.ye = false;
1092
+ t.oe &= ~REACTIVE_MANUAL_WRITE;
1093
+ // The children this commit publishes are the frame's now (#3404) — and so
1094
+ // are the dependencies of the pass that produced the value: the previous
1095
+ // frame's tail goes (A30, #3410; `recompute` left it for a staged pass). Only
1096
+ // after a clean pass: `_error` is cleared by a clean pass or by the node's
1097
+ // own landing (whose pass was clean), so a set `_error` means the last pass
1098
+ // threw, kept its full list, and `_depsTail` marks where it stopped.
1099
+ if (t.o?._ == null) trimStaleDeps(t);
1100
+ // A LANE frame still parked here (#3662) rode a hold that stashed the run
1101
+ // that would have retired it: this commit applies that run, so it goes too.
1102
+ t.C &= -68157441;
1103
+ if (!(t.S & STATUS_PENDING)) t.S &= ~STATUS_UNINITIALIZED;
1104
+ // A flight this commit leaves in the air (unobserved, or observed only by
1105
+ // a boundary) now has PUBLISHED inputs: its committed value is stale
1106
+ // against the frame. read()'s reveal carve-out keys on the mark (#3305).
1107
+ else e.C |= CONFIG_INPUTS_PUBLISHED;
1108
+ if (t.o != null && (t.o.dt !== null || t.o.Et !== null)) GlobalQueue.ut(t, false, true);
1109
+ if (e.C & CONFIG_HAS_COMPANIONS) GlobalQueue.Qn(e);
1110
+ }
1111
+
1112
+ // Store commit hook (INTERNALS-STORE-STATE.md §3): installed by the store
1113
+ // module at init (same treeshakeable pattern as _resolveOptimistic /
1114
+ // _clearOptimisticStores). Folds committed store-node values into their
1115
+ // backing objects at the same moment pending values commit — the single
1116
+ // mutation point of the owned-raw model.
1117
+ let storeCommitHook = null;
1118
+
1119
+ function setStoreCommitHook(e) {
1120
+ storeCommitHook = e;
1121
+ }
1122
+
1123
+ /** Held truth committed this finalize, awaiting its post-revert wake (see
1124
+ * finalizePureQueue): the commit IS the reveal, but subscribers must not
1125
+ * re-derive until the settling transaction's optimistic overrides have
1126
+ * reverted — a commit-time wake recomputes them in the window where
1127
+ * confirming truth is committed and the override still displays, a torn
1128
+ * frame no timeline contains. */ const heldRevealed = [];
1129
+
1130
+ /** Unchanged passes with a stale dependency tail, waiting on this flush's
1131
+ * verdict (A30, #3469). A pass that changed nothing replaced nothing either —
1132
+ * and cannot know at its own tail whether the flush that ran it will park:
1133
+ * parked, its inputs are held and the committed frame still derives from the
1134
+ * tail (`b() ? b() : a()` computed `1` from the held `b`, equal to the `1` it
1135
+ * had from `a` — with `a` trimmed, the mainline `a = 2` never reached it).
1136
+ * Trimmed when the flush commits; dropped with a park, the tail stays linked
1137
+ * until a committing pass trims it (one spurious recompute at most). */ const heldTrims = [];
1138
+
1139
+ function commitPendingNodes() {
1140
+ while (heldTrims.length) trimStaleDeps(heldTrims.pop());
1141
+ const e = currentBatch.In;
1142
+ for (let t = 0; t < e.length; t++) {
1143
+ const n = e[t];
1144
+ commitPendingNode(n);
1145
+ // The stamp dies with the commit (#3143) — symmetric with
1146
+ // resolveOptimisticNodes clearing optimistic stamps. A stamp outliving
1147
+ // its transaction let any later write (even a value-equal no-op, which
1148
+ // re-opens before the equality bail) resurrect the finished transaction;
1149
+ // a boundary flag rewritten every finalize pass then spun the drain loop
1150
+ // forever (#3140). The held-truth mark dies the same death — the commit
1151
+ // IS the reveal — but its wake defers to the post-revert pass: ordinary
1152
+ // subscribers were masked to committed all hold (some re-derived against
1153
+ // that old view and cached it), and commits are otherwise silent
1154
+ // (staging already notified), so without a wake they'd hold the old
1155
+ // world forever.
1156
+ n.me = null;
1157
+ if (n.C & CONFIG_HELD_TRUTH) {
1158
+ n.C &= ~CONFIG_HELD_TRUTH;
1159
+ heldRevealed.push(n);
1160
+ }
1161
+ }
1162
+ e.length = 0;
1163
+ storeCommitHook?.();
1164
+ }
1165
+
1166
+ function finalizePureQueue(e = null, t = false) {
1167
+ // For incomplete transitions, skip pending resolution and optimistic reversion
1168
+ // For completing transitions or no-transition, resolve pending and revert optimistic
1169
+ const n = currentBatch;
1170
+ const i = !t;
1171
+ if (i) commitPendingNodes();
1172
+ // A parked finalize sweeps nothing: the boundaries' staged swaps are the
1173
+ // transaction's, and a boundary whose own output parks the verdict was
1174
+ // judged under it in run(), ahead of the verdict (#3540).
1175
+ if (!t && globalQueue.Xn.length) checkBoundaryChildren(globalQueue);
1176
+ // Contested effects (#3322) re-derive from the world this commit just
1177
+ // produced. Ahead of the heap run — not the post-heap gated replay — so
1178
+ // the recompute and the effect phase land in this same pass and the value
1179
+ // the other transaction wrote into the slot is never published.
1180
+ // (No clear: a completed transition is never finalized again.)
1181
+ // A transaction whose settle reverts optimism re-derives them post-revert
1182
+ // instead (below, with the gated replay): between commitPendingNodes and
1183
+ // _resolveOptimistic the truth is committed but the overrides still
1184
+ // display, and a re-derive here would compose the two — the #3164 tear,
1185
+ // one window later. The slot meanwhile holds the frame that is on screen.
1186
+ const r = e?.ht;
1187
+ const s = i && (e ?? n).bn.length !== 0;
1188
+ if (r && !s) for (const e of r) if (!(e.oe & REACTIVE_DISPOSED)) enqueueSub(e);
1189
+ const o = dirtyQueue.EE >= dirtyQueue.st;
1190
+ if (o) runHeap(dirtyQueue, GlobalQueue.nt);
1191
+ if (i) {
1192
+ // Boundary checks, commit hooks and recomputes can enter a transaction,
1193
+ // which adopts the batch this finalize was settling: nothing batch-derived
1194
+ // may be committed or reverted here — the entered transaction owns it now
1195
+ // (#3319). A completing transaction's OWN containers are a different
1196
+ // matter: when the ambient batch was separate from it (the #2916 shape),
1197
+ // adoption never touched them and it must still settle them; when the
1198
+ // batch WAS the completing transaction, adoption re-stamped its contents
1199
+ // into the entered one and there is nothing left to settle.
1200
+ if (currentBatch !== n) {
1201
+ if (e === null || e === n) return;
1202
+ } else if (o) commitPendingNodes();
1203
+ // The settling batch: the completing transaction's, or the ambient one.
1204
+ const t = e ?? n;
1205
+ // Optimistic reversion: a non-empty batch means _optimisticWrite ran,
1206
+ // which installed the engine's hooks.
1207
+ if (t.bn.length) GlobalQueue.Bn(t.bn);
1208
+ if (r && s) {
1209
+ for (const e of r) if (!(e.oe & REACTIVE_DISPOSED)) enqueueSub(e);
1210
+ schedule();
1211
+ }
1212
+ // Replay entanglement: subs recorded by the read-time gate get rescheduled
1213
+ // so they re-run with the now-committed values visible. The ambient batch
1214
+ // replays too — laneReadsCommitted records readers whose committed-view
1215
+ // read hid a same-tick plain write that just committed above (#2963).
1216
+ if (t.Tt.size) {
1217
+ for (const e of t.Tt) {
1218
+ if (e.oe & REACTIVE_DISPOSED) continue;
1219
+ enqueueSub(e);
1220
+ }
1221
+ t.Tt.clear();
1222
+ // A completing transition keeps the outer flush loop alive by itself;
1223
+ // the ambient batch needs the re-arm or the replay sits in the heap
1224
+ // until the next unrelated write.
1225
+ schedule();
1226
+ }
1227
+ // Declared motion ends with the transaction: settle (or plain flush end
1228
+ // for ambient marks) releases each registration's refcount. A non-empty
1229
+ // batch means registerAffectsMark ran, which installed the hook. Marks
1230
+ // held boundary display state through the visual channel, and their
1231
+ // release is the display-state update point — re-run the boundary sweep
1232
+ // (the earlier sweep above ran while the marks were still live).
1233
+ if (t.A.length) {
1234
+ GlobalQueue.G(t.A);
1235
+ if (globalQueue.Xn.length) checkBoundaryChildren(globalQueue);
1236
+ }
1237
+ // A non-empty set means trackOptimisticStore ran, which installed the
1238
+ // hook; the hook iterates, clears, and schedules (keeping the loop out of
1239
+ // core lets esbuild shake it — rollup already folds the null guard). The
1240
+ // completing transition scopes the clear to its own layer keys (#2899).
1241
+ if (t.En.size) GlobalQueue.ii(t.En, e);
1242
+ // Held-truth reveal wake (#3164), post-revert by construction: this
1243
+ // finalize committed confirming truth whose subscribers were masked all
1244
+ // hold — some re-derived against the committed view (the staging, or a
1245
+ // confirming carrier's landing, notified them as a plain write) and
1246
+ // cached it, and stash-restored applies may carry those torn values.
1247
+ // Waking and recomputing HERE — after _resolveOptimistic and the store
1248
+ // clears above — means every apply paints the settled view; a wake at
1249
+ // commit time would recompute them in the window where truth is
1250
+ // committed but the settling transaction's overrides still display.
1251
+ if (heldRevealed.length !== 0) {
1252
+ while (heldRevealed.length) insertSubs(heldRevealed.pop());
1253
+ if (dirtyQueue.EE >= dirtyQueue.st) {
1254
+ runHeap(dirtyQueue, GlobalQueue.nt);
1255
+ commitPendingNodes();
1256
+ }
1257
+ }
1258
+ sweepTransientStoreNodes();
1259
+ // Lanes only enter activeLanes through the engine's getOrCreateLane.
1260
+ if (activeLanes.size) GlobalQueue.Wn(e);
1261
+ }
1262
+ }
1263
+
1264
+ /** The boundary sweep: the commit's (`_checkSources`), or — `held`, before
1265
+ * the verdict (#3540) — the one for a collecting boundary whose output is
1266
+ * pending on its fallback's read (`_judgeHeld`). */ function checkBoundaryChildren(e, t) {
1267
+ for (const n of e.Xn) {
1268
+ t ? n.Re?.() : n.Te?.();
1269
+ checkBoundaryChildren(n, t);
1270
+ }
1271
+ }
1272
+
1273
+ /**
1274
+ * Count of live `affects()` registrations across the system (including
1275
+ * store-scope inherited marks). Gates the read-path mark check in `read()` so
1276
+ * graphs that never use the feature pay one integer compare.
1277
+ */ let activeAffectsMarks = 0;
1278
+
1279
+ /**
1280
+ * Counter mutation seam for the mark engine in affects.ts: an imported `let`
1281
+ * binding is read-only, and the read-path gate above must stay a plain module
1282
+ * variable so `read()` pays one integer compare, not a function call.
1283
+ *
1284
+ * @internal
1285
+ */ function shiftAffectsMarks(e) {
1286
+ activeAffectsMarks += e;
1287
+ }
1288
+
1289
+ function reassignPendingTransition(e) {
1290
+ for (let t = 0; t < e.length; t++) {
1291
+ e[t].me = activeTransition;
1292
+ e[t].C &= ~CONFIG_ADOPTED_UNFLUSHED;
1293
+ // this flush carried it
1294
+ }
1295
+ }
1296
+
1297
+ const globalQueue = new GlobalQueue;
1298
+
1299
+ // Hot-path mirror of `globalQueue._batch`: `queuePendingNode` runs once per
1300
+ // staged write and `commitPendingNodes` once per flush, and the extra
1301
+ // property hop through `_batch` was a measured instruction-count regression
1302
+ // (CodSpeed update1to1, PR #2905). The field stays authoritative for
1303
+ // cross-module readers; every `_batch` assignment updates both.
1304
+ let currentBatch = globalQueue.m;
1305
+
1306
+ function flush(e) {
1307
+ // Inside an action body the drain is incoherent (#3333): the action's
1308
+ // writes are held by its transaction until it settles, so a drain can't
1309
+ // reveal them — and the loop below only exits once `activeTransition` is
1310
+ // null, so it would PARK the transaction mid-slice and every write after it
1311
+ // in the body would land as a plain, committed write. The reporter's
1312
+ // "leading flush()" workaround was exactly that leak. Prod: run `fn` if
1313
+ // given (its writes stay in the transaction) and skip the drain.
1314
+ if (actionStepDepth > 0) {
1315
+ return e ? e() : undefined;
1316
+ }
1317
+ if (e) {
1318
+ syncDepth++;
1319
+ try {
1320
+ return e();
1321
+ } finally {
1322
+ // Decrement even if the drain throws (a throwing effect): a leaked
1323
+ // syncDepth would stop `schedule()` from ever queuing a microtask again.
1324
+ try {
1325
+ flush();
1326
+ } finally {
1327
+ syncDepth--;
1328
+ }
1329
+ }
1330
+ }
1331
+ if (globalQueue.$t) {
1332
+ return;
1333
+ }
1334
+ if (halted) return;
1335
+ // Attribution: whether this call drained anything, so `flushEnd` fires once
1336
+ // per real drain and never for a no-op call. The declaration is dead in
1337
+ // prod (its only write is behind true) and rollup drops it.
1338
+ let t = false;
1339
+ // The drain's opening instant, under the loop's own condition so it fires
1340
+ // exactly when `flushEnd` below will. Outside every try (see the rule in
1341
+ // attribution-hooks.ts).
1342
+ if (attrHooks !== null && (scheduled || activeTransition)) attrHooks.flushStart();
1343
+ // `flush()` is an explicit drain point, so it must also process an active
1344
+ // transition even if no microtask was scheduled for it yet.
1345
+ while (scheduled || activeTransition) {
1346
+ globalQueue.flush();
1347
+ t = true;
1348
+ }
1349
+ // Provenance ends with the drain: every ambient window (an action's first
1350
+ // slice, a landing's propagation) runs to this flush.
1351
+ origin = 0;
1352
+ // Outside every try in this function (see the rule in attribution-hooks.ts):
1353
+ // the drain loop above is the one place all scheduled work funnels through,
1354
+ // so this is the "committed and effects ran, or parked" instant for
1355
+ // everything the loop processed.
1356
+ if (t && attrHooks !== null) attrHooks.flushEnd();
1357
+ }
1358
+
1359
+ function runQueue(e, t) {
1360
+ for (let n = 0; n < e.length; n++) e[n](t);
1361
+ }
1362
+
1363
+ /** Does `reporter` still hold the transaction waiting on `source` — live,
1364
+ * routed to no collecting loading boundary, and deriving from the source in
1365
+ * its current pass? The verdict's per-reporter test (sourceObserved), also
1366
+ * a boundary re-arm's (boundaries.ts `_rearm` runs before the verdict prunes
1367
+ * registrations that stopped counting). */ function reporterBlocksSource(e, t, n) {
1368
+ const i = e.oe;
1369
+ if (i & REACTIVE_DISPOSED) return false;
1370
+ // A zombie renders until the commit that disposes it (#3463): while its
1371
+ // removal is staged in a live transaction it is still on screen, and what
1372
+ // it displays must stay consistent with the frame — a held `Show`'s
1373
+ // `Details: 0` beside the lane's `Value: 1` otherwise. Its say is moot for
1374
+ // the verdict of the transaction that stages the removal (`verdict`): done,
1375
+ // and the commit disposes it; not done, and it stays parked regardless. A
1376
+ // zombie whose removal commits this flush (owner's pass not held) is dead.
1377
+ // The owner is stamped when the flush parks; held in this flush, its
1378
+ // staging transaction is the active one.
1379
+ if (i & REACTIVE_ZOMBIE) {
1380
+ let t = e;
1381
+ while (t && t.oe & REACTIVE_ZOMBIE) t = t._parent;
1382
+ let i = t && (t.me || (t.C & CONFIG_HELD_CHILDREN ? activeTransition : null));
1383
+ // A LANE frame's member (#3662) is displayed until its owner's run
1384
+ // applies, and the lane's transaction is what applies it (its completion
1385
+ // runs the lane's queue): moot for that verdict, live for every other.
1386
+ if (!i && t && t.C & CONFIG_LANE_FRAME && t.o?.Ce) i = findLane(t.o.Ce).me;
1387
+ if (!i || (i = currentTransition(i)).Dt === true || i === n) return false;
1388
+ }
1389
+ // Fallback-caught async holds nothing. A collecting loading boundary
1390
+ // consumes the notification, so a reader under a fallback never registers —
1391
+ // but a reader registered while its boundary showed content stays
1392
+ // registered when the boundary's `on` later changes and it flips to the
1393
+ // fallback. The reader is behind the fallback now; if nothing outside the
1394
+ // boundary consumes the flight, the hold is over (A33, ruled 2026-09-12, #3375).
1395
+ for (let t = e.T; t; t = t._parent) if (t.te & STATUS_PENDING && !t.H) return false;
1396
+ // "Still derives from the source" is a question about THIS pass's reads:
1397
+ // the deps up to `_depsTail`. Past it lie the committed frame's — kept
1398
+ // linked by A30 until the commit trims them (a staged pass, an errored
1399
+ // one). Reading them here made a reporter whose pass had stopped reading
1400
+ // the source (a gate closed in the same flush as the write) look live, and
1401
+ // the hold it kept was the commit that would have trimmed the dep that
1402
+ // kept it (spec O3, same-flush form; fuzzer #3446 P1 cases 21/79). A
1403
+ // trimmed list ends at `_depsTail`, so the bound is free there; a pass
1404
+ // that read nothing has a null tail and derives from nothing.
1405
+ // The registration is trusted: a pending mark rides only this pass's links
1406
+ // (notifyStatus skips the kept tail), so a registered reporter read the
1407
+ // source, or threw on it.
1408
+ if (e.o?.ue?.has(t)) return true;
1409
+ const r = e.Je;
1410
+ for (let n = r === null ? null : e.Ee; n; n = n === r ? null : n.Ie) {
1411
+ let e = n.De;
1412
+ while (e) {
1413
+ // Or through a memo pending on the flight (#3494): a stale reader
1414
+ // served a held memo's committed value never turned pending itself, so
1415
+ // its only trace of the flight is the memo between them — `copy` of
1416
+ // `details`. Judged dead, its transaction released `count=1` beside
1417
+ // the `Copy: 0` it displays. `_pendingSources` is transitive, so one
1418
+ // hop covers any depth.
1419
+ if (e === t || e.Ne === t || e.o?.ue?.has(t)) return true;
1420
+ e = e.o?.$n;
1421
+ }
1422
+ }
1423
+ return !!(e.S & STATUS_PENDING && e.o?._ instanceof NotReadyError && e.o?._.source === t);
1424
+ }
1425
+
1426
+ /**
1427
+ * Does a live reporter of `transition` still observe `source` pending? Dead
1428
+ * reporters (disposed, behind a fallback, no longer reading the source) are
1429
+ * pruned as they are found, and the source's entry with them. Shared by the
1430
+ * settle verdict and the lane's hold check (`waitingTransition`): a live
1431
+ * action parks its transaction without a verdict, so this prune is the only
1432
+ * one an optimistic lane whose last async reader unmounted mid-action ever
1433
+ * gets — without it the lane held on the dead reporter's registration until
1434
+ * the flight it no longer observed landed (#3426).
1435
+ */ function sourceObserved(e, t, n) {
1436
+ const i = e.ae.get(t);
1437
+ let r = false;
1438
+ for (const e of i ?? []) {
1439
+ if (reporterBlocksSource(e, t, n)) return true;
1440
+ // A zombie the verdict passes over is kept, not pruned (#3463): moot for
1441
+ // this verdict, it still holds a lane's reveal while the transaction
1442
+ // stays parked on something else.
1443
+ if (n && e.oe & REACTIVE_ZOMBIE) r = true; else i.delete(e);
1444
+ }
1445
+ if (!r) e.ae.delete(t);
1446
+ return false;
1447
+ }
1448
+
1449
+ function transitionComplete(e) {
1450
+ if (e.Dt) return true;
1451
+ if (e.tt.length) {
1452
+ // A live action parks the transaction regardless of async state.
1453
+ if (attrHooks !== null) attrHooks.holdStart(e);
1454
+ return false;
1455
+ }
1456
+ let t = true;
1457
+ for (const n of e.ae.keys()) {
1458
+ // The source blocks while its OWN flight is up — the self entry in its
1459
+ // pending sources (added by notifyStatus's source path, with status;
1460
+ // retired by the landing and the supersede sweep, so it implies
1461
+ // STATUS_PENDING). `_error.source` is not that test: propagation from an
1462
+ // input that went pending later overwrites it with the input (#3375 — a
1463
+ // boundary-consumed load re-asked under a held derivation), and the
1464
+ // still-flying source read as settled, committing the writes it was
1465
+ // asked with ahead of its answer.
1466
+ // Not the self entry alone: a source whose own flight an upstream re-ask
1467
+ // superseded is still pending — on that re-ask (#3462). Its reader cannot
1468
+ // render until the chain lands, and the landing folds this transaction in
1469
+ // (enterWaiting). Judged complete instead, a re-entry between the two (a
1470
+ // repeated write to a held signal) committed the held writes beside the
1471
+ // reader's stale frame.
1472
+ if (sourceObserved(e, n, e) && n.o?.ue?.size) {
1473
+ t = false;
1474
+ break;
1475
+ }
1476
+ }
1477
+ // Override blockage lives with the engine (absent hook = "no optimistic
1478
+ // blockage"); the hook's loops over _optimisticNodes/_optimisticStores are
1479
+ // no-ops when the transition holds neither, so no pre-check is needed.
1480
+ if (t && GlobalQueue.Yn?.(e)) t = false;
1481
+ // Attribution hook: this verdict is the fork between settling (held writes
1482
+ // commit next — `_pendingNodes` still lists them) and parking (the flush
1483
+ // runs the lane effects, then stashes; `holdEnd` fires from stashQueues).
1484
+ // Fired here rather than at flush()'s call site because that site is inside
1485
+ // a `try` (see the rule in attribution-hooks.ts).
1486
+ if (attrHooks !== null) t ? attrHooks.transitionSettled(e) : attrHooks.holdStart(e);
1487
+ t && (e.Dt = true);
1488
+ return t;
1489
+ }
1490
+
1491
+ /** A fresh, unentered transaction (#3146): the optimistic store's truth
1492
+ * flight DECLARES an owned transaction instead of relying on whatever the
1493
+ * ambient adoption machinery stamped on its firewall. Activate it with
1494
+ * initTransition; it is a plain batch until then. */ function createTransition() {
1495
+ return createBatch();
1496
+ }
1497
+
1498
+ function currentTransition(e) {
1499
+ while (e.Dt && typeof e.Dt === "object") e = e.Dt;
1500
+ return e;
1501
+ }
1502
+
1503
+ /**
1504
+ * The live transition blocked on `source` — the one whose render reader
1505
+ * observed it pending (INV-3 records the observation in whichever transaction
1506
+ * was active when the reader was notified). The observation is a fact about
1507
+ * the node, so a hold check must not assume it was recorded in the transaction
1508
+ * it happens to hold — lanes merge across transactions (#2912), and a merged
1509
+ * root's transaction knows nothing of the async its members' transactions
1510
+ * observed (#3335). Null when nobody is waiting — a registration whose every
1511
+ * reporter has since died is nobody (#3426).
1512
+ */ function waitingTransition(e) {
1513
+ for (const t of transitions) if (sourceObserved(t, e)) return t;
1514
+ return null;
1515
+ }
1516
+
1517
+ /** A landing enters EVERY parked transaction still waiting on `source`, folding
1518
+ * them into the active one (A15: each reveal that discovered the flight
1519
+ * completes at its landing). The fold used to happen as the waiters' stamped
1520
+ * readers recomputed under the landing — recompute re-entering an effect's
1521
+ * stamp — which also folded in writes those readers merely shared a hole
1522
+ * with (#3407); effects no longer re-enter, so the landing folds explicitly.
1523
+ * Live iteration is safe: a merge deletes the outgoing (active) entry and
1524
+ * re-adds the visited one. */ function enterWaiting(e) {
1525
+ for (const t of transitions) if (sourceObserved(t, e)) globalQueue.initTransition(t);
1526
+ }
1527
+
1528
+ function runInTransition(e, t) {
1529
+ const n = activeTransition;
1530
+ try {
1531
+ activeTransition = currentTransition(e);
1532
+ return t();
1533
+ } finally {
1534
+ activeTransition = n;
1535
+ }
1536
+ }
1537
+
1538
+ /** Run `fn` with `transition` as BOTH the ambient transaction and the
1539
+ * registration batch, restoring both after. runInTransition alone is not
1540
+ * enough for code that WRITES on behalf of a transaction from inside someone
1541
+ * else's window (optimistic replay re-arming a still-open action's edits
1542
+ * during a landing commit, #3123): registrations route through the queue's
1543
+ * batch pointer, and a bare activeTransition swap leaves them in the ambient
1544
+ * batch — a plain batch "completes" at the next flush and reverts optimistic
1545
+ * registrations that were supposed to live with the transaction.
1546
+ * initTransition is the wrong tool here: it MERGES the currently ambient
1547
+ * transaction into the target, entangling whatever the interrupted window
1548
+ * belonged to. */ function runAsTransitionBatch(e, t) {
1549
+ const n = activeTransition;
1550
+ const i = globalQueue.m;
1551
+ try {
1552
+ activeTransition = currentTransition(e);
1553
+ currentBatch = globalQueue.m = activeTransition;
1554
+ return t();
1555
+ } finally {
1556
+ activeTransition = n;
1557
+ currentBatch = globalQueue.m = i;
1558
+ }
1559
+ }
1560
+
1561
+ export { GlobalQueue, Queue, ROOT_ERROR_HOOK, actionStepDepth, activeAffectsMarks, activeLanes, activeTransition, armReaskClear, assignOrMergeLane, batchJoins, bumpNotifyEpoch, clock, createTransition, currentTransition, deferSlotRelease, dirtyQueue, enforceLoadingBoundary, entangleConfirmingTransitions, enterActionStep, enterWaiting, exitActionStep, finalizePureQueue, findLane, flush, globalQueue, haltReactivity, hasActiveOverride, heldTrims, insertSubs, notifyEpoch, origin, pendingRearms, projectionWriteActive, queuePendingNode, queueRearm, reaskArmed, reporterBlocksSource, resetErrorHalt, runAsTransitionBatch, runInTransition, schedule, scheduleWithheld, setOrigin, setProjectionWriteActive, setStoreCommitHook, shiftAffectsMarks, sourceObserved, storeCommitHook, transitions, waitingTransition, wakeParked, wokenTransitions, zombieQueue };