@solidjs/signals 2.0.0-beta.18 → 2.0.0-beta.19

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 (56) hide show
  1. package/dist/dev.js +2743 -694
  2. package/dist/node.cjs +6419 -4234
  3. package/dist/prod/affects.js +222 -0
  4. package/dist/prod/boundaries.js +568 -0
  5. package/dist/prod/core/action.js +83 -0
  6. package/dist/prod/core/async.js +394 -0
  7. package/dist/prod/core/constants.js +78 -0
  8. package/dist/prod/core/context.js +67 -0
  9. package/dist/prod/core/core.js +772 -0
  10. package/dist/prod/core/dev.js +3 -0
  11. package/dist/prod/core/effect.js +145 -0
  12. package/dist/prod/core/error.js +59 -0
  13. package/dist/prod/core/external.js +98 -0
  14. package/dist/prod/core/graph.js +91 -0
  15. package/dist/prod/core/heap.js +132 -0
  16. package/dist/prod/core/invariants.js +42 -0
  17. package/dist/prod/core/lanes.js +130 -0
  18. package/dist/prod/core/optimistic.js +265 -0
  19. package/dist/prod/core/owner.js +293 -0
  20. package/dist/prod/core/scheduler.js +689 -0
  21. package/dist/prod/core/verdict.js +293 -0
  22. package/dist/prod/index.js +45 -0
  23. package/dist/prod/map.js +264 -0
  24. package/dist/prod/signals.js +389 -0
  25. package/dist/prod/store/optimistic.js +149 -0
  26. package/dist/prod/store/projection.js +184 -0
  27. package/dist/prod/store/reconcile.js +392 -0
  28. package/dist/prod/store/store.js +739 -0
  29. package/dist/prod/store/storePath.js +103 -0
  30. package/dist/prod/store/utils.js +297 -0
  31. package/dist/types/affects.d.ts +1 -1
  32. package/dist/types/boundaries.d.ts +6 -6
  33. package/dist/types/core/async.d.ts +4 -38
  34. package/dist/types/core/core.d.ts +12 -78
  35. package/dist/types/core/external.d.ts +0 -30
  36. package/dist/types/core/heap.d.ts +8 -0
  37. package/dist/types/core/index.d.ts +3 -2
  38. package/dist/types/core/optimistic.d.ts +6 -0
  39. package/dist/types/core/owner.d.ts +8 -0
  40. package/dist/types/core/scheduler.d.ts +39 -34
  41. package/dist/types/core/verdict.d.ts +2 -0
  42. package/dist/types/store/projection.d.ts +0 -1
  43. package/dist/types-cjs/affects.d.cts +1 -1
  44. package/dist/types-cjs/boundaries.d.cts +6 -6
  45. package/dist/types-cjs/core/async.d.cts +4 -38
  46. package/dist/types-cjs/core/core.d.cts +12 -78
  47. package/dist/types-cjs/core/external.d.cts +0 -30
  48. package/dist/types-cjs/core/heap.d.cts +8 -0
  49. package/dist/types-cjs/core/index.d.cts +3 -2
  50. package/dist/types-cjs/core/optimistic.d.cts +6 -0
  51. package/dist/types-cjs/core/owner.d.cts +8 -0
  52. package/dist/types-cjs/core/scheduler.d.cts +39 -34
  53. package/dist/types-cjs/core/verdict.d.cts +2 -0
  54. package/dist/types-cjs/store/projection.d.cts +0 -1
  55. package/package.json +8 -6
  56. package/dist/prod.js +0 -4637
@@ -0,0 +1,689 @@
1
+ import { EFFECT_RENDER, EFFECT_USER, STATUS_PENDING, REACTIVE_DISPOSED, REACTIVE_REASK, CONFIG_IN_SNAPSHOT_SCOPE, REACTIVE_SNAPSHOT_STALE, REACTIVE_OPTIMISTIC_DIRTY, REACTIVE_ZOMBIE, NOT_PENDING, EFFECT_TRACKED, 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, 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
+ Le: 0,
23
+ EE: 0
24
+ };
25
+
26
+ const zombieQueue = {
27
+ eE: new Array(2e3).fill(undefined),
28
+ tE: false,
29
+ Le: 0,
30
+ EE: 0
31
+ };
32
+
33
+ let clock = 0;
34
+
35
+ let activeTransition = null;
36
+
37
+ let scheduled = false;
38
+
39
+ let halted = false;
40
+
41
+ let haltNotified = false;
42
+
43
+ let syncDepth = 0;
44
+
45
+ let projectionWriteActive = false;
46
+
47
+ // Store property nodes that were created solely to carry a pending write (no
48
+ // subscribers at write time). Swept after each flush that commits pending
49
+ // values — any still without subs get disposed via their `_unobserved` hook,
50
+ // releasing the slot in the parent store's node map.
51
+ const transientStoreNodes = new Set;
52
+
53
+ function registerTransientStoreNode(e) {
54
+ transientStoreNodes.add(e);
55
+ }
56
+
57
+ function canUseSimpleSyncFlush(e) {
58
+ return transitions.size === 0 && activeLanes.size === 0 && e.wt.length === 0 && e.Be.length === 0 && e.N.length === 0 && e.Lt.size === 0 && transientStoreNodes.size === 0;
59
+ }
60
+
61
+ function sweepTransientStoreNodes() {
62
+ if (transientStoreNodes.size === 0) return;
63
+ for (const e of transientStoreNodes) {
64
+ if (e.p !== null) {
65
+ transientStoreNodes.delete(e);
66
+ continue;
67
+ }
68
+ if (e.ge !== NOT_PENDING) continue;
69
+ if (e.be !== undefined && e.be !== NOT_PENDING) continue;
70
+ // A live affects() mark keeps the node addressable: sweeping it would
71
+ // detach the refcount from the slot (a fresh probe would upsert a new,
72
+ // unmarked node for the same property).
73
+ if (e.P) continue;
74
+ transientStoreNodes.delete(e);
75
+ e.ct?.();
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Toggles the dev-mode "must be inside a `<Loading>` boundary" enforcement
81
+ * window. Only `render()` calls this — wrapping the initial mount so that a
82
+ * top-level uncaught async read surfaces the diagnostic. Not part of the
83
+ * user-facing API.
84
+ *
85
+ * @internal
86
+ */ function enforceLoadingBoundary(e) {}
87
+
88
+ function setProjectionWriteActive(e) {
89
+ projectionWriteActive = e;
90
+ }
91
+
92
+ function mergeTransitionState(e, t) {
93
+ t.Ht = e;
94
+ e.Te.push(...t.Te);
95
+ for (const i of activeLanes) if (i.ve === t) i.ve = e;
96
+ if (t.Be.length) {
97
+ // Move (don't copy): the global queue may still alias the outgoing
98
+ // array, and the adoption pass in initTransition would re-push its
99
+ // contents into the target — duplicating every entry.
100
+ e.Be.push(...t.Be);
101
+ t.Be.length = 0;
102
+ }
103
+ if (t.N.length) {
104
+ // Move (don't copy): the global queue may still alias the outgoing
105
+ // array, and the adoption pass in initTransition would re-push its
106
+ // contents into the target — double-releasing every mark.
107
+ e.N.push(...t.N);
108
+ t.N.length = 0;
109
+ }
110
+ for (const i of t.Lt) e.Lt.add(i);
111
+ for (const [i, n] of t.qt) {
112
+ let t = e.qt.get(i);
113
+ if (!t) e.qt.set(i, t = new Set);
114
+ for (const e of n) t.add(e);
115
+ }
116
+ for (const i of t.Qt) e.Qt.add(i);
117
+ }
118
+
119
+ function schedule() {
120
+ if (halted) {
121
+ notifyHalted();
122
+ return;
123
+ }
124
+ if (scheduled) return;
125
+ scheduled = true;
126
+ if (!syncDepth && !globalQueue.Bt && !projectionWriteActive) queueMicrotask(flush);
127
+ }
128
+
129
+ /**
130
+ * Permanently halts the reactive system. Called when a user error escapes
131
+ * every boundary — app state is undefined at that point, so scheduling stops
132
+ * entirely rather than limping along with a half-applied update.
133
+ */ function haltReactivity(e) {
134
+ if (halted) return;
135
+ halted = true;
136
+ let t = "[REACTIVITY_HALTED]";
137
+ // Log the cause here too: callers rethrow it, but a creation-time throw
138
+ // unwinds through ancestor recomputes that convert it to status instead of
139
+ // surfacing it (#2884), so the rethrow alone cannot guarantee visibility.
140
+ e === undefined ? console.error(t) : console.error(t, e);
141
+ }
142
+
143
+ // Logs on the first write after a halt so a frozen interaction is traceable.
144
+ function notifyHalted() {
145
+ if (haltNotified) return;
146
+ haltNotified = true;
147
+ console.error("[REACTIVITY_HALTED]");
148
+ }
149
+
150
+ /** @internal Test/dev-reload hook. Revives scheduling after a halt. */ function resetErrorHalt() {
151
+ halted = false;
152
+ haltNotified = false;
153
+ }
154
+
155
+ class Queue {
156
+ He=null;
157
+ zt=[ [], [] ];
158
+ wt=[];
159
+ created=clock;
160
+ addChild(e) {
161
+ this.wt.push(e);
162
+ e.He = this;
163
+ }
164
+ removeChild(e) {
165
+ const t = this.wt.indexOf(e);
166
+ if (t >= 0) {
167
+ this.wt.splice(t, 1);
168
+ e.He = null;
169
+ }
170
+ }
171
+ notify(e, t, i, n) {
172
+ if (this.He) return this.He.notify(e, t, i, n);
173
+ return false;
174
+ }
175
+ run(e) {
176
+ if (this.zt[e - 1].length) {
177
+ const t = this.zt[e - 1];
178
+ this.zt[e - 1] = [];
179
+ runQueue(t, e);
180
+ }
181
+ for (let t = 0; t < this.wt.length; t++) this.wt[t].run?.(e);
182
+ }
183
+ enqueue(e, t) {
184
+ if (e) {
185
+ // Route to lane's effect queue if we're in an optimistic recomputation
186
+ if (currentOptimisticLane) {
187
+ const i = findLane(currentOptimisticLane);
188
+ i.tn[e - 1].push(t);
189
+ } else {
190
+ this.zt[e - 1].push(t);
191
+ }
192
+ }
193
+ schedule();
194
+ }
195
+ stashQueues(e) {
196
+ e.zt[0].push(...this.zt[0]);
197
+ e.zt[1].push(...this.zt[1]);
198
+ this.zt = [ [], [] ];
199
+ for (let t = 0; t < this.wt.length; t++) {
200
+ let i = this.wt[t];
201
+ let n = e.wt[t];
202
+ if (!n) {
203
+ n = {
204
+ zt: [ [], [] ],
205
+ wt: []
206
+ };
207
+ e.wt[t] = n;
208
+ }
209
+ i.stashQueues(n);
210
+ }
211
+ }
212
+ restoreQueues(e) {
213
+ this.zt[0].push(...e.zt[0]);
214
+ this.zt[1].push(...e.zt[1]);
215
+ for (let t = 0; t < e.wt.length; t++) {
216
+ const i = e.wt[t];
217
+ let n = this.wt[t];
218
+ if (n) n.restoreQueues(i);
219
+ }
220
+ }
221
+ }
222
+
223
+ class GlobalQueue extends Queue {
224
+ Bt=false;
225
+ xt=null;
226
+ Yt=[];
227
+ Be=[];
228
+ N=[];
229
+ Lt=new Set;
230
+ static Ce;
231
+ static Oe;
232
+ static ut;
233
+ static Kt=null;
234
+ // Store-side hook: drops a keyless affects() mark's identity scope when the
235
+ // carrier node's last registration releases (wired by store.ts, mirroring
236
+ // _clearOptimisticStore).
237
+ static T=null;
238
+ // affects()-side hooks (wired by affects.ts, mirroring _update): the mark
239
+ // engine — count/register/release plus the post-commit re-application of
240
+ // marked reads — lives with the feature. Every call site is gated by state
241
+ // only that module creates, so `!` invocations are safe once the gate holds.
242
+ static j=null;
243
+ static h=null;
244
+ static D=null;
245
+ static F=null;
246
+ static $=null;
247
+ static I=null;
248
+ // External-source bridge (wired by enableExternalSource(); null while no
249
+ // config is active — including after _resetExternalSourceConfig()).
250
+ static Ot=null;
251
+ static Rt=null;
252
+ // Verdict-layer hooks (wired by verdict.ts when isPending()/latest() are
253
+ // imported; null in apps that never use them). Call sites either guard for
254
+ // null or sit behind state only the verdict layer can create (`!` is safe
255
+ // there: `_pendingSignal`/`_latestValueComputed` are only ever assigned by
256
+ // verdict.ts, and `pendingCheckActive`/`latestReadActive` only flip inside
257
+ // isPending()/latest()).
258
+ static _e=null;
259
+ static _=null;
260
+ static me=null;
261
+ static R=null;
262
+ static Gt=null;
263
+ static Pt=null;
264
+ static vt=null;
265
+ static tt=null;
266
+ static nt=null;
267
+ static Zt=null;
268
+ // Optimistic-engine hooks (wired by core/optimistic.ts via
269
+ // installOptimisticEngine(), called from verdict.ts / createOptimistic /
270
+ // createOptimisticStore — every module that can create optimistic state).
271
+ // Call sites are gated by state only the engine can create: an
272
+ // `_overrideValue` slot, a lane in `activeLanes`, an `_optimisticNodes`
273
+ // entry, or a non-null `currentOptimisticLane`, so `!` invocations are safe
274
+ // once the gate holds.
275
+ static bt=null;
276
+ static Ut=null;
277
+ static yt=null;
278
+ static Wt=null;
279
+ static jt=null;
280
+ static Mt=null;
281
+ static gt=null;
282
+ static Dt=null;
283
+ static ht=null;
284
+ static kt=null;
285
+ static $e=null;
286
+ static et=null;
287
+ static Xe=null;
288
+ static Vt=null;
289
+ flush() {
290
+ if (this.Bt) return;
291
+ this.Bt = true;
292
+ try {
293
+ if (false) ;
294
+ runHeap(dirtyQueue, GlobalQueue.Ce);
295
+ if (activeTransition) {
296
+ const e = transitionComplete(activeTransition);
297
+ if (!e) {
298
+ const e = activeTransition;
299
+ runHeap(zombieQueue, GlobalQueue.Ce);
300
+ this.xt = null;
301
+ this.Yt = [];
302
+ this.Be = [];
303
+ this.N = [];
304
+ this.Lt = new Set;
305
+ // Run lane effects immediately (before stashing) - lanes with no pending async
306
+ if (activeLanes.size) {
307
+ GlobalQueue.Mt(EFFECT_RENDER);
308
+ GlobalQueue.Mt(EFFECT_USER);
309
+ }
310
+ this.stashQueues(e.Jt);
311
+ clock++;
312
+ scheduled = dirtyQueue.EE >= dirtyQueue.Le;
313
+ reassignPendingTransition(e.Yt);
314
+ activeTransition = null;
315
+ // The stash pass (committed-view rerun of plain optimistic signals)
316
+ // wraps finalizePureQueue in the engine; a non-empty _optimisticNodes
317
+ // means _optimisticWrite ran, which installed the hook.
318
+ if (!e.Te.length && !e.qt.size && e.Be.length) {
319
+ GlobalQueue.yt(e);
320
+ } else {
321
+ finalizePureQueue(null, true);
322
+ }
323
+ return;
324
+ }
325
+ this.Yt !== activeTransition.Yt && this.Yt.push(...activeTransition.Yt);
326
+ this.restoreQueues(activeTransition.Jt);
327
+ transitions.delete(activeTransition);
328
+ const t = activeTransition;
329
+ activeTransition = null;
330
+ reassignPendingTransition(this.Yt);
331
+ finalizePureQueue(t);
332
+ } else {
333
+ if (canUseSimpleSyncFlush(this)) {
334
+ commitPendingNodes();
335
+ if (dirtyQueue.EE >= dirtyQueue.Le) {
336
+ runHeap(dirtyQueue, GlobalQueue.Ce);
337
+ commitPendingNodes();
338
+ }
339
+ } else {
340
+ if (transitions.size) runHeap(zombieQueue, GlobalQueue.Ce);
341
+ finalizePureQueue();
342
+ }
343
+ }
344
+ clock++;
345
+ // Check if finalization added items to the heap (from optimistic reversion)
346
+ scheduled = dirtyQueue.EE >= dirtyQueue.Le;
347
+ // Run lane effects first (for ready lanes), then regular effects
348
+ activeLanes.size && GlobalQueue.Mt(EFFECT_RENDER);
349
+ this.run(EFFECT_RENDER);
350
+ activeLanes.size && GlobalQueue.Mt(EFFECT_USER);
351
+ this.run(EFFECT_USER);
352
+ if (false) ;
353
+ if (false && !scheduled && !activeTransition && transitions.size === 0 && activeLanes.size === 0) ;
354
+ if (false) ;
355
+ } finally {
356
+ this.Bt = false;
357
+ }
358
+ }
359
+ notify(e, t, i, n) {
360
+ // Only track async if the boundary is propagating STATUS_PENDING (not caught by boundary)
361
+ if (t & STATUS_PENDING) {
362
+ if (i & STATUS_PENDING) {
363
+ const t = n !== undefined ? n : e.k;
364
+ if (activeTransition && t) {
365
+ const i = t.source;
366
+ let n = activeTransition.qt.get(i);
367
+ if (!n) activeTransition.qt.set(i, n = new Set);
368
+ const s = n.size;
369
+ n.add(e);
370
+ if (n.size !== s) schedule();
371
+ }
372
+ }
373
+ return true;
374
+ }
375
+ return false;
376
+ }
377
+ initTransition(e) {
378
+ if (e) e = currentTransition(e);
379
+ if (e && e === activeTransition) return;
380
+ if (!e && activeTransition && activeTransition.Ie === clock) return;
381
+ if (!activeTransition) {
382
+ activeTransition = e ?? {
383
+ Ie: clock,
384
+ Yt: [],
385
+ qt: new Map,
386
+ Be: [],
387
+ N: [],
388
+ Lt: new Set,
389
+ Te: [],
390
+ Jt: {
391
+ zt: [ [], [] ],
392
+ wt: []
393
+ },
394
+ Ht: false,
395
+ Qt: new Set
396
+ };
397
+ } else if (e) {
398
+ const t = activeTransition;
399
+ mergeTransitionState(e, t);
400
+ transitions.delete(t);
401
+ activeTransition = e;
402
+ }
403
+ transitions.add(activeTransition);
404
+ activeTransition.Ie = clock;
405
+ if (this.xt !== null) {
406
+ this.xt.ve = activeTransition;
407
+ activeTransition.Yt.push(this.xt);
408
+ this.xt = null;
409
+ }
410
+ if (this.Yt !== activeTransition.Yt) {
411
+ for (let e = 0; e < this.Yt.length; e++) {
412
+ const t = this.Yt[e];
413
+ t.ve = activeTransition;
414
+ activeTransition.Yt.push(t);
415
+ }
416
+ this.Yt = activeTransition.Yt;
417
+ }
418
+ if (this.Be !== activeTransition.Be) {
419
+ for (let e = 0; e < this.Be.length; e++) {
420
+ const t = this.Be[e];
421
+ t.ve = activeTransition;
422
+ activeTransition.Be.push(t);
423
+ }
424
+ this.Be = activeTransition.Be;
425
+ }
426
+ if (this.N !== activeTransition.N) {
427
+ // Adopt ambient marks into the transaction (marks don't hijack the
428
+ // node's _transition — a mark on a plain signal must not entangle
429
+ // unrelated writes to it; the same rule holds one hop downstream:
430
+ // propagation never queues pended subscribers as pending nodes, see
431
+ // propagateAffectsMark, #2893). After adoption the queue aliases the
432
+ // transition's array, so later registrations land there directly.
433
+ activeTransition.N.push(...this.N);
434
+ this.N = activeTransition.N;
435
+ }
436
+ for (const e of activeLanes) {
437
+ if (!e.ve) e.ve = activeTransition;
438
+ }
439
+ if (this.Lt !== activeTransition.Lt) {
440
+ for (const e of this.Lt) activeTransition.Lt.add(e);
441
+ this.Lt = activeTransition.Lt;
442
+ }
443
+ }
444
+ }
445
+
446
+ function queuePendingNode(e) {
447
+ if (activeTransition) {
448
+ globalQueue.Yt.push(e);
449
+ return;
450
+ }
451
+ if (globalQueue.xt === null && globalQueue.Yt.length === 0) {
452
+ globalQueue.xt = e;
453
+ return;
454
+ }
455
+ if (globalQueue.xt !== null) {
456
+ globalQueue.Yt.push(globalQueue.xt);
457
+ globalQueue.xt = null;
458
+ }
459
+ globalQueue.Yt.push(e);
460
+ }
461
+
462
+ // Sticky: flips true on the first refresh() ever (the only setter of
463
+ // REACTIVE_REASK) so the hot notification loop skips the per-subscriber flag
464
+ // clear entirely in apps that never refresh.
465
+ let reaskArmed = false;
466
+
467
+ function armReaskClear() {
468
+ reaskArmed = true;
469
+ }
470
+
471
+ function insertSubs(e, t = false) {
472
+ // Get source lane: prefer node's own lane over current context
473
+ // This is important for isPending signals which need their own lane to flush immediately
474
+ const i = e.Je || currentOptimisticLane;
475
+ const n = e.Ye !== undefined;
476
+ const s = reaskArmed;
477
+ for (let a = e.p; a !== null; a = a.de) {
478
+ // A value-change notification is a new question for the subscriber: any
479
+ // pending re-ask mark (refresh) it carried is superseded.
480
+ if (s) a.Ee.u &= ~REACTIVE_REASK;
481
+ if (n && a.Ee.U & CONFIG_IN_SNAPSHOT_SCOPE) {
482
+ a.Ee.u |= REACTIVE_SNAPSHOT_STALE;
483
+ continue;
484
+ }
485
+ if (t && i) {
486
+ a.Ee.u |= REACTIVE_OPTIMISTIC_DIRTY;
487
+ assignOrMergeLane(a.Ee, i);
488
+ } else if (t) {
489
+ a.Ee.u |= REACTIVE_OPTIMISTIC_DIRTY;
490
+ // No source lane means reversion - clear subscriber's lane so effects go to regular queue
491
+ a.Ee.Je = undefined;
492
+ }
493
+ enqueueSub(a.Ee);
494
+ }
495
+ }
496
+
497
+ function commitPendingNode(e) {
498
+ const t = e;
499
+ if (!t.xe) {
500
+ if (e.ge !== NOT_PENDING) {
501
+ e.Ue = e.ge;
502
+ e.ge = NOT_PENDING;
503
+ }
504
+ if (e.ye || e.pe) GlobalQueue.R(e);
505
+ return;
506
+ }
507
+ if (e.ge !== NOT_PENDING) {
508
+ e.Ue = e.ge;
509
+ e.ge = NOT_PENDING;
510
+ // Set _modified for effects, but not for tracked effects (they handle their own scheduling)
511
+ if (e.De && e.De !== EFFECT_TRACKED) e.it = true;
512
+ }
513
+ t.u &= ~REACTIVE_MANUAL_WRITE;
514
+ if (!(t.i & STATUS_PENDING)) t.i &= ~STATUS_UNINITIALIZED;
515
+ if (t.Ze !== null || t.We !== null) GlobalQueue.Oe(t, false, true);
516
+ if (e.ye || e.pe) GlobalQueue.R(e);
517
+ }
518
+
519
+ function commitPendingNodes() {
520
+ if (globalQueue.xt !== null) {
521
+ commitPendingNode(globalQueue.xt);
522
+ globalQueue.xt = null;
523
+ }
524
+ const e = globalQueue.Yt;
525
+ for (let t = 0; t < e.length; t++) {
526
+ commitPendingNode(e[t]);
527
+ }
528
+ e.length = 0;
529
+ }
530
+
531
+ function finalizePureQueue(e = null, t = false) {
532
+ // For incomplete transitions, skip pending resolution and optimistic reversion
533
+ // For completing transitions or no-transition, resolve pending and revert optimistic
534
+ const i = !t;
535
+ if (i) commitPendingNodes();
536
+ if (!t && globalQueue.wt.length) checkBoundaryChildren(globalQueue);
537
+ const n = dirtyQueue.EE >= dirtyQueue.Le;
538
+ if (n) runHeap(dirtyQueue, GlobalQueue.Ce);
539
+ if (i) {
540
+ if (n) commitPendingNodes();
541
+ // Optimistic reversion: a non-empty batch means _optimisticWrite ran,
542
+ // which installed the engine's hooks.
543
+ const t = e ? e.Be : globalQueue.Be;
544
+ if (t.length) GlobalQueue.Ut(t);
545
+ // Replay entanglement: subs recorded by the read-time gate get rescheduled
546
+ // so they re-run with the now-committed values visible.
547
+ if (e && e.Qt.size) {
548
+ for (const t of e.Qt) {
549
+ if (t.u & REACTIVE_DISPOSED) continue;
550
+ enqueueSub(t);
551
+ }
552
+ e.Qt.clear();
553
+ }
554
+ // Declared motion ends with the transaction: settle (or plain flush end
555
+ // for ambient marks) releases each registration's refcount. A non-empty
556
+ // batch means registerAffectsMark ran, which installed the hook.
557
+ const i = e ? e.N : globalQueue.N;
558
+ if (i.length) GlobalQueue.h(i);
559
+ const s = e ? e.Lt : globalQueue.Lt;
560
+ // A non-empty set means trackOptimisticStore ran, which installed the
561
+ // hook; the hook iterates, clears, and schedules (keeping the loop out of
562
+ // core lets esbuild shake it — rollup already folds the null guard).
563
+ if (s.size) GlobalQueue.Kt(s);
564
+ sweepTransientStoreNodes();
565
+ // Lanes only enter activeLanes through the engine's getOrCreateLane.
566
+ if (activeLanes.size) GlobalQueue.jt(e);
567
+ }
568
+ }
569
+
570
+ function checkBoundaryChildren(e) {
571
+ for (const t of e.wt) {
572
+ t.Se?.();
573
+ checkBoundaryChildren(t);
574
+ }
575
+ }
576
+
577
+ /**
578
+ * Count of live `affects()` registrations across the system (including
579
+ * store-scope inherited marks). Gates the read-path mark check in `read()` so
580
+ * graphs that never use the feature pay one integer compare.
581
+ */ let activeAffectsMarks = 0;
582
+
583
+ /**
584
+ * Counter mutation seam for the mark engine in affects.ts: an imported `let`
585
+ * binding is read-only, and the read-path gate above must stay a plain module
586
+ * variable so `read()` pays one integer compare, not a function call.
587
+ *
588
+ * @internal
589
+ */ function shiftAffectsMarks(e) {
590
+ activeAffectsMarks += e;
591
+ }
592
+
593
+ function reassignPendingTransition(e) {
594
+ for (let t = 0; t < e.length; t++) {
595
+ e[t].ve = activeTransition;
596
+ }
597
+ }
598
+
599
+ const globalQueue = new GlobalQueue;
600
+
601
+ function flush(e) {
602
+ if (e) {
603
+ syncDepth++;
604
+ try {
605
+ return e();
606
+ } finally {
607
+ // Decrement even if the drain throws (a throwing effect): a leaked
608
+ // syncDepth would stop `schedule()` from ever queuing a microtask again.
609
+ try {
610
+ flush();
611
+ } finally {
612
+ syncDepth--;
613
+ }
614
+ }
615
+ }
616
+ if (globalQueue.Bt) {
617
+ return;
618
+ }
619
+ if (halted) return;
620
+ // `flush()` is an explicit drain point, so it must also process an active
621
+ // transition even if no microtask was scheduled for it yet.
622
+ while (scheduled || activeTransition) {
623
+ globalQueue.flush();
624
+ }
625
+ }
626
+
627
+ function runQueue(e, t) {
628
+ for (let i = 0; i < e.length; i++) e[i](t);
629
+ }
630
+
631
+ function reporterBlocksSource(e, t) {
632
+ if (e.u & (REACTIVE_ZOMBIE | REACTIVE_DISPOSED)) return false;
633
+ if (e.m === t || e.M?.has(t)) return true;
634
+ for (let i = e.S; i; i = i.st) {
635
+ let e = i.ot;
636
+ while (e) {
637
+ if (e === t || e.rt === t) return true;
638
+ e = e.en;
639
+ }
640
+ }
641
+ return !!(e.i & STATUS_PENDING && e.k instanceof NotReadyError && e.k.source === t);
642
+ }
643
+
644
+ function transitionComplete(e) {
645
+ if (e.Ht) return true;
646
+ if (e.Te.length) return false;
647
+ let t = true;
648
+ for (const [i, n] of e.qt) {
649
+ let s = false;
650
+ for (const e of n) {
651
+ if (reporterBlocksSource(e, i)) {
652
+ s = true;
653
+ break;
654
+ }
655
+ n.delete(e);
656
+ }
657
+ if (!s) e.qt.delete(i); else if (i.i & STATUS_PENDING && i.k?.source === i) {
658
+ t = false;
659
+ break;
660
+ }
661
+ }
662
+ // Override blockage lives with the engine. Absent hook = "no optimistic
663
+ // blockage", which is exact: only _optimisticWrite (engine) pushes to
664
+ // _optimisticNodes, so without the engine the loop was vacuous anyway.
665
+ if (t && e.Be.length && GlobalQueue.Wt(e)) t = false;
666
+ t && (e.Ht = true);
667
+ return t;
668
+ }
669
+
670
+ function currentTransition(e) {
671
+ while (e.Ht && typeof e.Ht === "object") e = e.Ht;
672
+ return e;
673
+ }
674
+
675
+ function setActiveTransition(e) {
676
+ activeTransition = e;
677
+ }
678
+
679
+ function runInTransition(e, t) {
680
+ const i = activeTransition;
681
+ try {
682
+ activeTransition = currentTransition(e);
683
+ return t();
684
+ } finally {
685
+ activeTransition = i;
686
+ }
687
+ }
688
+
689
+ export { GlobalQueue, Queue, activeAffectsMarks, activeLanes, activeTransition, armReaskClear, assignOrMergeLane, clock, currentTransition, dirtyQueue, enforceLoadingBoundary, finalizePureQueue, findLane, flush, globalQueue, haltReactivity, insertSubs, projectionWriteActive, queuePendingNode, registerTransientStoreNode, resetErrorHalt, runInTransition, schedule, setActiveTransition, setProjectionWriteActive, shiftAffectsMarks, zombieQueue };