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

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 (68) hide show
  1. package/dist/dev.js +3276 -928
  2. package/dist/node.cjs +6646 -4189
  3. package/dist/prod/affects.js +218 -0
  4. package/dist/prod/boundaries.js +568 -0
  5. package/dist/prod/core/action.js +96 -0
  6. package/dist/prod/core/async.js +380 -0
  7. package/dist/prod/core/constants.js +92 -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 +140 -0
  18. package/dist/prod/core/optimistic.js +273 -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 +292 -0
  22. package/dist/prod/index.js +45 -0
  23. package/dist/prod/map.js +293 -0
  24. package/dist/prod/signals.js +389 -0
  25. package/dist/prod/store/optimistic.js +184 -0
  26. package/dist/prod/store/projection.js +184 -0
  27. package/dist/prod/store/reconcile.js +426 -0
  28. package/dist/prod/store/store.js +870 -0
  29. package/dist/prod/store/storePath.js +103 -0
  30. package/dist/prod/store/utils.js +316 -0
  31. package/dist/types/affects.d.ts +1 -1
  32. package/dist/types/boundaries.d.ts +6 -6
  33. package/dist/types/core/action.d.ts +19 -6
  34. package/dist/types/core/async.d.ts +4 -38
  35. package/dist/types/core/constants.d.ts +12 -0
  36. package/dist/types/core/core.d.ts +12 -78
  37. package/dist/types/core/dev.d.ts +7 -0
  38. package/dist/types/core/external.d.ts +0 -30
  39. package/dist/types/core/heap.d.ts +8 -0
  40. package/dist/types/core/index.d.ts +3 -2
  41. package/dist/types/core/lanes.d.ts +2 -0
  42. package/dist/types/core/optimistic.d.ts +6 -0
  43. package/dist/types/core/owner.d.ts +8 -0
  44. package/dist/types/core/scheduler.d.ts +40 -39
  45. package/dist/types/core/types.d.ts +9 -1
  46. package/dist/types/core/verdict.d.ts +2 -0
  47. package/dist/types/store/projection.d.ts +0 -1
  48. package/dist/types/store/store.d.ts +8 -2
  49. package/dist/types-cjs/affects.d.cts +1 -1
  50. package/dist/types-cjs/boundaries.d.cts +6 -6
  51. package/dist/types-cjs/core/action.d.cts +19 -6
  52. package/dist/types-cjs/core/async.d.cts +4 -38
  53. package/dist/types-cjs/core/constants.d.cts +12 -0
  54. package/dist/types-cjs/core/core.d.cts +12 -78
  55. package/dist/types-cjs/core/dev.d.cts +7 -0
  56. package/dist/types-cjs/core/external.d.cts +0 -30
  57. package/dist/types-cjs/core/heap.d.cts +8 -0
  58. package/dist/types-cjs/core/index.d.cts +3 -2
  59. package/dist/types-cjs/core/lanes.d.cts +2 -0
  60. package/dist/types-cjs/core/optimistic.d.cts +6 -0
  61. package/dist/types-cjs/core/owner.d.cts +8 -0
  62. package/dist/types-cjs/core/scheduler.d.cts +40 -39
  63. package/dist/types-cjs/core/types.d.cts +9 -1
  64. package/dist/types-cjs/core/verdict.d.cts +2 -0
  65. package/dist/types-cjs/store/projection.d.cts +0 -1
  66. package/dist/types-cjs/store/store.d.cts +8 -2
  67. package/package.json +8 -6
  68. package/dist/prod.js +0 -4637
@@ -0,0 +1,140 @@
1
+ import { NOT_PENDING } from "./constants.js";
2
+
3
+ import { currentTransition, activeTransition } from "./scheduler.js";
4
+
5
+ // Map from optimistic signal to its lane (reused for multiple writes to same signal)
6
+ const signalLanes = new WeakMap;
7
+
8
+ // All active lanes (for cleanup on transition completion)
9
+ const activeLanes = new Set;
10
+
11
+ /**
12
+ * Get an existing lane for a signal or create a new one.
13
+ * Reuses lane for multiple writes to the same signal.
14
+ */ function getOrCreateLane(n) {
15
+ let e = signalLanes.get(n);
16
+ if (e) {
17
+ return findLane(e);
18
+ }
19
+ // Detect parent lane: _parentSource chains from pendingSignal → pendingValueComputed → original.
20
+ // The child lane should not merge with the parent lane.
21
+ const i = n.en;
22
+ const r = i?.Je ? findLane(i.Je) : null;
23
+ e = {
24
+ rn: n,
25
+ Pe: new Set,
26
+ tn: [ [], [] ],
27
+ an: null,
28
+ ve: activeTransition,
29
+ sn: r
30
+ };
31
+ signalLanes.set(n, e);
32
+ activeLanes.add(e);
33
+ // A companion may have written before the owner's first optimistic write
34
+ // (affects() as an action's first statement pokes the verdict companion of a
35
+ // still lane-less node, #2887), leaving its lane parentless. Adopt it now:
36
+ // parent-child is a property of the nodes, not of write order — otherwise
37
+ // the owner's write merges the companion's subscribers into this lane and
38
+ // their effects wait on its async instead of flushing immediately.
39
+ adoptCompanionLane(n.ye, e);
40
+ adoptCompanionLane(n.pe, e);
41
+ return e;
42
+ }
43
+
44
+ function adoptCompanionLane(n, e) {
45
+ if (!n) return;
46
+ const i = signalLanes.get(n);
47
+ if (!i) return;
48
+ const r = findLane(i);
49
+ // Only the companion's own unmerged root is safely re-parentable: a root
50
+ // that absorbed other lanes carries work that is not a child of this owner.
51
+ if (r !== e && r.rn === n && !r.sn) r.sn = e;
52
+ }
53
+
54
+ /**
55
+ * Union-find: find the root lane.
56
+ */ function findLane(n) {
57
+ while (n.an) n = n.an;
58
+ return n;
59
+ }
60
+
61
+ /**
62
+ * Merge two lanes when their dependency graphs overlap.
63
+ */ function mergeLanes(n, e) {
64
+ n = findLane(n);
65
+ e = findLane(e);
66
+ if (n === e) return n;
67
+ e.an = n;
68
+ // Move (not copy) the merged lane's work: after the merge all routing goes
69
+ // through findLane() to the root, so anything left behind here is dead —
70
+ // and anything *added* here later is a routing bug (INV-5).
71
+ for (const i of e.Pe) n.Pe.add(i);
72
+ e.Pe.clear();
73
+ n.tn[0].push(...e.tn[0]);
74
+ n.tn[1].push(...e.tn[1]);
75
+ e.tn[0].length = 0;
76
+ e.tn[1].length = 0;
77
+ return n;
78
+ }
79
+
80
+ /**
81
+ * Resolve a node's lane: follow union-find chain, verify active, clear if stale.
82
+ */ function resolveLane(n) {
83
+ const e = n.Je;
84
+ if (!e) return undefined;
85
+ const i = findLane(e);
86
+ if (activeLanes.has(i)) return i;
87
+ n.Je = undefined;
88
+ return undefined;
89
+ }
90
+
91
+ function resolveTransition(n) {
92
+ // An active override answers with its owner, not its lane: lanes are
93
+ // scheduling affinity and a shared subscriber merges them across
94
+ // transactions (#2912) — the merged root's _transition would hand this
95
+ // node's override to whichever action wrote last through the shared
96
+ // reader. Chase merge chains; a dead owner settled through another path.
97
+ if (hasActiveOverride(n) && n.fn) {
98
+ const e = n.fn = currentTransition(n.fn);
99
+ if (e.cn !== true) return e;
100
+ n.fn = null;
101
+ }
102
+ return resolveLane(n)?.ve ?? n.ve;
103
+ }
104
+
105
+ /**
106
+ * Check if a node has an active optimistic override.
107
+ */ function hasActiveOverride(n) {
108
+ return !!(n.be !== undefined && n.be !== NOT_PENDING);
109
+ }
110
+
111
+ /**
112
+ * Assign or merge a lane onto a node. At convergence points (node already has
113
+ * a different active lane), merge unless the node has an active override.
114
+ */ function assignOrMergeLane(n, e) {
115
+ const i = findLane(e);
116
+ const r = n.Je;
117
+ if (r) {
118
+ // If the subscriber's lane was merged into another lane, it's stale —
119
+ // replace it with the new source lane instead of following the merge chain
120
+ // (which would incorrectly merge the new lane into the old group)
121
+ if (r.an) {
122
+ n.Je = e;
123
+ return;
124
+ }
125
+ const t = findLane(r);
126
+ if (activeLanes.has(t)) {
127
+ if (t !== i && !hasActiveOverride(n)) {
128
+ // Parent-child lanes stay independent so isPending resolves without
129
+ // waiting for the parent's async. The child keeps ownership.
130
+ if (i.sn && findLane(i.sn) === t) {
131
+ n.Je = e;
132
+ } else if (t.sn && findLane(t.sn) === i) ; else mergeLanes(i, t);
133
+ }
134
+ return;
135
+ }
136
+ }
137
+ n.Je = e;
138
+ }
139
+
140
+ export { activeLanes, assignOrMergeLane, findLane, getOrCreateLane, hasActiveOverride, mergeLanes, resolveLane, resolveTransition, signalLanes };
@@ -0,0 +1,273 @@
1
+ import { NOT_PENDING, unwrapOverride, STATUS_UNINITIALIZED, OVERRIDE_UNDEFINED, STATUS_PENDING, CONFIG_OWNED_WRITE, REACTIVE_MANUAL_WRITE, REACTIVE_OPTIMISTIC_DIRTY, EFFECT_RENDER, EFFECT_USER } from "./constants.js";
2
+
3
+ import { latestReadActive, currentOptimisticLane, stale } from "./core.js";
4
+
5
+ import { NotReadyError } from "./error.js";
6
+
7
+ import { enqueueSub } from "./heap.js";
8
+
9
+ import "./invariants.js";
10
+
11
+ import { resolveTransition, getOrCreateLane, hasActiveOverride, activeLanes, signalLanes, findLane, resolveLane, assignOrMergeLane } from "./lanes.js";
12
+
13
+ import { GlobalQueue, activeTransition, globalQueue, clock, insertSubs, schedule, finalizePureQueue } from "./scheduler.js";
14
+
15
+ /**
16
+ * The optimistic write engine, moved out of core.ts/scheduler.ts. Everything
17
+ * here serves only optimistic overrides — createOptimistic,
18
+ * createOptimisticStore (and its store-node writes), and the verdict layer's
19
+ * companions (which are optimistic nodes). Modules that can create optimistic
20
+ * state call `installOptimisticEngine()` before creating it; apps that never
21
+ * import one of those APIs never retain any of this.
22
+ *
23
+ * Core call sites fire the hooks behind guards on state only this module can
24
+ * create (`_overrideValue !== undefined`, `currentOptimisticLane !== null`,
25
+ * `_optimisticNodes.length`, `activeLanes.size`), so `!` invocations are safe
26
+ * once the gate holds — the same late-binding contract as verdict.ts.
27
+ */
28
+ // When a background transition is stashed, plain optimistic signals need one
29
+ // committed-view rerun. Keep that override local to the stash flush.
30
+ let stashedOptimisticReads = null;
31
+
32
+ /** The optimistic half of setSignal, fired when `_overrideValue !== undefined`. */ function optimisticWrite(e, n) {
33
+ const t = e.be !== NOT_PENDING;
34
+ const i = t ? unwrapOverride(e.be) : e.Ue;
35
+ if (typeof n === "function") n = n(i);
36
+ const u = !!(e.i & STATUS_UNINITIALIZED) || !e.Ge || !e.Ge(i, n);
37
+ if (!u) {
38
+ // Same-value write with an active override still entangles the current
39
+ // action's transition — the hold must outlast all overlapping actions.
40
+ if (t) {
41
+ const n = resolveTransition(e);
42
+ if (n && activeTransition !== n) globalQueue.initTransition(n);
43
+ }
44
+ return n;
45
+ }
46
+ if (t) globalQueue.initTransition(resolveTransition(e));
47
+ // No revert target is stashed: while the override is active every reader
48
+ // sees it (A17), so authoritative arrivals commit silently into _value and
49
+ // reverting is just dropping the override — _value is already correct.
50
+ else globalQueue.N.Be.push(e);
51
+ // Stamp ownership on the node (post-merge, so entangled writers share the
52
+ // joint root). resolveTransition prefers this over the lane's _transition,
53
+ // which a shared subscriber can merge across transactions (#2912).
54
+ e.fn = activeTransition;
55
+ const s = getOrCreateLane(e);
56
+ e.Je = s;
57
+ // Literal undefined must not land raw: the slot doubles as the optimistic
58
+ // brand, and erasing it makes the write invisible and routes follow-up
59
+ // writes off the optimistic path into permanent commits (#2898).
60
+ e.be = n === undefined ? OVERRIDE_UNDEFINED : n;
61
+ GlobalQueue._e !== null && GlobalQueue._e(e, n);
62
+ e.Ie = clock;
63
+ insertSubs(e, true);
64
+ schedule();
65
+ return n;
66
+ }
67
+
68
+ function readStashed(e) {
69
+ return !!stashedOptimisticReads?.has(e);
70
+ }
71
+
72
+ function queueStashedOptimisticEffects(e) {
73
+ for (let n = e.p; n !== null; n = n.de) {
74
+ const e = n.Ee;
75
+ if (!e.De) continue;
76
+ enqueueSub(e);
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Incomplete-transition finalization when the stashed transition holds
82
+ * optimistic nodes: give plain optimistic signals one committed-view rerun.
83
+ */ function stashOptimistic(e) {
84
+ stashedOptimisticReads = new Set;
85
+ for (let n = 0; n < e.Be.length; n++) {
86
+ const t = e.Be[n];
87
+ if (t.xe || t.U & CONFIG_OWNED_WRITE) continue;
88
+ stashedOptimisticReads.add(t);
89
+ queueStashedOptimisticEffects(t);
90
+ }
91
+ try {
92
+ finalizePureQueue(null, true);
93
+ } finally {
94
+ stashedOptimisticReads = null;
95
+ }
96
+ }
97
+
98
+ /**
99
+ * transitionComplete's override blockage: a settling transition stays open
100
+ * while one of its optimistic nodes holds an active override that is still
101
+ * pending on real (non-affects-sentinel) async.
102
+ */ function transitionBlocked(e) {
103
+ for (let n = 0; n < e.Be.length; n++) {
104
+ const t = e.Be[n];
105
+ if (hasActiveOverride(t) && "i" in t && t.i & STATUS_PENDING && t.k instanceof NotReadyError &&
106
+ // Mark-sourced pending never blocks settlement: affects() releases AT
107
+ // settle, so counting its sentinel here would deadlock the window it
108
+ // is scoped to.
109
+ !t.k.source?.l) {
110
+ return true;
111
+ }
112
+ }
113
+ return false;
114
+ }
115
+
116
+ function resolveOptimisticNodes(e) {
117
+ // Settlement writes below (snapCompanionsToState → updatePendingSignal-style
118
+ // notifications) may push fresh optimistic nodes; only this batch settles
119
+ // now, so iterate a fixed window and splice it out at the end.
120
+ const n = e.length;
121
+ for (let t = 0; t < n; t++) {
122
+ const n = e[t];
123
+ n.Je = undefined;
124
+ // Revert is a pure drop: there is no revert target to commit —
125
+ // override-covered authoritative values hold in _pendingValue and
126
+ // elevate on their OWN transition's schedule (A18 as re-ruled 2026-07-07).
127
+ if (!(n.i & STATUS_PENDING)) n.i &= ~STATUS_UNINITIALIZED;
128
+ const i = n.be;
129
+ n.be = NOT_PENDING;
130
+ if (i !== NOT_PENDING && n.Ue !== unwrapOverride(i)) insertSubs(n, true);
131
+ n.ve = null;
132
+ n.fn = null;
133
+ }
134
+ // Settlement checkpoint (#2838): companions caught in this batch (or owned
135
+ // by a node in it) re-derive from committed state, so verdicts survive the
136
+ // transition that produced them (A19 — pending is a property of the data).
137
+ for (let t = 0; t < n; t++) {
138
+ const n = e[t];
139
+ if (n.ye || n.pe) GlobalQueue.R(n);
140
+ const i = n.en;
141
+ if (i && (i.ye === n || i.pe === n)) GlobalQueue.R(i);
142
+ }
143
+ e.splice(0, n);
144
+ }
145
+
146
+ function runQueue(e, n) {
147
+ for (let t = 0; t < e.length; t++) e[t](n);
148
+ }
149
+
150
+ /**
151
+ * Run effects from all lanes that are ready (no pending async).
152
+ */ function runLaneEffects(e) {
153
+ for (const n of activeLanes) {
154
+ if (n.an || n.Pe.size > 0) continue;
155
+ const t = n.tn[e - 1];
156
+ if (t.length) {
157
+ n.tn[e - 1] = [];
158
+ runQueue(t, e);
159
+ }
160
+ }
161
+ }
162
+
163
+ function cleanupCompletedLanes(e) {
164
+ for (const n of activeLanes) {
165
+ const t = e ? n.ve === e : !n.ve;
166
+ if (!t) continue;
167
+ if (!n.an) {
168
+ if (n.tn[0].length) runQueue(n.tn[0], EFFECT_RENDER);
169
+ if (n.tn[1].length) runQueue(n.tn[1], EFFECT_USER);
170
+ }
171
+ if (n.rn.Je === n) n.rn.Je = undefined;
172
+ n.Pe.clear();
173
+ n.tn[0].length = 0;
174
+ n.tn[1].length = 0;
175
+ activeLanes.delete(n);
176
+ signalLanes.delete(n.rn);
177
+ }
178
+ }
179
+
180
+ /** read()'s per-lane suspension test (pending-throw path, lane context). */ function laneSuspends(e) {
181
+ // Per-lane suspension: only throw if in same lane as pending async
182
+ // AND the node doesn't have an active override (overrides are the visible value,
183
+ // downstream in the lane should read the override, not throw)
184
+ const n = e.Je;
185
+ if (!n) return false;
186
+ return findLane(n) === findLane(currentOptimisticLane) && !hasActiveOverride(e);
187
+ }
188
+
189
+ /**
190
+ * read()'s entanglement gate: a reader recomputing under an optimistic lane
191
+ * that reads a pending mid-transition write sees the committed value; the sub
192
+ * is recorded for replay at commit.
193
+ */ function gatedRead(e, n, t) {
194
+ if (latestReadActive || e.ge === NOT_PENDING || e.xe || n !== e && !(n.u & REACTIVE_MANUAL_WRITE)) {
195
+ return false;
196
+ }
197
+ activeTransition.un.add(t);
198
+ return true;
199
+ }
200
+
201
+ /**
202
+ * read()'s value selection under a lane: return the committed `_value` for
203
+ * optimistic/lane-assigned signals, stale-mode reads, and pending owners.
204
+ */ function laneReadsCommitted(e, n, t) {
205
+ return e.be !== undefined || !!e.Je || n === e && stale && t.en !== e || !!(n.i & STATUS_PENDING);
206
+ }
207
+
208
+ /**
209
+ * recompute()'s lane posture: resolve the node's own lane (own=true), or adopt
210
+ * a dependency's optimistic lane (own=false — parent-deeper-than-owned-child
211
+ * can run before its OPT-dirty child propagates).
212
+ */ function recomputeLane(e, n) {
213
+ if (n) return resolveLane(e) ?? null;
214
+ for (let n = e.S; n; n = n.st) {
215
+ const t = n.ot;
216
+ if (t.u & REACTIVE_OPTIMISTIC_DIRTY) {
217
+ const n = resolveLane(t);
218
+ if (n) {
219
+ e.u |= REACTIVE_OPTIMISTIC_DIRTY;
220
+ assignOrMergeLane(e, n);
221
+ return n;
222
+ }
223
+ }
224
+ }
225
+ return null;
226
+ }
227
+
228
+ /** recompute()'s catch path: track pending async in the current lane. */ function laneAsyncPending(e) {
229
+ const n = findLane(currentOptimisticLane);
230
+ if (n.rn !== e) {
231
+ n.Pe.add(e);
232
+ e.Je = n;
233
+ GlobalQueue.P !== null && GlobalQueue.P(n.rn);
234
+ }
235
+ }
236
+
237
+ /** recompute()'s success path: the node's async settled, clear it from its lane. */ function laneAsyncSettled(e) {
238
+ const n = resolveLane(e);
239
+ if (n) {
240
+ n.Pe.delete(e);
241
+ GlobalQueue.P !== null && GlobalQueue.P(n.rn);
242
+ }
243
+ }
244
+
245
+ function trackOptimisticStore(e) {
246
+ // After initTransition, globalQueue._batch IS activeTransition (same reference)
247
+ globalQueue.N.dn.add(e);
248
+ schedule();
249
+ }
250
+
251
+ /**
252
+ * Installs the engine's hooks. Idempotent; called by every module that can
253
+ * create optimistic state (verdict.ts at module top level, createOptimistic
254
+ * and createOptimisticStore at first call) BEFORE any optimistic node exists.
255
+ */ function installOptimisticEngine() {
256
+ if (GlobalQueue.bt !== null) return;
257
+ GlobalQueue.bt = optimisticWrite;
258
+ GlobalQueue.En = resolveOptimisticNodes;
259
+ GlobalQueue.Tn = stashOptimistic;
260
+ GlobalQueue.In = transitionBlocked;
261
+ GlobalQueue.Nn = cleanupCompletedLanes;
262
+ GlobalQueue.On = runLaneEffects;
263
+ GlobalQueue.Dt = readStashed;
264
+ GlobalQueue.gt = gatedRead;
265
+ GlobalQueue.ht = laneSuspends;
266
+ GlobalQueue.vt = laneReadsCommitted;
267
+ GlobalQueue.$e = recomputeLane;
268
+ GlobalQueue.et = laneAsyncPending;
269
+ GlobalQueue.Xe = laneAsyncSettled;
270
+ GlobalQueue.mn = trackOptimisticStore;
271
+ }
272
+
273
+ export { installOptimisticEngine };
@@ -0,0 +1,293 @@
1
+ import { REACTIVE_DISPOSED, REACTIVE_ZOMBIE, REACTIVE_IN_HEAP, REACTIVE_IN_HEAP_HEIGHT, CONFIG_TRANSPARENT, defaultContext } from "./constants.js";
2
+
3
+ import { context, runWithOwner, pendingCheckActive, latestReadActive, tracking } from "./core.js";
4
+
5
+ import { unlinkSubs } from "./graph.js";
6
+
7
+ import { deleteFromHeap, queueFor, insertIntoHeap, insertIntoHeapHeight } from "./heap.js";
8
+
9
+ import { GlobalQueue, zombieQueue, dirtyQueue, globalQueue } from "./scheduler.js";
10
+
11
+ const PENDING_OWNER = {};
12
+
13
+ // Dummy owner to trigger store's read() path
14
+ function markDisposal(e) {
15
+ let n = e.He;
16
+ while (n) {
17
+ const e = n.u;
18
+ n.u = e | REACTIVE_ZOMBIE;
19
+ // migrate height-adjust entries too, not just recompute entries: every
20
+ // `deleteFromHeap` call site picks the queue from the zombie flag, so a
21
+ // node left physically linked in `dirtyQueue` after being zombified gets
22
+ // unlinked from the wrong queue on dispose, corrupting the bucket and
23
+ // livelocking the next `runHeap` that reaches it (#2759)
24
+ if (e & (REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT)) {
25
+ deleteFromHeap(n, e & REACTIVE_ZOMBIE ? zombieQueue : dirtyQueue);
26
+ if (e & REACTIVE_IN_HEAP) insertIntoHeap(n, zombieQueue); else insertIntoHeapHeight(n, zombieQueue);
27
+ }
28
+ markDisposal(n);
29
+ n = n.Ve;
30
+ }
31
+ }
32
+
33
+ function dispose(e) {
34
+ let n = e.S;
35
+ while (n !== null) {
36
+ n = unlinkSubs(n);
37
+ }
38
+ e.S = null;
39
+ e.Ke = null;
40
+ disposeChildren(e, true);
41
+ }
42
+
43
+ function disposeChildren(e, n = false, t) {
44
+ const i = e.u;
45
+ if (i & REACTIVE_DISPOSED) return;
46
+ if (n) {
47
+ e.u = i | REACTIVE_DISPOSED;
48
+ // Companions are created detached and outlive their owner, but a verdict
49
+ // must not: a disposed source can never settle, so an isPending companion
50
+ // latched `true` here would hold a spinner forever (INV-9, the PR #2845
51
+ // edge). Snap runs after the DISPOSED flag is set so the oracle reads
52
+ // false, and notifies subscribers still watching the companion.
53
+ const n = e;
54
+ if (n.ye || n.pe) GlobalQueue.R(n);
55
+ }
56
+ if (n && e.xe) e.Ae = null;
57
+ let l = t ? e.Ze : e.He;
58
+ while (l) {
59
+ const e = l.Ve;
60
+ if (l.S) {
61
+ const e = l;
62
+ deleteFromHeap(e, queueFor(e));
63
+ let n = e.S;
64
+ do {
65
+ n = unlinkSubs(n);
66
+ } while (n !== null);
67
+ e.S = null;
68
+ e.Ke = null;
69
+ }
70
+ disposeChildren(l, true);
71
+ l = e;
72
+ }
73
+ if (t) {
74
+ e.Ze = null;
75
+ } else {
76
+ e.He = null;
77
+ e.je = 0;
78
+ }
79
+ // O(1) splice out of parent's chain on individual dispose. Skipped during
80
+ // batch dispose (parent already disposed) and zombie disposal (node sits on
81
+ // parent's _pendingFirstChild). We leave node._nextSibling intact so outer
82
+ // walks that already advanced past us still reach later siblings.
83
+ if (n && !t && !(i & REACTIVE_ZOMBIE) && e.Fe !== null && !(e.Fe.u & REACTIVE_DISPOSED)) {
84
+ const n = e.Nt;
85
+ const t = e.Ve;
86
+ if (n !== null) n.Ve = t; else e.Fe.He = t;
87
+ if (t !== null) t.Nt = n;
88
+ e.Nt = null;
89
+ }
90
+ runDisposal(e, t);
91
+ // Final effect-returned cleanup fires at true disposal, after `_disposal`
92
+ // to mirror rerun ordering (compute-phase teardown first, cleanup last).
93
+ if (n && e.dt) {
94
+ const n = e.dt;
95
+ e.dt = undefined;
96
+ n();
97
+ }
98
+ }
99
+
100
+ function runDisposal(e, n) {
101
+ let t = n ? e.We : e.Me;
102
+ if (!t) return;
103
+ if (Array.isArray(t)) {
104
+ for (let e = 0; e < t.length; e++) {
105
+ const n = t[e];
106
+ n.call(n);
107
+ }
108
+ } else {
109
+ t.call(t);
110
+ }
111
+ n ? e.We = null : e.Me = null;
112
+ }
113
+
114
+ function childId(e, n) {
115
+ let t = e;
116
+ while (t.U & CONFIG_TRANSPARENT && t.Fe) t = t.Fe;
117
+ if (t.id != null) return formatId(t.id, n ? t.je++ : t.je);
118
+ throw new Error("");
119
+ }
120
+
121
+ /**
122
+ * Allocates and returns the next stable child id for `owner`. Used by
123
+ * hydration plumbing and `createUniqueId`. Not part of the user-facing API.
124
+ *
125
+ * @internal
126
+ */ function getNextChildId(e) {
127
+ return childId(e, true);
128
+ }
129
+
130
+ /**
131
+ * The id a freshly-created node inherits: an explicit `options.id` wins;
132
+ * transparent nodes share their parent's id; otherwise the parent's next
133
+ * child id is consumed (or `undefined` outside an id-carrying tree).
134
+ */ function inheritId(e, n, t) {
135
+ return e?.id ?? (n ? t?.id : t?.id != null ? getNextChildId(t) : undefined);
136
+ }
137
+
138
+ /**
139
+ * Returns the *next* child id for `owner` without consuming it. Used by
140
+ * hydration plumbing to peek at the id a future child will receive.
141
+ *
142
+ * @internal
143
+ */ function peekNextChildId(e) {
144
+ return childId(e, false);
145
+ }
146
+
147
+ function formatId(e, n) {
148
+ const t = n.toString(36), i = t.length - 1;
149
+ return e + (i ? String.fromCharCode(64 + i) : "") + t;
150
+ }
151
+
152
+ /**
153
+ * Returns the currently-tracking observer (the computation that subscribes to
154
+ * reactive reads at this point), or `null` if reads here would be untracked.
155
+ * Used by reactive primitives that need to know whether they're inside a
156
+ * tracking scope. App code rarely needs this — see `getOwner()` for the
157
+ * lifecycle owner instead.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * // Library predicate: only register a hot-path subscription when the
162
+ * // caller is inside a tracking scope (memo / effect compute / JSX).
163
+ * function trackIfTracked(source: () => unknown) {
164
+ * if (getObserver()) source();
165
+ * }
166
+ * ```
167
+ */ function getObserver() {
168
+ if (pendingCheckActive || latestReadActive) return PENDING_OWNER;
169
+ return tracking ? context : null;
170
+ }
171
+
172
+ /**
173
+ * Returns the current reactive **owner** — the lifecycle node that the next
174
+ * `cleanup()` / `onCleanup()` / `createSignal()` etc. will be attached to.
175
+ *
176
+ * Returns `null` if called outside any owner. Capture the owner with
177
+ * `getOwner()` and re-enter it later with `runWithOwner(owner, fn)` to attach
178
+ * disposables created from a callback (event handler, async resolution, etc.)
179
+ * back to a component's lifecycle.
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * function defer<T>(fn: () => T) {
184
+ * const owner = getOwner();
185
+ * queueMicrotask(() => runWithOwner(owner, fn));
186
+ * }
187
+ * ```
188
+ */ function getOwner() {
189
+ return context;
190
+ }
191
+
192
+ /**
193
+ * Low-level: registers `fn` as a disposal callback on the current owner.
194
+ * Most code should use `onCleanup()` from `solid-js`, which adds dev-mode
195
+ * checks. `cleanup()` is the unchecked primitive used by internals.
196
+ */ function cleanup(e) {
197
+ if (!context) return e;
198
+ if (!context.Me) context.Me = e; else if (Array.isArray(context.Me)) context.Me.push(e); else context.Me = [ context.Me, e ];
199
+ return e;
200
+ }
201
+
202
+ /**
203
+ * Returns `true` if the owner has been disposed (or marked zombie pending
204
+ * disposal). Pair with a captured owner to bail out of late callbacks whose
205
+ * surrounding component already unmounted.
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * function onSettleSafe(fn: () => void) {
210
+ * const owner = getOwner();
211
+ * queueMicrotask(() => {
212
+ * if (owner && isDisposed(owner)) return; // component unmounted; skip
213
+ * runWithOwner(owner, fn);
214
+ * });
215
+ * }
216
+ * ```
217
+ */ function isDisposed(e) {
218
+ return !!(e.u & (REACTIVE_DISPOSED | REACTIVE_ZOMBIE));
219
+ }
220
+
221
+ function disposeRootSelf(e = true) {
222
+ disposeChildren(this, e);
223
+ }
224
+
225
+ /**
226
+ * Creates a fresh owner attached as a child of the current owner (or as a
227
+ * detached root if there is none). Used by framework internals to group
228
+ * cleanups; app code should use `createRoot()` (host a reactive scope outside
229
+ * a component) or `runWithOwner()` (re-enter a captured owner).
230
+ *
231
+ * @internal
232
+ */ function createOwner(e) {
233
+ const n = context;
234
+ const t = e?.transparent ?? false;
235
+ const i = {
236
+ id: inheritId(e, t, n),
237
+ U: t ? CONFIG_TRANSPARENT : 0,
238
+ At: true,
239
+ Ct: n?.At ? n.Ct : n,
240
+ He: null,
241
+ Ve: null,
242
+ Nt: null,
243
+ Me: null,
244
+ v: n?.v ?? globalQueue,
245
+ we: n?.we || defaultContext,
246
+ je: 0,
247
+ We: null,
248
+ Ze: null,
249
+ Fe: n,
250
+ dispose: disposeRootSelf
251
+ };
252
+ if (n) {
253
+ const e = n.He;
254
+ if (e === null) {
255
+ n.He = i;
256
+ } else {
257
+ i.Ve = e;
258
+ e.Nt = i;
259
+ n.He = i;
260
+ }
261
+ }
262
+ return i;
263
+ }
264
+
265
+ /**
266
+ * Creates a detached reactive root. The callback receives a `dispose()`
267
+ * function which, when called, tears down every signal, memo, effect, and
268
+ * `onCleanup` registered inside the root.
269
+ *
270
+ * Use this to host long-lived reactive scopes outside of a component (custom
271
+ * controllers, app bootstrapping, tests). Inside a component, prefer
272
+ * letting Solid's component lifecycle own things.
273
+ *
274
+ * @example
275
+ * ```ts
276
+ * const dispose = createRoot(dispose => {
277
+ * const [n, setN] = createSignal(0);
278
+ * createEffect(() => n(), value => console.log(value));
279
+ * setInterval(() => setN(x => x + 1), 1000);
280
+ * return dispose;
281
+ * });
282
+ *
283
+ * // Later, to tear everything down:
284
+ * dispose();
285
+ * ```
286
+ *
287
+ * @description https://docs.solidjs.com/reference/reactive-utilities/create-root
288
+ */ function createRoot(e, n) {
289
+ const t = createOwner(n);
290
+ return runWithOwner(t, () => e(() => t.dispose()));
291
+ }
292
+
293
+ export { cleanup, createOwner, createRoot, dispose, disposeChildren, getNextChildId, getObserver, getOwner, inheritId, isDisposed, markDisposal, peekNextChildId };