@solidjs/signals 2.0.0-rc.0 → 2.0.0-rc.10

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