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

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 (117) hide show
  1. package/dist/dev-shared.js +5243 -0
  2. package/dist/dev.attribution.js +2333 -0
  3. package/dist/dev.js +1229 -6834
  4. package/dist/observe/affects.js +127 -0
  5. package/dist/observe/attribution.js +1 -0
  6. package/dist/observe/boundaries.js +586 -0
  7. package/dist/observe/core/action.js +167 -0
  8. package/dist/observe/core/async.js +736 -0
  9. package/dist/observe/core/attribution-hooks.js +60 -0
  10. package/dist/observe/core/attribution.js +2318 -0
  11. package/dist/{types-cjs/core/constants.d.cts → observe/core/constants.js} +129 -73
  12. package/dist/{types-cjs/core/context.d.cts → observe/core/context.js} +37 -12
  13. package/dist/observe/core/core.js +1289 -0
  14. package/dist/observe/core/dev.js +232 -0
  15. package/dist/observe/core/effect.js +179 -0
  16. package/dist/observe/core/error.js +80 -0
  17. package/dist/observe/core/external.js +98 -0
  18. package/dist/observe/core/graph.js +155 -0
  19. package/dist/observe/core/heap.js +147 -0
  20. package/dist/observe/core/invariants.js +43 -0
  21. package/dist/observe/core/lanes.js +168 -0
  22. package/dist/observe/core/optimistic.js +438 -0
  23. package/dist/observe/core/owner.js +308 -0
  24. package/dist/observe/core/scheduler.js +1190 -0
  25. package/dist/observe/core/verdict.js +551 -0
  26. package/dist/observe/index.js +55 -0
  27. package/dist/observe/map.js +482 -0
  28. package/dist/{types-cjs/signals.d.cts → observe/signals.js} +295 -317
  29. package/dist/observe/store/index.js +38 -0
  30. package/dist/observe/store/next/optimistic.js +651 -0
  31. package/dist/observe/store/next/projection.js +242 -0
  32. package/dist/observe/store/next/reconcile.js +373 -0
  33. package/dist/observe/store/next/store.js +2213 -0
  34. package/dist/observe/store/next/target.js +51 -0
  35. package/dist/observe/store/store.js +323 -0
  36. package/dist/observe/store/storePath.js +103 -0
  37. package/dist/observe/store/utils.js +210 -0
  38. package/dist/prod/attribution.js +32 -0
  39. package/dist/prod/core/action.js +52 -32
  40. package/dist/prod/core/async.js +171 -127
  41. package/dist/prod/core/constants.js +24 -1
  42. package/dist/prod/core/context.js +3 -3
  43. package/dist/prod/core/core.js +434 -291
  44. package/dist/prod/core/effect.js +54 -34
  45. package/dist/prod/core/external.js +2 -2
  46. package/dist/prod/core/graph.js +32 -32
  47. package/dist/prod/core/heap.js +49 -44
  48. package/dist/prod/core/lanes.js +45 -38
  49. package/dist/prod/core/optimistic.js +207 -73
  50. package/dist/prod/core/owner.js +43 -39
  51. package/dist/prod/core/scheduler.js +319 -190
  52. package/dist/prod/core/verdict.js +67 -67
  53. package/dist/prod/index.js +9 -3
  54. package/dist/prod/map.js +292 -141
  55. package/dist/prod/signals.js +7 -10
  56. package/dist/prod/store/next/optimistic.js +140 -125
  57. package/dist/prod/store/next/projection.js +3 -3
  58. package/dist/prod/store/next/reconcile.js +87 -84
  59. package/dist/prod/store/next/store.js +524 -280
  60. package/dist/prod/store/next/target.js +32 -10
  61. package/dist/prod/store/store.js +12 -14
  62. package/dist/prod/store/utils.js +36 -27
  63. package/dist/types/attribution.d.ts +15 -0
  64. package/dist/types/attribution.prod.d.ts +13 -0
  65. package/dist/types/core/async.d.ts +1 -1
  66. package/dist/types/core/attribution-hooks.d.ts +119 -6
  67. package/dist/types/core/attribution.d.ts +208 -53
  68. package/dist/types/core/constants.d.ts +23 -0
  69. package/dist/types/core/core.d.ts +7 -0
  70. package/dist/types/core/dev.d.ts +124 -26
  71. package/dist/types/core/effect.d.ts +8 -1
  72. package/dist/types/core/index.d.ts +2 -1
  73. package/dist/types/core/lanes.d.ts +8 -1
  74. package/dist/types/core/scheduler.d.ts +60 -0
  75. package/dist/types/core/types.d.ts +29 -9
  76. package/dist/types/index.d.ts +9 -2
  77. package/dist/types/map.d.ts +2 -0
  78. package/dist/types/store/index.d.ts +1 -0
  79. package/dist/types/store/next/store.d.ts +5 -0
  80. package/dist/types/store/next/target.d.ts +36 -9
  81. package/dist/types/store/utils.d.ts +7 -0
  82. package/package.json +21 -17
  83. package/dist/node.cjs +0 -10541
  84. package/dist/node.dev.cjs +0 -13724
  85. package/dist/types-cjs/affects.d.cts +0 -47
  86. package/dist/types-cjs/boundaries.d.cts +0 -175
  87. package/dist/types-cjs/core/action.d.cts +0 -64
  88. package/dist/types-cjs/core/async.d.cts +0 -23
  89. package/dist/types-cjs/core/attribution-hooks.d.cts +0 -127
  90. package/dist/types-cjs/core/attribution.d.cts +0 -495
  91. package/dist/types-cjs/core/core.d.cts +0 -185
  92. package/dist/types-cjs/core/dev.d.cts +0 -136
  93. package/dist/types-cjs/core/effect.d.cts +0 -30
  94. package/dist/types-cjs/core/error.d.cts +0 -56
  95. package/dist/types-cjs/core/external.d.cts +0 -15
  96. package/dist/types-cjs/core/graph.d.cts +0 -28
  97. package/dist/types-cjs/core/heap.d.cts +0 -24
  98. package/dist/types-cjs/core/index.d.cts +0 -13
  99. package/dist/types-cjs/core/invariants.d.cts +0 -59
  100. package/dist/types-cjs/core/lanes.d.cts +0 -54
  101. package/dist/types-cjs/core/optimistic.d.cts +0 -6
  102. package/dist/types-cjs/core/owner.d.cts +0 -124
  103. package/dist/types-cjs/core/scheduler.d.cts +0 -236
  104. package/dist/types-cjs/core/types.d.cts +0 -204
  105. package/dist/types-cjs/core/verdict.d.cts +0 -2
  106. package/dist/types-cjs/index.d.cts +0 -10
  107. package/dist/types-cjs/map.d.cts +0 -69
  108. package/dist/types-cjs/package.json +0 -3
  109. package/dist/types-cjs/store/index.d.cts +0 -18
  110. package/dist/types-cjs/store/next/optimistic.d.cts +0 -25
  111. package/dist/types-cjs/store/next/projection.d.cts +0 -8
  112. package/dist/types-cjs/store/next/reconcile.d.cts +0 -10
  113. package/dist/types-cjs/store/next/store.d.cts +0 -121
  114. package/dist/types-cjs/store/next/target.d.cts +0 -166
  115. package/dist/types-cjs/store/store.d.cts +0 -143
  116. package/dist/types-cjs/store/storePath.d.cts +0 -58
  117. package/dist/types-cjs/store/utils.d.cts +0 -74
@@ -0,0 +1,38 @@
1
+ import "../core/core.js";
2
+
3
+ import "../core/scheduler.js";
4
+
5
+ import "../core/invariants.js";
6
+
7
+ import "../core/verdict.js";
8
+
9
+ import "../core/effect.js";
10
+
11
+ import { createStoreNext, deepNext, snapshotNext } from "./next/store.js";
12
+
13
+ export { storeHasFamily, storeHasOptimisticFamily, storeIsShallow } from "./next/store.js";
14
+
15
+ import { reconcileNextState } from "./next/reconcile.js";
16
+
17
+ import { createStoreDerivedNext } from "./next/projection.js";
18
+
19
+ export { createProjectionNext as createProjection } from "./next/projection.js";
20
+
21
+ function createStore(e, t, r) {
22
+ if (typeof e === "function") return createStoreDerivedNext(e, t, r);
23
+ return createStoreNext(e, !!t?.shallow);
24
+ }
25
+
26
+ function reconcile(e, t = "id") {
27
+ return r => reconcileNextState(e, r, t);
28
+ }
29
+
30
+ function snapshot(e) {
31
+ return snapshotNext(e);
32
+ }
33
+
34
+ function deep(e) {
35
+ return deepNext(e);
36
+ }
37
+
38
+ export { createStore, deep, reconcile, snapshot };
@@ -0,0 +1,651 @@
1
+ import { CONFIG_AUTO_DISPOSE, STATUS_PENDING, STATUS_UNINITIALIZED, NOT_PENDING, CONFIG_OPTIMISTIC, CONFIG_HELD_TRUTH, unwrapOverride } from "../../core/constants.js";
2
+
3
+ import { computed, setSignal, isEqual } from "../../core/core.js";
4
+
5
+ import { getOwner } from "../../core/owner.js";
6
+
7
+ import { activeTransition, GlobalQueue, currentTransition, globalQueue, createTransition, runAsTransitionBatch } from "../../core/scheduler.js";
8
+
9
+ import "../../core/invariants.js";
10
+
11
+ import "../../core/verdict.js";
12
+
13
+ import "../../core/effect.js";
14
+
15
+ import { installOptimisticEngine } from "../../core/optimistic.js";
16
+
17
+ import { $TARGET, markRawIngest, setNextOptimisticViewResolver, isWrappable, rawValuesUsed, isRawValue } from "../store.js";
18
+
19
+ import { runProjectionComputedNext } from "./projection.js";
20
+
21
+ import { wrapNext, runAuthoritative, storeSetterNext, hasActiveOverride, unwrapValue, targetsEqual, heldMaskView, getNode, getHasNode, getKeySetNode, bumpDeep, stagedTruthPB, authoritativeRead } from "./store.js";
22
+
23
+ import { sameKey } from "./reconcile.js";
24
+
25
+ import { setOptHooks, $OWNER, lookupTarget } from "./target.js";
26
+
27
+ /**
28
+ * Store rewrite — optimistic stores (§3/§7, RUL-3): no store-side layer, no
29
+ * backup snapshots. Nodes in an optimistic family are ARMED core signals
30
+ * (`_overrideValue` slot), so every user write rides the engine's
31
+ * optimisticWrite — per-transaction ownership, entanglement, reverts, and
32
+ * flash-at-flush are inherited, not reimplemented. Membership edits live on
33
+ * armed presence nodes (the §6 overlay), so structural optimism reverts with
34
+ * the same per-transaction granularity (FINDING-2's fix by construction).
35
+ *
36
+ * Derived form = an optimistic projection. Landings follow the fold rule
37
+ * (#3164, RUL-2 as re-ruled): while a transaction retains optimistic edits
38
+ * on the family, truth that lands STAGES into that transaction — a keyed
39
+ * identity-preserving walk written through the ordinary staged setter
40
+ * channel — and reveals atomically at settle, exactly like a signal landing
41
+ * under an active override (asyncWrite's held branch). Optimistic edits are
42
+ * never consumed by landings; they live exactly as long as their transaction
43
+ * and die by engine-native revert. With no retainer, landings commit
44
+ * immediately under projectionWriteActive (authoritative, silently beneath
45
+ * any bare-write overrides — those ride the flight's own transition, #2951).
46
+ * Authoritative readers (until()'s predicate) tunnel into staged truth via
47
+ * the node read path's pending-value arm. The transitionBlocked store-half
48
+ * (#2951) is installed here for next-shaped targets, chaining the
49
+ * legacy/engine checks.
50
+ */
51
+ /** #3164 fold: a stamped truth is HELD (masked from ordinary readers until
52
+ * the reveal) only while its transition is live AND retaining optimism —
53
+ * overrides are what make partial-coverage composition a tear. A plain
54
+ * async transition carries no overrides, so downstream computes must see
55
+ * staged values to converge (normal speculation). Resolves merges first:
56
+ * merge unions optimistic nodes/stores into the target. */ function transitionHoldsOptimism(e) {
57
+ const t = currentTransition(e);
58
+ return t.Ut !== true && (t.en.length !== 0 || t.nn.size !== 0);
59
+ }
60
+
61
+ let blockedInstalled = false;
62
+
63
+ function installNextBlockedHalf() {
64
+ if (blockedInstalled) return;
65
+ blockedInstalled = true;
66
+ // Late-bind the optimistic machinery into the plain store/reconcile paths
67
+ // (all call sites are fam?.opt-gated, so this always runs first) and the
68
+ // affects witness's view resolver.
69
+ setOptHooks({
70
+ notifyOptimisticWrites: notifyOptimisticWrites,
71
+ optimisticView: optimisticView,
72
+ applyTentative: applyTentative,
73
+ retainsOptimism: transitionHoldsOptimism
74
+ });
75
+ setNextOptimisticViewResolver((e, t) => optimisticView(e, t));
76
+ // Scheduler flush tails call _clearOptimisticStores whenever tracked
77
+ // stores exist; next has no layer to clear — reverts are engine-native —
78
+ // so the hook only empties the batch set.
79
+ if (!GlobalQueue.ri) {
80
+ GlobalQueue.ri = e => {
81
+ for (const t of e) {
82
+ const e = t?.[$TARGET];
83
+ const n = e?.fam?.overlaid;
84
+ if (n !== undefined) {
85
+ for (const e of n) {
86
+ // Keyset resync (classic channel twin): the keyset node's own
87
+ // revert can compare EQUAL (a landing's bump matched the
88
+ // tentative bump) while the arrangement underneath changed —
89
+ // mapArray/ownKeys subscribers must re-read the post-revert
90
+ // view. Authoritative bump: never re-arm the node we are
91
+ // clearing.
92
+ if (e.k !== null) runAuthoritative(() => setSignal(e.k, e => e + 1));
93
+ }
94
+ }
95
+ }
96
+ e.clear();
97
+ };
98
+ }
99
+ const e = GlobalQueue.Pn;
100
+ GlobalQueue.Pn = t => {
101
+ for (const e of t.nn) {
102
+ const n = e?.[$TARGET];
103
+ const i = n?.fam;
104
+ const o = i?.node;
105
+ // The hold exists to keep optimistic state alive until the store's own
106
+ // truth lands (#2951). Once the family carries NO live overrides (a
107
+ // landing consumed them, or they never existed), a pending firewall is
108
+ // no reason to park the transaction — blocking then leaks it forever
109
+ // when the in-flight question is never answered (undisposed fixtures).
110
+ if (o == null || !(o.S & STATUS_PENDING)) continue;
111
+ // Ownership is declared (#3146): only the flight's OWN transaction
112
+ // parks on the flight (the #2951 anchor routed the bare write there).
113
+ // A transaction that merely brushed the store never waits for truth
114
+ // it does not carry.
115
+ const r = i.ft != null ? liveTransition(i.ft) : null;
116
+ if (r !== null && r !== currentTransition(t)) continue;
117
+ if (familyHasLiveOverrides(i)) return true;
118
+ }
119
+ return e(t);
120
+ };
121
+ }
122
+
123
+ function familyHasLiveOverrides(e) {
124
+ const t = e.overlaid;
125
+ if (t === undefined || t.size === 0) return false;
126
+ for (const e of t) {
127
+ for (const t of [ e.n, e.h ]) {
128
+ if (t === null) continue;
129
+ for (const e of Reflect.ownKeys(t)) {
130
+ const n = t[e];
131
+ if (n.o?.De !== undefined && n.o?.De !== NOT_PENDING) return true;
132
+ }
133
+ }
134
+ if (e.k !== null && e.k.o?.De !== undefined && e.k.o?.De !== NOT_PENDING) return true;
135
+ }
136
+ t.clear();
137
+ // nothing live — drop the bookkeeping
138
+ return false;
139
+ }
140
+
141
+ function createOptimisticStoreNext(e, t, n) {
142
+ // Engine first (armed nodes need optimisticWrite installed before any
143
+ // node exists), then the next-shape hooks.
144
+ installOptimisticEngine();
145
+ installNextBlockedHalf();
146
+ const i = typeof e === "function";
147
+ const o = i ? n : t;
148
+ const r = i ? t : e;
149
+ const s = {
150
+ map: new WeakMap,
151
+ node: null,
152
+ shallow: !!o?.shallow,
153
+ opt: true
154
+ };
155
+ const l = wrapNext(r, null, null, s);
156
+ s.px = l;
157
+ // Same key resolution the projection channels use ("id" default) — replay's
158
+ // satisfaction rule reads it off the family.
159
+ const u = o?.key === undefined ? "id" : o.key;
160
+ s.key = typeof u === "function" ? u : u === null ? null : e => isWrappable(e) ? e[u] : undefined;
161
+ if (s.shallow) {
162
+ l[$TARGET].s = true;
163
+ markRawIngest(r);
164
+ }
165
+ if (i) {
166
+ const t = e;
167
+ // #3146: an async settle event belongs to the flight's OWN transaction.
168
+ // A live declared one re-enters (a merge if the generic settle path
169
+ // already entered a graph-stamped stranger — the landing supersedes any
170
+ // recompute deriving from that stranger's world); a dead one renews (per
171
+ // A18(1) each arrival reveals on its own schedule, so per-yield
172
+ // transactions die with their commit and the next settle event opens the
173
+ // flight's next one — still declared, never anonymous). An UNDECLARED
174
+ // flight (loading window) keeps the ambient reveal (#2933: the loading
175
+ // rail is transaction-invisible).
176
+ const enterFlightTransition = () => {
177
+ const e = s.ft;
178
+ if (e == null) return;
179
+ let t = liveTransition(e);
180
+ if (t === null) s.ft = t = createTransition();
181
+ s.node.Ee = t;
182
+ globalQueue.initTransition(t);
183
+ };
184
+ // Landing router (#3164 fold ruling): while a transaction retains
185
+ // optimistic edits on this family, truth landings stage INTO it and
186
+ // reveal atomically at settle; with no retainer they commit immediately
187
+ // under the authoritative posture (async commits land outside the
188
+ // computed's sync body, so the posture is re-applied here) — inside the
189
+ // flight-owned transaction (#3146). Sync commits (the derive's body,
190
+ // owner is the firewall itself) reveal with their own recompute's flush.
191
+ const wrapCommit = (e, t) => {
192
+ const n = retainingTransition(s);
193
+ if (n !== null) return void stageLanding(s, n, t);
194
+ if (getOwner() !== s.node) enterFlightTransition();
195
+ runAuthoritative(e);
196
+ };
197
+ // Draft writes (the derive mutating its draft, sync body and post-await
198
+ // continuations alike) are the same truth channel per-operation: bind
199
+ // each op to the retaining transaction so its node writes stage and its
200
+ // backing fold defers (ensurePB stamps foldBatches with the swapped-in
201
+ // batch; the write-override eager-commit branch in notifyWrites yields
202
+ // to any active transaction).
203
+ const aroundDraftWrite = e => {
204
+ const t = retainingTransition(s);
205
+ if (t === null) e(); else runFolded(t, e);
206
+ };
207
+ // Flight declaration (#3146): a recompute that registered a truth-flight
208
+ // OWNS its transaction. The ask's transaction is recorded on the family
209
+ // (created by the flight's own pending throw when none was ambient, the
210
+ // causing write's/refresh's when one was — graph-driven causality) and
211
+ // the firewall is stamped so every settle path resolves the flight's
212
+ // transaction by construction, not by whatever last brushed the node.
213
+ // When the ask took none (a dead stale stamp — the previous flight's,
214
+ // cleared nowhere — bare-returns the pre-throw entry), the flight opens
215
+ // its own here, same activation point as the pre-throw's creation: the
216
+ // ambient batch (the causing write, same-tick bare optimism) adopts into
217
+ // it exactly as it would have there, and the pending notification that
218
+ // follows this unwind registers observers against it. The flight
219
+ // also registers as its own async reporter: the transaction lives
220
+ // exactly as long as the question is unanswered, observed or not — the
221
+ // #2951 refetch-hold no longer depends on a tracked observer having
222
+ // happened to register one. Loading-window flights declare nothing
223
+ // (#2933: the loading rail is transaction-invisible); a sync run clears
224
+ // the declaration.
225
+ const declareFlight = e => {
226
+ if (e.o?.Se == null) {
227
+ if (!e.de) s.ft = null;
228
+ return;
229
+ }
230
+ // First flight (#3146 carve-out): nothing has ever committed, so there
231
+ // is no truth to keep on screen and no optimistic state to protect. An
232
+ // uninitialized ask suspends its readers into their Loading boundary
233
+ // exactly like a plain derived store's first flight — declaring a
234
+ // transaction here instead held the ROOT MOUNT (render()'s scheduled
235
+ // insert rides transitions) until the fetch landed, so the boundary's
236
+ // fallback never showed and the whole page stayed blank. The loading
237
+ // window (#2933) already declares nothing for the same reason; once
238
+ // the first truth lands, every refetch flight declares as before.
239
+ if (e.de || e.S & STATUS_UNINITIALIZED) return;
240
+ let t = activeTransition;
241
+ if (t === null) globalQueue.initTransition(t = createTransition());
242
+ s.ft = t;
243
+ e.Ee = t;
244
+ let n = t.Ie.get(e);
245
+ if (n === undefined) t.Ie.set(e, n = new Set);
246
+ n.add(e);
247
+ };
248
+ let n;
249
+ if (o?.seedLoadingValue) n = {
250
+ loadingValue: undefined
251
+ };
252
+ if (o?.name) n = {
253
+ ...n,
254
+ name: o.name
255
+ };
256
+ const i = computed(() => {
257
+ const e = getOwner();
258
+ try {
259
+ runAuthoritative(() => runProjectionComputedNext(l, t, o?.key === undefined ? "id" : o.key, wrapCommit, aroundDraftWrite));
260
+ } finally {
261
+ declareFlight(e);
262
+ }
263
+ }, n);
264
+ i.T &= ~CONFIG_AUTO_DISPOSE;
265
+ s.node = i;
266
+ }
267
+ return [ l, e => {
268
+ // Retention ledger (#3164): record the owning transaction so landings
269
+ // know to fold. Captured at entry — the action machinery has the
270
+ // transaction ambient while user code runs; a bare write with no
271
+ // transaction retains nothing (it rides the flight's own transition
272
+ // per #2951 and dies with it).
273
+ const t = activeTransition;
274
+ storeSetterNext(l, e);
275
+ if (t !== null) (s.rt ??= new Set).add(t);
276
+ } ];
277
+ }
278
+
279
+ /** Resolve a retained transition through its merge chain (`_done` holds the
280
+ * merge target while merged, `true` once settled). Null = dead. */ function liveTransition(e) {
281
+ while (typeof e.Ut === "object") e = e.Ut;
282
+ return e.Ut === true ? null : e;
283
+ }
284
+
285
+ /** The transaction truth landings fold into: the first live member of the
286
+ * family's retention ledger (dead members prune here). Multiple live
287
+ * retainers entangle through their shared family writes and settle
288
+ * together, so folding into the first reaches all of them. */ function retainingTransition(e) {
289
+ const t = e.rt;
290
+ if (t === undefined || t.size === 0) return null;
291
+ let n = null;
292
+ for (const e of t) {
293
+ const i = liveTransition(e);
294
+ if (i === null) t.delete(e); else n ??= i;
295
+ }
296
+ return n;
297
+ }
298
+
299
+ /** Fold a landing into the retaining transaction (#3164): the landed value
300
+ * is written through the ORDINARY staged setter channel — node writes park
301
+ * as `_pendingValue` registered with the transaction's batch (speculation
302
+ * and until()'s authoritative tunnel see them; live view stays coherent),
303
+ * and the backing fold defers via the foldBatches stamp — under the
304
+ * authoritative posture, so armed nodes take the engine bypass and no
305
+ * override is created. The engine's own commit machinery reveals everything
306
+ * atomically when the transaction settles (transitions never abort: failed
307
+ * actions still commit — only optimistic overrides revert). */ function stageLanding(e, t, n) {
308
+ runFolded(t, () => runAuthoritative(() => storeSetterNext(e.px, t => {
309
+ stagedApply(t, unwrapValue(n), e.key ?? null);
310
+ }, false)));
311
+ }
312
+
313
+ /** Run a fold write inside the retaining transaction's batch, then
314
+ * transition-stamp its staged nodes NOW (parity with the parked-transition
315
+ * flush path's reassignPendingTransition): the stamp is what routes stale
316
+ * (render) readers to the committed value — core read's cross-transaction
317
+ * guard — and what makes foldHeld defer the backing for context-free
318
+ * readers. A microtask staging never crosses that flush path, so without
319
+ * the stamp a render effect's speculative recompute would compose staged
320
+ * truth with live overrides — the #3164 tear, one window later. Armed
321
+ * nodes additionally raise CONFIG_HELD_TRUTH: their staged value is
322
+ * confirming truth masked from ordinary readers until the reveal (plain
323
+ * staged nodes stay visible — normal speculation; override-covered nodes
324
+ * stay unarmed — the override is their display and its revert their
325
+ * notification, A17). */ function runFolded(e, t) {
326
+ runAsTransitionBatch(e, t);
327
+ const n = e.tn;
328
+ for (let t = 0; t < n.length; t++) {
329
+ const i = n[t];
330
+ i.Ee = e;
331
+ if (i.T & CONFIG_OPTIMISTIC && !hasActiveOverride(i)) i.T |= CONFIG_HELD_TRUTH;
332
+ }
333
+ }
334
+
335
+ /** Keyed identity-preserving deep merge through live draft proxies — the
336
+ * staged twin of the adoption walk. Reads see the pending backing (staged
337
+ * view), so consecutive landings during one hold compose; key-matched rows
338
+ * keep their raw (and so their proxy) in the slot with only changed leaves
339
+ * written; unmatched rows land wholesale. Runs inside stageLanding's
340
+ * authoritative bracket: drafts seed from committed truth, never overlays. */ function stagedApply(e, t, n) {
341
+ const i = Array.isArray(e);
342
+ if (i && Array.isArray(t)) {
343
+ const i = t.length;
344
+ if (n !== null) {
345
+ // Occurrence-aware key queues (parity with the adoption window):
346
+ // duplicate keys match per occurrence, each current row consumed once.
347
+ let o = null;
348
+ const r = e.length;
349
+ for (let t = 0; t < r; t++) {
350
+ const i = unwrapValue(e[t]);
351
+ if (!isWrappable(i)) continue;
352
+ const r = n(i);
353
+ if (r === undefined) continue;
354
+ const s = (o ??= new Map).get(r);
355
+ if (s === undefined) o.set(r, [ i ]); else s.push(i);
356
+ }
357
+ // Echo adoption: an optimistic structural add whose key the landing
358
+ // confirms must keep its raw (and so its proxy — list drivers keep the
359
+ // DOM row). Tentative rows never reach committed truth (they live in
360
+ // node overrides), so key-match the draft target's active override
361
+ // rows as a secondary pool. Committed rows queued first own their
362
+ // keys; overlay rows only extend coverage. Adopted raws enter staged
363
+ // truth; at settle the override reverts and the reveal re-seats the
364
+ // same raw.
365
+ const s = e[$TARGET]?.n;
366
+ if (s != null) {
367
+ for (const e of Reflect.ownKeys(s)) {
368
+ const t = s[e];
369
+ if (!hasActiveOverride(t)) continue;
370
+ const i = unwrapValue(unwrapOverride(t.o.De));
371
+ if (!isWrappable(i)) continue;
372
+ const r = n(i);
373
+ if (r === undefined) continue;
374
+ const l = (o ??= new Map).get(r);
375
+ if (l === undefined) o.set(r, [ i ]); else l.push(i);
376
+ }
377
+ }
378
+ for (let r = 0; r < i; r++) {
379
+ const i = t[r];
380
+ let s;
381
+ if (isWrappable(i) && o !== null) {
382
+ const e = n(i);
383
+ if (e !== undefined) {
384
+ for (const [t, n] of o) {
385
+ if (!sameKey(t, e)) continue;
386
+ s = n.shift();
387
+ if (n.length === 0) o.delete(t);
388
+ break;
389
+ }
390
+ }
391
+ }
392
+ if (s !== undefined) {
393
+ if (unwrapValue(e[r]) !== s) e[r] = s;
394
+ stagedApply(e[r], i, n);
395
+ } else {
396
+ const t = unwrapValue(e[r]);
397
+ if (!isEqual(t, i) && !targetsEqual(t, i)) e[r] = i;
398
+ }
399
+ }
400
+ } else {
401
+ for (let o = 0; o < i; o++) {
402
+ const i = t[o];
403
+ const r = unwrapValue(e[o]);
404
+ if (r === i) continue;
405
+ if (isWrappable(i) && isWrappable(r) && Array.isArray(i) === Array.isArray(r)) stagedApply(e[o], i, n); else if (!isEqual(r, i) && !targetsEqual(r, i)) e[o] = i;
406
+ }
407
+ }
408
+ if (e.length !== i) e.length = i;
409
+ return;
410
+ }
411
+ // Object merge; also the degenerate root-kind-change shape (arrays accept
412
+ // keyed writes/deletes, so a wholesale restatement still lands staged).
413
+ for (const o of Reflect.ownKeys(t)) {
414
+ if (i && o === "length" || o === $OWNER) continue;
415
+ const r = t[o];
416
+ const s = unwrapValue(e[o]);
417
+ if (s === r) continue;
418
+ if (isWrappable(r) && isWrappable(s) && Array.isArray(r) === Array.isArray(s)) {
419
+ // Different-keyed entities never merge (tentative-channel parity):
420
+ // the incoming object replaces the slot wholesale.
421
+ if (n !== null) {
422
+ const t = n(s);
423
+ const i = n(r);
424
+ if (t !== undefined && i !== undefined && !sameKey(t, i)) {
425
+ e[o] = r;
426
+ continue;
427
+ }
428
+ }
429
+ stagedApply(e[o], r, n);
430
+ } else if (!isEqual(s, r) && !targetsEqual(s, r)) {
431
+ e[o] = r;
432
+ }
433
+ }
434
+ for (const n of Reflect.ownKeys(e)) {
435
+ if (i && n === "length" || n === $OWNER || n in t) continue;
436
+ delete e[n];
437
+ }
438
+ }
439
+
440
+ // ---- optimistic-only store machinery (moved from next/store.ts /
441
+ // next/reconcile.ts so plain-store bundles tree-shake it) ----
442
+ /** Diff the draft against the current OPTIMISTIC VIEW (committed + active
443
+ * overrides — the same view the draft was seeded from) and emit engine writes
444
+ * for exactly the changed keys. Visible-view diffing keeps no-op writes from
445
+ * entangling lanes (RUL-10 / opt R38). */ function notifyOptimisticWrites(e, t) {
446
+ // A bare write while the store's own truth is in flight rides the FLIGHT'S
447
+ // OWN transaction (#2951 via the #3146 declaration): entangle it so the
448
+ // override survives until the refetch settles instead of flash-reverting
449
+ // at plain flush end. The blocked-check store-half keeps that transaction
450
+ // from settling while the firewall is pending. Declared ownership replaces
451
+ // the old circumstantial route through the firewall's `_transition` stamp,
452
+ // which was whatever last brushed the node.
453
+ const n = e.fam?.ft;
454
+ if (n != null) {
455
+ const e = liveTransition(n);
456
+ if (e !== null) globalQueue.initTransition(e);
457
+ }
458
+ // The write is judged against what ordinary readers SEE, not against the
459
+ // committed backing slot: under an adoption hold (#3074) `t.v` is already
460
+ // the truth a live transaction is holding, and an optimistic write equal
461
+ // to it compared as a no-op — no override, no lane, and the screen kept
462
+ // the pre-hold value until the transaction committed (#3330's store
463
+ // twin: `s.v = 1` after a `yield` while the derive had already staged
464
+ // `v: 1`). Signal parity: the override reveals on its lane now, with its
465
+ // derivations; the held truth reveals on the transaction's schedule.
466
+ const i = heldMaskView(e) ?? e.v;
467
+ // Compare RAWS on both sides (`nv` below is unwrapped already). A chained
468
+ // target's `old` is the inner store's proxy, whose reads hand back inner
469
+ // child PROXIES; the draft's clone holds the inner raws. Comparing the two
470
+ // as-is marked every untouched row changed, and the overlay then served
471
+ // each from an override as a fresh non-chained target — row identities
472
+ // churned for the life of the action and snapped back at settle (#3323).
473
+ const visible = (t, n) => {
474
+ const i = e.n?.[t];
475
+ return unwrapValue(i !== undefined && hasActiveOverride(i) ? unwrapOverride(i.o?.De) : n);
476
+ };
477
+ const visiblePresent = t => {
478
+ const n = e.h?.[t];
479
+ return n !== undefined && hasActiveOverride(n) ? !!unwrapOverride(n.o?.De) : t in i;
480
+ };
481
+ let o = false;
482
+ const r = Array.isArray(t);
483
+ for (const n of Reflect.ownKeys(t)) {
484
+ if (r && n === "length" || n === $OWNER) continue;
485
+ const s = unwrapValue(t[n]);
486
+ if (!visiblePresent(n)) {
487
+ // Optimistic add: value node + presence node + membership bump.
488
+ setSignal(getNode(e, n, i[n]), () => s);
489
+ setSignal(getHasNode(e, n, n in i), true);
490
+ o = true;
491
+ } else {
492
+ const t = visible(n, i[n]);
493
+ if (!isEqual(t, s) && !targetsEqual(t, s)) {
494
+ setSignal(getNode(e, n, t), () => s);
495
+ if (r) o = true;
496
+ }
497
+ }
498
+ }
499
+ for (const n of Reflect.ownKeys(i)) {
500
+ if (r && n === "length" || n === $OWNER) continue;
501
+ if (n in t || !visiblePresent(n)) continue;
502
+ // Optimistic delete: node reads undefined, presence flips, membership bumps.
503
+ setSignal(getNode(e, n, i[n]), () => undefined);
504
+ setSignal(getHasNode(e, n, true), false);
505
+ o = true;
506
+ }
507
+ if (r) {
508
+ const n = visible("length", i.length);
509
+ if (n !== t.length) {
510
+ setSignal(getNode(e, "length", n), () => t.length);
511
+ o = true;
512
+ }
513
+ }
514
+ if (o) setSignal(getKeySetNode(e), e => e + 1);
515
+ // Deep-witness: optimistic value writes notify deep() subscribers too
516
+ // (structural ones already ride the key-set bump above).
517
+ bumpDeep(e);
518
+ // Discard the draft — committed raw is untouched (revert target by
519
+ // construction) — restoring any truth-staged backing this draft displaced
520
+ // (#3164 fold: ensurePB parked it so tentative writes could not pollute
521
+ // staged truth). Register the root store for the scheduler's settle hooks.
522
+ e.pb = stagedTruthPB.get(e) ?? null;
523
+ if (e.pb !== null) stagedTruthPB.delete(e);
524
+ (e.fam.overlaid ??= new Set).add(e);
525
+ GlobalQueue.Hn?.(e.fam.px ?? e.px);
526
+ }
527
+
528
+ /** Optimistic-view composition for snapshot/deep (O1: snapshot is the CURRENT
529
+ * view, lane values included; a fresh copy per call during pending windows —
530
+ * RUL-12). Returns `src` untouched when no override is active on `t`.
531
+ * Authoritative-view reads (until()'s predicate) skip composition entirely:
532
+ * the predicate observes authoritative truth, never the caller's tentative
533
+ * overlay. (Write-side emission callers never run under such a compute.) */ function optimisticView(e, t) {
534
+ if (e.fam?.opt !== true || authoritativeRead()) return t;
535
+ let n = null;
536
+ const ensure = () => n ??= Array.isArray(t) ? [ ...t ] : {
537
+ ...t
538
+ };
539
+ const i = e.n;
540
+ if (i !== null) {
541
+ for (const e of Reflect.ownKeys(i)) {
542
+ const n = i[e];
543
+ if (!hasActiveOverride(n)) continue;
544
+ const o = unwrapOverride(n.o?.De);
545
+ if (e === "length" && Array.isArray(t)) {
546
+ if (t.length !== o) ensure().length = o;
547
+ } else if (!isEqual(t[e], o)) ensure()[e] = o;
548
+ }
549
+ }
550
+ const o = e.h;
551
+ if (o !== null) {
552
+ for (const e of Reflect.ownKeys(o)) {
553
+ const i = o[e];
554
+ if (!hasActiveOverride(i)) continue;
555
+ const r = !!unwrapOverride(i.o?.De);
556
+ if (!r && e in (n ?? t)) delete ensure()[e];
557
+ }
558
+ }
559
+ return n ?? t;
560
+ }
561
+
562
+ function applyTentative(e, t, n) {
563
+ const i = e.pb ?? e.v;
564
+ const o = optimisticView(e, i);
565
+ const r = e.fam;
566
+ const s = Array.isArray(t);
567
+ if (Array.isArray(o) !== s) return;
568
+ // kind change at root: flat overrides below
569
+ const l = [];
570
+ let u;
571
+ if (s) u = [ ...t ]; else {
572
+ u = {};
573
+ for (const e of Reflect.ownKeys(t)) if (e !== $OWNER) u[e] = t[e];
574
+ }
575
+ const match = (e, t) => {
576
+ if (!isWrappable(e) || !isWrappable(t)) return null;
577
+ if (rawValuesUsed && (isRawValue(e) || isRawValue(t))) return null;
578
+ if (Array.isArray(e) !== Array.isArray(t)) return null;
579
+ if (n) {
580
+ const i = n(e);
581
+ const o = n(t);
582
+ // SameValueZero (re-audit 3, P1-3): parity with the plain reconcile
583
+ // channel — NaN keys are self-equal.
584
+ if (i !== undefined && o !== undefined && !sameKey(i, o)) return null;
585
+ }
586
+ return lookupTarget(unwrapValue(e), r) ?? null;
587
+ };
588
+ if (s) {
589
+ const e = o;
590
+ let i = null;
591
+ for (let o = 0; o < t.length; o++) {
592
+ const r = t[o];
593
+ if (!isWrappable(r)) continue;
594
+ let s;
595
+ if (n) {
596
+ const t = n(r);
597
+ if (t !== undefined) {
598
+ if (i === null) {
599
+ // Occurrence-aware index queues (re-audit 3, P1-3): parity with
600
+ // the plain adoption window — duplicate keys match per
601
+ // occurrence, each view row consumed once.
602
+ i = new Map;
603
+ for (let t = 0; t < e.length; t++) {
604
+ const o = unwrapValue(e[t]);
605
+ if (isWrappable(o)) {
606
+ const e = n(o);
607
+ if (e === undefined) continue;
608
+ const r = i.get(e);
609
+ if (r === undefined) i.set(e, t); else if (Array.isArray(r)) r.push(t); else i.set(e, [ r, t ]);
610
+ }
611
+ }
612
+ }
613
+ const o = i.get(t);
614
+ if (o === undefined) s = undefined; else if (Array.isArray(o)) {
615
+ s = unwrapValue(e[o.shift()]);
616
+ if (o.length === 1) i.set(t, o[0]);
617
+ } else {
618
+ s = unwrapValue(e[o]);
619
+ i.delete(t);
620
+ }
621
+ } else s = unwrapValue(e[o]);
622
+ } else s = unwrapValue(e[o]);
623
+ const a = match(s, r);
624
+ if (a !== null) {
625
+ // Keep the existing row in the slot (identity preserved); recurse.
626
+ u[o] = unwrapValue(s);
627
+ l.push([ a, r ]);
628
+ }
629
+ }
630
+ } else {
631
+ for (const e of Reflect.ownKeys(t)) {
632
+ if (e === $OWNER) continue;
633
+ const n = unwrapValue(o[e]);
634
+ const i = t[e];
635
+ const r = match(n, i);
636
+ if (r !== null) {
637
+ u[e] = n;
638
+ l.push([ r, i ]);
639
+ }
640
+ }
641
+ }
642
+ // Flat overrides for this level (adds, removals, moved slots, length, leaf
643
+ // values) — preserve any live user draft backing across the call.
644
+ const a = e.pb;
645
+ e.pb = null;
646
+ notifyOptimisticWrites(e, u);
647
+ e.pb = a;
648
+ for (let e = 0; e < l.length; e++) applyTentative(l[e][0], unwrapValue(l[e][1]), n);
649
+ }
650
+
651
+ export { createOptimisticStoreNext, notifyOptimisticWrites, optimisticView, transitionHoldsOptimism };