@solidjs/signals 2.0.0-beta.9 → 2.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/README.md +14 -10
  2. package/dist/dev.js +6740 -2011
  3. package/dist/node.cjs +7927 -3340
  4. package/dist/prod/affects.js +126 -0
  5. package/dist/prod/boundaries.js +572 -0
  6. package/dist/prod/core/action.js +139 -0
  7. package/dist/prod/core/async.js +607 -0
  8. package/dist/prod/core/constants.js +92 -0
  9. package/dist/prod/core/context.js +67 -0
  10. package/dist/prod/core/core.js +828 -0
  11. package/dist/prod/core/dev.js +3 -0
  12. package/dist/prod/core/effect.js +145 -0
  13. package/dist/prod/core/error.js +68 -0
  14. package/dist/prod/core/external.js +98 -0
  15. package/dist/prod/core/graph.js +104 -0
  16. package/dist/prod/core/heap.js +140 -0
  17. package/dist/prod/core/invariants.js +42 -0
  18. package/dist/prod/core/lanes.js +140 -0
  19. package/dist/prod/core/optimistic.js +262 -0
  20. package/dist/prod/core/owner.js +302 -0
  21. package/dist/prod/core/scheduler.js +757 -0
  22. package/dist/prod/core/verdict.js +369 -0
  23. package/dist/prod/index.js +45 -0
  24. package/dist/prod/map.js +319 -0
  25. package/dist/prod/signals.js +394 -0
  26. package/dist/prod/store/index.js +36 -0
  27. package/dist/prod/store/next/optimistic.js +377 -0
  28. package/dist/prod/store/next/projection.js +172 -0
  29. package/dist/prod/store/next/reconcile.js +327 -0
  30. package/dist/prod/store/next/store.js +1232 -0
  31. package/dist/prod/store/next/target.js +20 -0
  32. package/dist/prod/store/store.js +325 -0
  33. package/dist/prod/store/storePath.js +103 -0
  34. package/dist/prod/store/utils.js +201 -0
  35. package/dist/types/affects.d.ts +47 -0
  36. package/dist/types/boundaries.d.ts +60 -13
  37. package/dist/types/core/action.d.ts +35 -6
  38. package/dist/types/core/async.d.ts +16 -1
  39. package/dist/types/core/constants.d.ts +35 -6
  40. package/dist/types/core/context.d.ts +10 -2
  41. package/dist/types/core/core.d.ts +66 -63
  42. package/dist/types/core/dev.d.ts +17 -2
  43. package/dist/types/core/effect.d.ts +1 -2
  44. package/dist/types/core/error.d.ts +33 -0
  45. package/dist/types/core/external.d.ts +0 -11
  46. package/dist/types/core/graph.d.ts +2 -1
  47. package/dist/types/core/heap.d.ts +8 -0
  48. package/dist/types/core/index.d.ts +3 -2
  49. package/dist/types/core/invariants.d.ts +59 -0
  50. package/dist/types/core/lanes.d.ts +2 -0
  51. package/dist/types/core/optimistic.d.ts +6 -0
  52. package/dist/types/core/owner.d.ts +50 -3
  53. package/dist/types/core/scheduler.d.ts +85 -11
  54. package/dist/types/core/types.d.ts +66 -1
  55. package/dist/types/core/verdict.d.ts +2 -0
  56. package/dist/types/index.d.ts +3 -2
  57. package/dist/types/map.d.ts +21 -5
  58. package/dist/types/signals.d.ts +183 -12
  59. package/dist/types/store/index.d.ts +16 -6
  60. package/dist/types/store/next/optimistic.d.ts +20 -0
  61. package/dist/types/store/next/projection.d.ts +24 -0
  62. package/dist/types/store/next/reconcile.d.ts +3 -0
  63. package/dist/types/store/next/store.d.ts +71 -0
  64. package/dist/types/store/next/target.d.ts +99 -0
  65. package/dist/types/store/optimistic.d.ts +3 -3
  66. package/dist/types/store/projection.d.ts +10 -6
  67. package/dist/types/store/reconcile.d.ts +31 -8
  68. package/dist/types/store/store.d.ts +91 -71
  69. package/dist/types/store/utils.d.ts +0 -40
  70. package/dist/types-cjs/affects.d.cts +47 -0
  71. package/dist/types-cjs/boundaries.d.cts +60 -13
  72. package/dist/types-cjs/core/action.d.cts +35 -6
  73. package/dist/types-cjs/core/async.d.cts +16 -1
  74. package/dist/types-cjs/core/constants.d.cts +35 -6
  75. package/dist/types-cjs/core/context.d.cts +10 -2
  76. package/dist/types-cjs/core/core.d.cts +66 -63
  77. package/dist/types-cjs/core/dev.d.cts +17 -2
  78. package/dist/types-cjs/core/effect.d.cts +1 -2
  79. package/dist/types-cjs/core/error.d.cts +33 -0
  80. package/dist/types-cjs/core/external.d.cts +0 -11
  81. package/dist/types-cjs/core/graph.d.cts +2 -1
  82. package/dist/types-cjs/core/heap.d.cts +8 -0
  83. package/dist/types-cjs/core/index.d.cts +3 -2
  84. package/dist/types-cjs/core/invariants.d.cts +59 -0
  85. package/dist/types-cjs/core/lanes.d.cts +2 -0
  86. package/dist/types-cjs/core/optimistic.d.cts +6 -0
  87. package/dist/types-cjs/core/owner.d.cts +50 -3
  88. package/dist/types-cjs/core/scheduler.d.cts +85 -11
  89. package/dist/types-cjs/core/types.d.cts +66 -1
  90. package/dist/types-cjs/core/verdict.d.cts +2 -0
  91. package/dist/types-cjs/index.d.cts +3 -2
  92. package/dist/types-cjs/map.d.cts +21 -5
  93. package/dist/types-cjs/signals.d.cts +183 -12
  94. package/dist/types-cjs/store/index.d.cts +16 -6
  95. package/dist/types-cjs/store/next/optimistic.d.cts +20 -0
  96. package/dist/types-cjs/store/next/projection.d.cts +24 -0
  97. package/dist/types-cjs/store/next/reconcile.d.cts +3 -0
  98. package/dist/types-cjs/store/next/store.d.cts +71 -0
  99. package/dist/types-cjs/store/next/target.d.cts +99 -0
  100. package/dist/types-cjs/store/optimistic.d.cts +3 -3
  101. package/dist/types-cjs/store/projection.d.cts +10 -6
  102. package/dist/types-cjs/store/reconcile.d.cts +31 -8
  103. package/dist/types-cjs/store/store.d.cts +91 -71
  104. package/dist/types-cjs/store/utils.d.cts +0 -40
  105. package/package.json +12 -9
  106. package/dist/prod.js +0 -3627
@@ -0,0 +1,757 @@
1
+ import { EFFECT_RENDER, EFFECT_USER, STATUS_PENDING, REACTIVE_IN_HEAP_HEIGHT, REACTIVE_DISPOSED, REACTIVE_ZOMBIE, NOT_PENDING, EFFECT_TRACKED, CONFIG_IN_SNAPSHOT_SCOPE, REACTIVE_SNAPSHOT_STALE, REACTIVE_OPTIMISTIC_DIRTY, REACTIVE_REASK, REACTIVE_MANUAL_WRITE, STATUS_UNINITIALIZED } from "./constants.js";
2
+
3
+ import { currentOptimisticLane } from "./core.js";
4
+
5
+ import { DEV } from "./dev.js";
6
+
7
+ import { NotReadyError } from "./error.js";
8
+
9
+ import { runHeap, deleteFromHeap, enqueueSub } from "./heap.js";
10
+
11
+ import { activeLanes, assignOrMergeLane, findLane } from "./lanes.js";
12
+
13
+ export { getOrCreateLane, hasActiveOverride, mergeLanes, resolveLane } from "./lanes.js";
14
+
15
+ import { devCheckFlushStart, devCheckActiveOverrides, devCensusCompanions, devCheckQuiescent } from "./invariants.js";
16
+
17
+ const transitions = new Set;
18
+
19
+ const dirtyQueue = {
20
+ eE: new Array(2e3).fill(undefined),
21
+ tE: false,
22
+ Ve: 0,
23
+ EE: 0
24
+ };
25
+
26
+ const zombieQueue = {
27
+ eE: new Array(2e3).fill(undefined),
28
+ tE: false,
29
+ Ve: 0,
30
+ EE: 0
31
+ };
32
+
33
+ /** runHeap callback that discards a queued zombie recompute instead of running
34
+ * it: unlink pure recompute entries; strip just the recompute bit from dirtied
35
+ * height-adjust entries so their height work still happens. */ function cancelZombieRecompute(e) {
36
+ if (e.se & REACTIVE_IN_HEAP_HEIGHT) e.se &= -12; else {
37
+ deleteFromHeap(e, zombieQueue);
38
+ e.se &= -4;
39
+ }
40
+ }
41
+
42
+ let clock = 0;
43
+
44
+ let activeTransition = null;
45
+
46
+ let scheduled = false;
47
+
48
+ let halted = false;
49
+
50
+ let haltNotified = false;
51
+
52
+ let syncDepth = 0;
53
+
54
+ let projectionWriteActive = false;
55
+
56
+ // Store property nodes that were created solely to carry a pending write (no
57
+ // subscribers at write time). Swept after each flush that commits pending
58
+ // values — any still without subs get disposed via their `_unobserved` hook,
59
+ // releasing the slot in the parent store's node map.
60
+ const transientStoreNodes = new Set;
61
+
62
+ function canUseSimpleSyncFlush(e) {
63
+ const t = e.m;
64
+ return transitions.size === 0 && activeLanes.size === 0 && e.vt.length === 0 && t.Me.length === 0 && t.A.length === 0 && t.dn.size === 0 && transientStoreNodes.size === 0;
65
+ }
66
+
67
+ function sweepTransientStoreNodes() {
68
+ if (transientStoreNodes.size === 0) return;
69
+ for (const e of transientStoreNodes) {
70
+ if (e.o !== null) {
71
+ transientStoreNodes.delete(e);
72
+ continue;
73
+ }
74
+ if (e._e !== NOT_PENDING) continue;
75
+ if (e.De !== undefined && e.De !== NOT_PENDING) continue;
76
+ // A live affects() mark keeps the node addressable: sweeping it would
77
+ // detach the refcount from the slot (a fresh probe would upsert a new,
78
+ // unmarked node for the same property).
79
+ if (e.t) continue;
80
+ transientStoreNodes.delete(e);
81
+ e.ut?.();
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Toggles the dev-mode "must be inside a `<Loading>` boundary" enforcement
87
+ * window. Only `render()` calls this — wrapping the initial mount so that a
88
+ * top-level uncaught async read surfaces the diagnostic. Not part of the
89
+ * user-facing API.
90
+ *
91
+ * @internal
92
+ */ function enforceLoadingBoundary(e) {}
93
+
94
+ function setProjectionWriteActive(e) {
95
+ projectionWriteActive = e;
96
+ }
97
+
98
+ /**
99
+ * Ambient work IS a transaction: the global queue always carries one
100
+ * current-transaction-shaped batch (`globalQueue._batch`). With no transition
101
+ * active, registrations (pending commits, optimistic nodes, affects marks,
102
+ * optimistic stores) land in a plain ambient batch that the plain flush
103
+ * finalizes; when a transition initializes it adopts the ambient batch's
104
+ * contents and `_batch` becomes the transition itself, so later registrations
105
+ * land there directly — no per-field aliasing.
106
+ */ function createBatch() {
107
+ return {
108
+ de: clock,
109
+ Qt: [],
110
+ Ie: new Map,
111
+ Me: [],
112
+ A: [],
113
+ dn: new Set,
114
+ ie: [],
115
+ yt: {
116
+ bt: [ [], [] ],
117
+ vt: []
118
+ },
119
+ fn: false,
120
+ ln: new Set
121
+ };
122
+ }
123
+
124
+ function mergeTransitionState(e, t) {
125
+ t.fn = e;
126
+ e.ie.push(...t.ie);
127
+ for (const i of activeLanes) if (i.Ne === t) i.Ne = e;
128
+ if (t.Me.length) {
129
+ // Move (don't copy): the global queue's batch may still be the outgoing
130
+ // transition, and the adoption pass in initTransition would re-push its
131
+ // contents into the target — duplicating every entry.
132
+ e.Me.push(...t.Me);
133
+ t.Me.length = 0;
134
+ }
135
+ if (t.A.length) {
136
+ // Move (don't copy): the global queue's batch may still be the outgoing
137
+ // transition, and the adoption pass in initTransition would re-push its
138
+ // contents into the target — double-releasing every mark.
139
+ e.A.push(...t.A);
140
+ t.A.length = 0;
141
+ }
142
+ for (const i of t.dn) e.dn.add(i);
143
+ for (const [i, n] of t.Ie) {
144
+ let t = e.Ie.get(i);
145
+ if (!t) e.Ie.set(i, t = new Set);
146
+ for (const e of n) t.add(e);
147
+ }
148
+ for (const i of t.ln) e.ln.add(i);
149
+ }
150
+
151
+ function schedule() {
152
+ if (halted) {
153
+ notifyHalted();
154
+ return;
155
+ }
156
+ if (scheduled) return;
157
+ scheduled = true;
158
+ if (!syncDepth && !globalQueue.cn && !projectionWriteActive) queueMicrotask(flush);
159
+ }
160
+
161
+ /**
162
+ * Permanently halts the reactive system. Called when a user error escapes
163
+ * every boundary — app state is undefined at that point, so scheduling stops
164
+ * entirely rather than limping along with a half-applied update.
165
+ */ function haltReactivity(e) {
166
+ if (halted) return;
167
+ halted = true;
168
+ let t = "[REACTIVITY_HALTED]";
169
+ // Log the cause here too: callers rethrow it, but a creation-time throw
170
+ // unwinds through ancestor recomputes that convert it to status instead of
171
+ // surfacing it (#2884), so the rethrow alone cannot guarantee visibility.
172
+ e === undefined ? console.error(t) : console.error(t, e);
173
+ }
174
+
175
+ // Logs on the first write after a halt so a frozen interaction is traceable.
176
+ function notifyHalted() {
177
+ if (haltNotified) return;
178
+ haltNotified = true;
179
+ console.error("[REACTIVITY_HALTED]");
180
+ }
181
+
182
+ /** @internal Test/dev-reload hook. Revives scheduling after a halt. */ function resetErrorHalt() {
183
+ halted = false;
184
+ haltNotified = false;
185
+ }
186
+
187
+ // Identifies one child-traversal pass in `Queue.run` so a rescan after the
188
+ // child list shifts can tell "already run this pass" from "still pending".
189
+ let queueRunToken = 0;
190
+
191
+ class Queue {
192
+ Fe=null;
193
+ bt=[ [], [] ];
194
+ vt=[];
195
+ kt=0;
196
+ created=clock;
197
+ addChild(e) {
198
+ this.vt.push(e);
199
+ e.Fe = this;
200
+ }
201
+ removeChild(e) {
202
+ const t = this.vt.indexOf(e);
203
+ if (t >= 0) {
204
+ this.vt.splice(t, 1);
205
+ e.Fe = null;
206
+ }
207
+ }
208
+ notify(e, t, i, n) {
209
+ if (this.Fe) return this.Fe.notify(e, t, i, n);
210
+ return false;
211
+ }
212
+ run(e) {
213
+ if (this.bt[e - 1].length) {
214
+ const t = this.bt[e - 1];
215
+ this.bt[e - 1] = [];
216
+ runQueue(t, e);
217
+ }
218
+ // Effects run here can dispose owners, and disposal removes queues from
219
+ // this list — the running child itself, an earlier sibling, or several at
220
+ // once. A plain index walk then skips whatever shifted into the cursor.
221
+ // Stamping each child before it runs makes the pass idempotent, so a shift
222
+ // can be recovered by rescanning from the front and every child still runs
223
+ // exactly once. Children appended mid-pass carry a stale stamp and run,
224
+ // matching the previous live-array behaviour.
225
+ const t = this.vt;
226
+ const i = ++queueRunToken;
227
+ for (let n = 0; n < t.length; ) {
228
+ const s = t[n];
229
+ if (s.kt !== i) {
230
+ s.kt = i;
231
+ s.run?.(e);
232
+ if (t[n] !== s) {
233
+ n = 0;
234
+ continue;
235
+ }
236
+ }
237
+ n++;
238
+ }
239
+ }
240
+ enqueue(e, t) {
241
+ if (e) {
242
+ // Route to lane's effect queue if we're in an optimistic recomputation
243
+ if (currentOptimisticLane) {
244
+ const i = findLane(currentOptimisticLane);
245
+ i.rn[e - 1].push(t);
246
+ } else {
247
+ this.bt[e - 1].push(t);
248
+ }
249
+ }
250
+ schedule();
251
+ }
252
+ stashQueues(e) {
253
+ e.bt[0].push(...this.bt[0]);
254
+ e.bt[1].push(...this.bt[1]);
255
+ this.bt = [ [], [] ];
256
+ for (let t = 0; t < this.vt.length; t++) {
257
+ let i = this.vt[t];
258
+ let n = e.vt[t];
259
+ if (!n) {
260
+ n = {
261
+ bt: [ [], [] ],
262
+ vt: []
263
+ };
264
+ e.vt[t] = n;
265
+ }
266
+ i.stashQueues(n);
267
+ }
268
+ }
269
+ restoreQueues(e) {
270
+ this.bt[0].push(...e.bt[0]);
271
+ this.bt[1].push(...e.bt[1]);
272
+ for (let t = 0; t < e.vt.length; t++) {
273
+ const i = e.vt[t];
274
+ let n = this.vt[t];
275
+ if (n) n.restoreQueues(i);
276
+ }
277
+ }
278
+ }
279
+
280
+ class GlobalQueue extends Queue {
281
+ cn=false;
282
+ // The current transaction-shaped batch: a plain ambient batch while no
283
+ // transition is active, the active transition itself after initTransition.
284
+ m=createBatch();
285
+ static Ce;
286
+ static me;
287
+ static et;
288
+ static gt=null;
289
+ // Store-side hook: drops a keyless affects() mark's identity scope when the
290
+ // carrier node's last registration releases (wired by store.ts, mirroring
291
+ // _clearOptimisticStore).
292
+ static p=null;
293
+ // affects()-side hooks (wired by affects.ts, mirroring _update): the mark
294
+ // engine — count/register/release — lives with the feature. Every call site
295
+ // is gated by state only that module creates, so `!` invocations are safe
296
+ // once the gate holds.
297
+ static G=null;
298
+ static M=null;
299
+ static h=null;
300
+ // External-source bridge (wired by enableExternalSource(); null while no
301
+ // config is active — including after _resetExternalSourceConfig()).
302
+ static dt=null;
303
+ static St=null;
304
+ // Verdict-layer hooks (wired by verdict.ts when isPending()/latest() are
305
+ // imported; null in apps that never use them). Call sites either guard for
306
+ // null or sit behind state only the verdict layer can create (`!` is safe
307
+ // there: `_pendingSignal`/`_latestValueComputed` are only ever assigned by
308
+ // verdict.ts, and `pendingCheckActive`/`latestReadActive` only flip inside
309
+ // isPending()/latest()).
310
+ static Pe=null;
311
+ static Se=null;
312
+ static Ge=null;
313
+ static un=null;
314
+ static At=null;
315
+ static Ct=null;
316
+ static Pt=null;
317
+ static $e=null;
318
+ static k=null;
319
+ static Lt=null;
320
+ // Optimistic-engine hooks (wired by core/optimistic.ts via
321
+ // installOptimisticEngine(), called from verdict.ts / createOptimistic /
322
+ // createOptimisticStore — every module that can create optimistic state).
323
+ // Call sites are gated by state only the engine can create: an
324
+ // `_overrideValue` slot, a lane in `activeLanes`, an `_optimisticNodes`
325
+ // entry, or a non-null `currentOptimisticLane`, so `!` invocations are safe
326
+ // once the gate holds.
327
+ static Gt=null;
328
+ static En=null;
329
+ static Tn=null;
330
+ static In=null;
331
+ static Nn=null;
332
+ static Rt=null;
333
+ static Ot=null;
334
+ static Dt=null;
335
+ static je=null;
336
+ static ze=null;
337
+ static Be=null;
338
+ static Qn=null;
339
+ flush() {
340
+ if (this.cn) return;
341
+ this.cn = true;
342
+ try {
343
+ if (false) ;
344
+ runHeap(dirtyQueue, GlobalQueue.Ce);
345
+ if (activeTransition) {
346
+ const e = transitionComplete(activeTransition);
347
+ if (!e) {
348
+ const e = activeTransition;
349
+ // When the parking batch IS the transition, all of its writes commit
350
+ // only with it — every zombie recompute they queued would run against
351
+ // a world the zombie never displays (zombies render mainline until
352
+ // commit), so cancel them instead of running them. Only an ambient
353
+ // batch's mainline writes (the #2916 shape below) legitimately reach
354
+ // zombies here. Height-adjust entries still process normally: a
355
+ // dirtied one keeps its height flag and falls through to runHeap's
356
+ // adjustHeight path on the next pass of the bucket.
357
+ runHeap(zombieQueue, this.m === e ? cancelZombieRecompute : GlobalQueue.Ce);
358
+ // Detach: the stashed transition keeps its batch; ambient work that
359
+ // follows lands in a fresh one. If the batch is already a separate
360
+ // ambient one — action done() restored activeTransition without
361
+ // adopting the batch, and an ordinary write landed there before
362
+ // the scheduled flush (#2916) — keep it: replacing it would strand
363
+ // its queued pending nodes with held _pendingValues forever.
364
+ if (this.m === e) currentBatch = this.m = createBatch();
365
+ // Run lane effects immediately (before stashing) - lanes with no pending async
366
+ if (activeLanes.size) {
367
+ GlobalQueue.Nn(EFFECT_RENDER);
368
+ GlobalQueue.Nn(EFFECT_USER);
369
+ }
370
+ this.stashQueues(e.yt);
371
+ clock++;
372
+ // A kept ambient batch may hold pending nodes (#2916): stay
373
+ // scheduled so the outer drain loop commits them via the plain
374
+ // flush path instead of leaving them until the next natural flush.
375
+ scheduled = dirtyQueue.EE >= dirtyQueue.Ve || this.m.Qt.length > 0;
376
+ reassignPendingTransition(e.Qt);
377
+ activeTransition = null;
378
+ finalizePureQueue(null, true);
379
+ return;
380
+ }
381
+ const t = activeTransition;
382
+ const i = this.m;
383
+ i !== t && i.Qt.push(...t.Qt);
384
+ this.restoreQueues(t.yt);
385
+ transitions.delete(t);
386
+ activeTransition = null;
387
+ reassignPendingTransition(i.Qt);
388
+ finalizePureQueue(t);
389
+ if (i === t) {
390
+ // Drop the dead Transition wrapper but keep its (drained) containers
391
+ // as the ambient batch — late registrations during finalization live
392
+ // there and must survive to the next flush.
393
+ const e = createBatch();
394
+ e.Qt = i.Qt;
395
+ e.Me = i.Me;
396
+ e.A = i.A;
397
+ e.dn = i.dn;
398
+ currentBatch = this.m = e;
399
+ }
400
+ } else {
401
+ if (canUseSimpleSyncFlush(this)) {
402
+ commitPendingNodes();
403
+ if (dirtyQueue.EE >= dirtyQueue.Ve) {
404
+ runHeap(dirtyQueue, GlobalQueue.Ce);
405
+ commitPendingNodes();
406
+ }
407
+ } else {
408
+ if (transitions.size) runHeap(zombieQueue, GlobalQueue.Ce);
409
+ finalizePureQueue();
410
+ }
411
+ }
412
+ clock++;
413
+ // Check if finalization added items to the heap (from optimistic reversion)
414
+ scheduled = dirtyQueue.EE >= dirtyQueue.Ve;
415
+ // Run lane effects first (for ready lanes), then regular effects
416
+ activeLanes.size && GlobalQueue.Nn(EFFECT_RENDER);
417
+ this.run(EFFECT_RENDER);
418
+ activeLanes.size && GlobalQueue.Nn(EFFECT_USER);
419
+ this.run(EFFECT_USER);
420
+ if (false) ;
421
+ if (false && !scheduled && !activeTransition && transitions.size === 0 && activeLanes.size === 0) ;
422
+ if (false) ;
423
+ } finally {
424
+ this.cn = false;
425
+ }
426
+ }
427
+ notify(e, t, i, n) {
428
+ // Only track async if the boundary is propagating STATUS_PENDING (not caught by boundary)
429
+ if (t & STATUS_PENDING) {
430
+ if (i & STATUS_PENDING) {
431
+ const t = n !== undefined ? n : e._;
432
+ // A visibility-only mark notification (the affects() boundary
433
+ // channel) updates display state on its way up but must be invisible
434
+ // to completion accounting BY CONSTRUCTION: it never registers a
435
+ // reporter and never counts toward the loading-boundary diagnostic.
436
+ if (t?.l) return true;
437
+ if (activeTransition && t) {
438
+ const i = t.source;
439
+ let n = activeTransition.Ie.get(i);
440
+ if (!n) activeTransition.Ie.set(i, n = new Set);
441
+ const s = n.size;
442
+ n.add(e);
443
+ if (n.size !== s) schedule();
444
+ }
445
+ }
446
+ return true;
447
+ }
448
+ return false;
449
+ }
450
+ initTransition(e) {
451
+ if (e) e = currentTransition(e);
452
+ if (e && e === activeTransition) return;
453
+ if (!e && activeTransition && activeTransition.de === clock) return;
454
+ if (!activeTransition) {
455
+ activeTransition = e ?? createBatch();
456
+ } else if (e) {
457
+ const t = activeTransition;
458
+ mergeTransitionState(e, t);
459
+ transitions.delete(t);
460
+ activeTransition = e;
461
+ }
462
+ transitions.add(activeTransition);
463
+ activeTransition.de = clock;
464
+ const t = this.m;
465
+ if (t !== activeTransition) {
466
+ // Adopt the ambient batch into the transaction, then make the
467
+ // transaction the batch so later registrations land there directly.
468
+ // Pending and optimistic nodes are re-stamped as the transaction's;
469
+ // marks don't hijack the node's _transition — a mark on a plain signal
470
+ // must not entangle unrelated writes to it; the same rule holds one hop
471
+ // downstream: propagation never queues pended subscribers as pending
472
+ // nodes, see propagateAffectsMark, #2893.
473
+ for (let e = 0; e < t.Qt.length; e++) {
474
+ const i = t.Qt[e];
475
+ i.Ne = activeTransition;
476
+ activeTransition.Qt.push(i);
477
+ }
478
+ for (let e = 0; e < t.Me.length; e++) {
479
+ const i = t.Me[e];
480
+ i.Ne = activeTransition;
481
+ activeTransition.Me.push(i);
482
+ }
483
+ if (t.A.length) activeTransition.A.push(...t.A);
484
+ for (const e of t.dn) activeTransition.dn.add(e);
485
+ // Gated readers recorded against the ambient batch move with it: their
486
+ // replay-at-commit now happens at the transaction's completion.
487
+ if (t.ln.size) {
488
+ for (const e of t.ln) activeTransition.ln.add(e);
489
+ t.ln.clear();
490
+ }
491
+ currentBatch = this.m = activeTransition;
492
+ }
493
+ for (const e of activeLanes) {
494
+ if (!e.Ne) e.Ne = activeTransition;
495
+ }
496
+ }
497
+ }
498
+
499
+ function queuePendingNode(e) {
500
+ currentBatch.Qt.push(e);
501
+ }
502
+
503
+ // Sticky: flips true on the first refresh() ever (the only setter of
504
+ // REACTIVE_REASK) so the hot notification loop skips the per-subscriber flag
505
+ // clear entirely in apps that never refresh.
506
+ let reaskArmed = false;
507
+
508
+ function armReaskClear() {
509
+ reaskArmed = true;
510
+ }
511
+
512
+ function insertSubs(e, t = false) {
513
+ // Get source lane: prefer node's own lane over current context
514
+ // This is important for isPending signals which need their own lane to flush immediately
515
+ const i = e.Ke || currentOptimisticLane;
516
+ const n = e.xe !== undefined;
517
+ const s = reaskArmed;
518
+ for (let r = e.o; r !== null; r = r.ue) {
519
+ // A value-change notification is a new question for the subscriber: any
520
+ // pending re-ask mark (refresh) it carried is superseded.
521
+ if (s) r.fe.se &= ~REACTIVE_REASK;
522
+ if (n && r.fe.T & CONFIG_IN_SNAPSHOT_SCOPE) {
523
+ r.fe.se |= REACTIVE_SNAPSHOT_STALE;
524
+ continue;
525
+ }
526
+ if (t && i) {
527
+ r.fe.se |= REACTIVE_OPTIMISTIC_DIRTY;
528
+ assignOrMergeLane(r.fe, i);
529
+ } else if (t) {
530
+ r.fe.se |= REACTIVE_OPTIMISTIC_DIRTY;
531
+ // No source lane means reversion - clear subscriber's lane so effects go to regular queue
532
+ r.fe.Ke = undefined;
533
+ }
534
+ enqueueSub(r.fe);
535
+ }
536
+ }
537
+
538
+ function commitPendingNode(e) {
539
+ const t = e;
540
+ if (!t.ce) {
541
+ if (e._e !== NOT_PENDING) {
542
+ e.be = e._e;
543
+ e._e = NOT_PENDING;
544
+ }
545
+ if (e.Oe || e.ge) GlobalQueue.un(e);
546
+ return;
547
+ }
548
+ if (e._e !== NOT_PENDING) {
549
+ e.be = e._e;
550
+ e._e = NOT_PENDING;
551
+ // Set _modified for effects, but not for tracked effects (they handle their own scheduling)
552
+ if (e.Re && e.Re !== EFFECT_TRACKED) e.Je = true;
553
+ }
554
+ // The committed hold is the first observable answer for a loading-window
555
+ // node — the window closes here, not at compute time (#2990). Unconditional
556
+ // store to an always-present computed slot.
557
+ t.Ee = false;
558
+ t.se &= ~REACTIVE_MANUAL_WRITE;
559
+ if (!(t.S & STATUS_PENDING)) t.S &= ~STATUS_UNINITIALIZED;
560
+ if (t.We !== null || t.Qe !== null) GlobalQueue.me(t, false, true);
561
+ if (e.Oe || e.ge) GlobalQueue.un(e);
562
+ }
563
+
564
+ // Store commit hook (INTERNALS-STORE-STATE.md §3): installed by the store
565
+ // module at init (same treeshakeable pattern as _resolveOptimistic /
566
+ // _clearOptimisticStores). Folds committed store-node values into their
567
+ // backing objects at the same moment pending values commit — the single
568
+ // mutation point of the owned-raw model.
569
+ let storeCommitHook = null;
570
+
571
+ function setStoreCommitHook(e) {
572
+ storeCommitHook = e;
573
+ }
574
+
575
+ function commitPendingNodes() {
576
+ const e = currentBatch.Qt;
577
+ for (let t = 0; t < e.length; t++) {
578
+ commitPendingNode(e[t]);
579
+ }
580
+ e.length = 0;
581
+ storeCommitHook?.();
582
+ }
583
+
584
+ function finalizePureQueue(e = null, t = false) {
585
+ // For incomplete transitions, skip pending resolution and optimistic reversion
586
+ // For completing transitions or no-transition, resolve pending and revert optimistic
587
+ const i = !t;
588
+ if (i) commitPendingNodes();
589
+ if (!t && globalQueue.vt.length) checkBoundaryChildren(globalQueue);
590
+ const n = dirtyQueue.EE >= dirtyQueue.Ve;
591
+ if (n) runHeap(dirtyQueue, GlobalQueue.Ce);
592
+ if (i) {
593
+ if (n) commitPendingNodes();
594
+ // The settling batch: the completing transaction's, or the ambient one.
595
+ const t = e ?? globalQueue.m;
596
+ // Optimistic reversion: a non-empty batch means _optimisticWrite ran,
597
+ // which installed the engine's hooks.
598
+ if (t.Me.length) GlobalQueue.En(t.Me);
599
+ // Replay entanglement: subs recorded by the read-time gate get rescheduled
600
+ // so they re-run with the now-committed values visible. The ambient batch
601
+ // replays too — laneReadsCommitted records readers whose committed-view
602
+ // read hid a same-tick plain write that just committed above (#2963).
603
+ if (t.ln.size) {
604
+ for (const e of t.ln) {
605
+ if (e.se & REACTIVE_DISPOSED) continue;
606
+ enqueueSub(e);
607
+ }
608
+ t.ln.clear();
609
+ // A completing transition keeps the outer flush loop alive by itself;
610
+ // the ambient batch needs the re-arm or the replay sits in the heap
611
+ // until the next unrelated write.
612
+ schedule();
613
+ }
614
+ // Declared motion ends with the transaction: settle (or plain flush end
615
+ // for ambient marks) releases each registration's refcount. A non-empty
616
+ // batch means registerAffectsMark ran, which installed the hook. Marks
617
+ // held boundary display state through the visual channel, and their
618
+ // release is the display-state update point — re-run the boundary sweep
619
+ // (the earlier sweep above ran while the marks were still live).
620
+ if (t.A.length) {
621
+ GlobalQueue.G(t.A);
622
+ if (globalQueue.vt.length) checkBoundaryChildren(globalQueue);
623
+ }
624
+ // A non-empty set means trackOptimisticStore ran, which installed the
625
+ // hook; the hook iterates, clears, and schedules (keeping the loop out of
626
+ // core lets esbuild shake it — rollup already folds the null guard). The
627
+ // completing transition scopes the clear to its own layer keys (#2899).
628
+ if (t.dn.size) GlobalQueue.gt(t.dn, e);
629
+ sweepTransientStoreNodes();
630
+ // Lanes only enter activeLanes through the engine's getOrCreateLane.
631
+ if (activeLanes.size) GlobalQueue.In(e);
632
+ }
633
+ }
634
+
635
+ function checkBoundaryChildren(e) {
636
+ for (const t of e.vt) {
637
+ t.ne?.();
638
+ checkBoundaryChildren(t);
639
+ }
640
+ }
641
+
642
+ /**
643
+ * Count of live `affects()` registrations across the system (including
644
+ * store-scope inherited marks). Gates the read-path mark check in `read()` so
645
+ * graphs that never use the feature pay one integer compare.
646
+ */ let activeAffectsMarks = 0;
647
+
648
+ /**
649
+ * Counter mutation seam for the mark engine in affects.ts: an imported `let`
650
+ * binding is read-only, and the read-path gate above must stay a plain module
651
+ * variable so `read()` pays one integer compare, not a function call.
652
+ *
653
+ * @internal
654
+ */ function shiftAffectsMarks(e) {
655
+ activeAffectsMarks += e;
656
+ }
657
+
658
+ function reassignPendingTransition(e) {
659
+ for (let t = 0; t < e.length; t++) {
660
+ e[t].Ne = activeTransition;
661
+ }
662
+ }
663
+
664
+ const globalQueue = new GlobalQueue;
665
+
666
+ // Hot-path mirror of `globalQueue._batch`: `queuePendingNode` runs once per
667
+ // staged write and `commitPendingNodes` once per flush, and the extra
668
+ // property hop through `_batch` was a measured instruction-count regression
669
+ // (CodSpeed update1to1, PR #2905). The field stays authoritative for
670
+ // cross-module readers; every `_batch` assignment updates both.
671
+ let currentBatch = globalQueue.m;
672
+
673
+ function flush(e) {
674
+ if (e) {
675
+ syncDepth++;
676
+ try {
677
+ return e();
678
+ } finally {
679
+ // Decrement even if the drain throws (a throwing effect): a leaked
680
+ // syncDepth would stop `schedule()` from ever queuing a microtask again.
681
+ try {
682
+ flush();
683
+ } finally {
684
+ syncDepth--;
685
+ }
686
+ }
687
+ }
688
+ if (globalQueue.cn) {
689
+ return;
690
+ }
691
+ if (halted) return;
692
+ // `flush()` is an explicit drain point, so it must also process an active
693
+ // transition even if no microtask was scheduled for it yet.
694
+ while (scheduled || activeTransition) {
695
+ globalQueue.flush();
696
+ }
697
+ }
698
+
699
+ function runQueue(e, t) {
700
+ for (let i = 0; i < e.length; i++) e[i](t);
701
+ }
702
+
703
+ function reporterBlocksSource(e, t) {
704
+ if (e.se & (REACTIVE_ZOMBIE | REACTIVE_DISPOSED)) return false;
705
+ if (e.oe?.has(t)) return true;
706
+ for (let i = e.tt; i; i = i.nt) {
707
+ let e = i.it;
708
+ while (e) {
709
+ if (e === t || e.lt === t) return true;
710
+ e = e.nn;
711
+ }
712
+ }
713
+ return !!(e.S & STATUS_PENDING && e._ instanceof NotReadyError && e._.source === t);
714
+ }
715
+
716
+ function transitionComplete(e) {
717
+ if (e.fn) return true;
718
+ if (e.ie.length) return false;
719
+ let t = true;
720
+ for (const [i, n] of e.Ie) {
721
+ let s = false;
722
+ for (const e of n) {
723
+ if (reporterBlocksSource(e, i)) {
724
+ s = true;
725
+ break;
726
+ }
727
+ n.delete(e);
728
+ }
729
+ if (!s) e.Ie.delete(i); else if (i.S & STATUS_PENDING && i._?.source === i) {
730
+ t = false;
731
+ break;
732
+ }
733
+ }
734
+ // Override blockage lives with the engine (absent hook = "no optimistic
735
+ // blockage"); the hook's loops over _optimisticNodes/_optimisticStores are
736
+ // no-ops when the transition holds neither, so no pre-check is needed.
737
+ if (t && GlobalQueue.Tn?.(e)) t = false;
738
+ t && (e.fn = true);
739
+ return t;
740
+ }
741
+
742
+ function currentTransition(e) {
743
+ while (e.fn && typeof e.fn === "object") e = e.fn;
744
+ return e;
745
+ }
746
+
747
+ function runInTransition(e, t) {
748
+ const i = activeTransition;
749
+ try {
750
+ activeTransition = currentTransition(e);
751
+ return t();
752
+ } finally {
753
+ activeTransition = i;
754
+ }
755
+ }
756
+
757
+ export { GlobalQueue, Queue, activeAffectsMarks, activeLanes, activeTransition, armReaskClear, assignOrMergeLane, clock, currentTransition, dirtyQueue, enforceLoadingBoundary, finalizePureQueue, findLane, flush, globalQueue, haltReactivity, insertSubs, projectionWriteActive, queuePendingNode, resetErrorHalt, runInTransition, schedule, setProjectionWriteActive, setStoreCommitHook, shiftAffectsMarks, storeCommitHook, zombieQueue };