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

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