@solidjs/signals 2.0.0-rc.11 → 2.0.0-rc.14

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 (118) hide show
  1. package/dist/dev-shared.js +2056 -3773
  2. package/dist/dev.attribution.js +159 -91
  3. package/dist/dev.js +7263 -8384
  4. package/dist/observe/affects.js +125 -104
  5. package/dist/observe/boundaries.js +458 -665
  6. package/dist/observe/core/action.js +63 -66
  7. package/dist/observe/core/async.js +240 -383
  8. package/dist/observe/core/attribution-queries.js +1 -1
  9. package/dist/observe/core/attribution.js +271 -199
  10. package/dist/observe/core/constants.js +115 -178
  11. package/dist/observe/core/context.js +3 -3
  12. package/dist/observe/core/core.js +840 -1428
  13. package/dist/observe/core/dev.js +41 -18
  14. package/dist/observe/core/effect.js +75 -86
  15. package/dist/observe/core/external.js +5 -5
  16. package/dist/observe/core/graph.js +61 -70
  17. package/dist/observe/core/heap.js +75 -98
  18. package/dist/observe/core/invariants.js +1 -34
  19. package/dist/observe/core/lanes.js +704 -181
  20. package/dist/observe/core/owner.js +80 -101
  21. package/dist/observe/core/scheduler.js +708 -1351
  22. package/dist/observe/core/verdict.js +302 -621
  23. package/dist/observe/flatten.js +79 -0
  24. package/dist/observe/index.js +15 -13
  25. package/dist/observe/map.js +232 -235
  26. package/dist/observe/reveal.js +212 -0
  27. package/dist/observe/signals.js +133 -215
  28. package/dist/observe/store/affects.js +135 -0
  29. package/dist/observe/store/index.js +29 -24
  30. package/dist/observe/store/optimistic.js +538 -0
  31. package/dist/observe/store/projection.js +302 -0
  32. package/dist/observe/store/reconcile.js +257 -0
  33. package/dist/observe/store/store.js +1945 -277
  34. package/dist/observe/store/storePath.js +1 -1
  35. package/dist/{prod/store/next → observe/store}/target.js +17 -18
  36. package/dist/observe/store/types.js +73 -0
  37. package/dist/observe/store/utils.js +1 -13
  38. package/dist/prod/affects.js +127 -106
  39. package/dist/prod/boundaries.js +442 -654
  40. package/dist/prod/core/action.js +71 -74
  41. package/dist/prod/core/async.js +307 -442
  42. package/dist/prod/core/constants.js +112 -177
  43. package/dist/prod/core/context.js +3 -3
  44. package/dist/prod/core/core.js +852 -1448
  45. package/dist/prod/core/effect.js +88 -99
  46. package/dist/prod/core/external.js +5 -5
  47. package/dist/prod/core/graph.js +61 -70
  48. package/dist/prod/core/heap.js +75 -98
  49. package/dist/prod/core/invariants.js +1 -32
  50. package/dist/prod/core/lanes.js +666 -181
  51. package/dist/prod/core/owner.js +75 -96
  52. package/dist/prod/core/scheduler.js +677 -1332
  53. package/dist/prod/core/verdict.js +297 -621
  54. package/dist/prod/flatten.js +77 -0
  55. package/dist/prod/index.js +15 -13
  56. package/dist/prod/map.js +143 -146
  57. package/dist/prod/reveal.js +212 -0
  58. package/dist/prod/signals.js +144 -226
  59. package/dist/prod/store/affects.js +135 -0
  60. package/dist/prod/store/index.js +28 -19
  61. package/dist/prod/store/optimistic.js +533 -0
  62. package/dist/prod/store/projection.js +295 -0
  63. package/dist/prod/store/reconcile.js +257 -0
  64. package/dist/prod/store/store.js +1840 -272
  65. package/dist/prod/store/storePath.js +1 -1
  66. package/dist/{observe/store/next → prod/store}/target.js +13 -17
  67. package/dist/prod/store/types.js +73 -0
  68. package/dist/prod/store/utils.js +1 -11
  69. package/dist/types/affects.d.ts +11 -30
  70. package/dist/types/boundaries.d.ts +62 -176
  71. package/dist/types/carved.d.ts +1 -0
  72. package/dist/types/core/action.d.ts +5 -6
  73. package/dist/types/core/async.d.ts +10 -5
  74. package/dist/types/core/attribution-hooks.d.ts +48 -1
  75. package/dist/types/core/attribution.d.ts +29 -9
  76. package/dist/types/core/constants.d.ts +125 -171
  77. package/dist/types/core/core.d.ts +104 -264
  78. package/dist/types/core/dev.d.ts +60 -7
  79. package/dist/types/core/effect.d.ts +0 -7
  80. package/dist/types/core/graph.d.ts +7 -1
  81. package/dist/types/core/heap.d.ts +5 -7
  82. package/dist/types/core/index.d.ts +6 -6
  83. package/dist/types/core/invariants.d.ts +0 -29
  84. package/dist/types/core/lanes.d.ts +65 -87
  85. package/dist/types/core/owner.d.ts +3 -3
  86. package/dist/types/core/scheduler.d.ts +249 -331
  87. package/dist/types/core/types.d.ts +51 -85
  88. package/dist/types/core/verdict.d.ts +3 -0
  89. package/dist/types/flatten.d.ts +8 -0
  90. package/dist/types/index.d.ts +7 -5
  91. package/dist/types/reveal.d.ts +19 -0
  92. package/dist/types/signals.d.ts +53 -92
  93. package/dist/types/store/affects.d.ts +1 -0
  94. package/dist/types/store/index.d.ts +35 -15
  95. package/dist/types/store/optimistic.d.ts +23 -0
  96. package/dist/types/store/projection.d.ts +18 -0
  97. package/dist/types/store/reconcile.d.ts +3 -0
  98. package/dist/types/store/store.d.ts +165 -143
  99. package/dist/types/store/storePath.d.ts +1 -1
  100. package/dist/types/store/target.d.ts +151 -0
  101. package/dist/types/store/types.d.ts +84 -0
  102. package/package.json +1 -1
  103. package/dist/observe/core/optimistic.js +0 -669
  104. package/dist/observe/store/next/optimistic.js +0 -717
  105. package/dist/observe/store/next/projection.js +0 -266
  106. package/dist/observe/store/next/reconcile.js +0 -375
  107. package/dist/observe/store/next/store.js +0 -2480
  108. package/dist/prod/core/optimistic.js +0 -658
  109. package/dist/prod/store/next/optimistic.js +0 -710
  110. package/dist/prod/store/next/projection.js +0 -257
  111. package/dist/prod/store/next/reconcile.js +0 -373
  112. package/dist/prod/store/next/store.js +0 -2356
  113. package/dist/types/core/optimistic.d.ts +0 -6
  114. package/dist/types/store/next/optimistic.d.ts +0 -25
  115. package/dist/types/store/next/projection.d.ts +0 -27
  116. package/dist/types/store/next/reconcile.d.ts +0 -10
  117. package/dist/types/store/next/store.d.ts +0 -143
  118. package/dist/types/store/next/target.d.ts +0 -196
@@ -1,2356 +0,0 @@
1
- import { unwrapOverride, $REFRESH, CONFIG_CHILDREN_FORBIDDEN, CONFIG_OWNED_WRITE, NOT_PENDING, CONFIG_OPTIMISTIC, STATUS_UNINITIALIZED, STATUS_ERROR, REACTIVE_RECOMPUTING_DEPS, CONFIG_OVERRIDE_SUPERSEDED, CONFIG_AUTHORITATIVE_READ } from "../../core/constants.js";
2
-
3
- import { setSlotUnobserved, setSignal, isEqual, rederiveHeld, suppressComputedRecompute, read, hasActiveOverride, pendingCheckActive, latestReadActive, readNodeFast, READ_SLOW, signal, unlinkFirewallChild, ext, visibleOverride, ownsHold, serve, setLatestReadActive, prepareComputed, slotSignal, stale, recordStaleReplay, enterStagedRead, context, currentOptimisticLane, heldDerivation } from "../../core/core.js";
4
-
5
- import { projectionWriteActive, setProjectionWriteActive, activeTransition, schedule, currentTransition, deferSlotRelease, setStoreCommitHook } from "../../core/scheduler.js";
6
-
7
- import "../../core/invariants.js";
8
-
9
- import { getObserver, getOwner } from "../../core/owner.js";
10
-
11
- import { $TARGET, isWrappable, markRawIngest, $PROXY, getWriteOverride, markRawOne, $RECORD, witnessAffectsMark, $TRACK, affectsScopesLive, inheritAffectsMarks, $AFFECTS, rawValuesUsed, isRawValue, setNextAffectsNodeResolver } from "../store.js";
12
-
13
- import { lookupTarget, optHooks, storeNextLookup, $OWNER, isOwned, markDescendants, devAssertNeverUserMutation } from "./target.js";
14
-
15
- /**
16
- * Store rewrite — increment 2: plain deep stores with pending-backing writes.
17
- * Contract: INTERNALS-STORE-STATE.md.
18
- *
19
- * Write model (RUL-1, unified): the first draft write to a target creates its
20
- * pending backing `pb` — a descriptor-preserving CoW clone. The draft mutates
21
- * `pb` natively (array methods, defineProperty, deletes all just work).
22
- * Reads: drafts and owner-context reads see `pb`; context-free reads see the
23
- * committed `b` until flush. At flush commit (core's storeCommitHook), each
24
- * written target folds: diff old `b` vs new backing notifies exactly the
25
- * changed keys through equality-gated nodes, then `b` becomes the new
26
- * backing. Setter return-value replacement parks the UNOWNED incoming object
27
- * in `pb` — adoption: fold swaps it in, ownership resets (2026-08-16c).
28
- *
29
- * Nodes carry no pending state — they are pure subscription points; `pb` is
30
- * the pending home. Laziness: a written target with no subscriptions folds as
31
- * a pointer swap with zero node work.
32
- */
33
- // ---------------------------------------------------------------------------
34
- // wrap / dedupe
35
- /** Pre-shaped constructor for OBJECT proxy targets: V8 tips a bare `{}` into
36
- * dictionary mode once ~19 named properties are assigned onto it (the #3044
37
- * `ovl`/`del` fields crossed that line — every trap's field read became a
38
- * hash lookup, a 15% deep-dbmon tick regression). Declaring every field in a
39
- * constructor pre-allocates in-object slots so the map stays fast, with
40
- * headroom for future fields. The prototype is reset to `Object.prototype`
41
- * so proxy-forwarded semantics (getPrototypeOf, constructor) are exactly a
42
- * plain object's. Array targets keep the bare-`[]` path — they must carry
43
- * the array exotic class for `Array.isArray(proxy)`.
44
- *
45
- * ARRAY SHAPE RULE: arrays normalize their named properties to dictionary
46
- * mode as the count grows (V8 13.x: counts ≡ 0 mod 3 from 18 up), so the
47
- * target's named field count is capped at 20 — any future write-side state
48
- * beyond `wk` must ride an extension object (see target.ts), never new
49
- * named fields here. */
50
- function TargetShape() {
51
- this.v = undefined;
52
- this.ch = undefined;
53
- this.pb = undefined;
54
- this.n = undefined;
55
- this.h = undefined;
56
- this.k = undefined;
57
- this.dk = undefined;
58
- this.u = undefined;
59
- this.pk = undefined;
60
- this.px = undefined;
61
- this.d = undefined;
62
- this.a = undefined;
63
- this.sc = undefined;
64
- this.kc = undefined;
65
- this.nc = undefined;
66
- this.ab = undefined;
67
- this.fam = undefined;
68
- this.s = undefined;
69
- this.ovl = undefined;
70
- this.del = undefined;
71
- this.wk = undefined;
72
- this.hv = undefined;
73
- this.ht = undefined;
74
- }
75
-
76
- TargetShape.prototype = Object.prototype;
77
-
78
- function createTarget(e, t, n, r = t?.fam ?? null) {
79
- // The proxy target carries the array exotic class when the value is an
80
- // array, so Array.isArray(proxy) is true; the fields live on it directly.
81
- // Direct field assignment in one fixed order (no Object.assign literal
82
- // copy): every target shares a hidden-class transition chain — createTarget
83
- // was the #2 store cost in the uibench creation profile.
84
- const i = Array.isArray(e) ? [] : new TargetShape;
85
- i.v = e;
86
- // Chained-backing flag (backing IS another store's proxy, §7b) — cached so
87
- // the hot read path never does a per-read symbol lookup on the backing.
88
- i.ch = e[$TARGET] !== undefined;
89
- i.pb = null;
90
- i.n = null;
91
- i.h = null;
92
- i.k = null;
93
- i.dk = null;
94
- i.wk = null;
95
- i.u = t;
96
- i.pk = n;
97
- i.px = null;
98
- i.d = false;
99
- i.a = false;
100
- i.sc = 0;
101
- i.kc = 0;
102
- i.nc = 0;
103
- i.ab = null;
104
- i.fam = r;
105
- i.s = false;
106
- i.ovl = false;
107
- i.del = null;
108
- i.hv = null;
109
- i.ht = null;
110
- i.px = new Proxy(i, traps);
111
- // Legacy interop: shared machinery (affects walks, wrap dedupe) reads the
112
- // proxy off looked-up targets as a field.
113
- i[$PROXY] = i.px;
114
- (r?.map ?? storeNextLookup).set(e, i);
115
- return i;
116
- }
117
-
118
- function wrapNext(e, t = null, n = null, r = t?.fam ?? null) {
119
- // markRaw'd values never wrap through ANY store (R42; sticky raw-marking
120
- // is one half of the never-both-wrapped-and-raw invariant, RUL-12).
121
- if (rawValuesUsed && isRawValue(e)) return e;
122
- const i = lookupTarget(e, r);
123
- if (i !== undefined) return i.px;
124
- const l = e[$TARGET];
125
- if (l !== undefined && l.px === e) {
126
- // Foreign-family proxies re-wrap into THIS family (writes stay isolated);
127
- // same-family and plain-store proxies pass through.
128
- if (r === null || l.fam === r) return e;
129
- return createTarget(e, t, n, r).px;
130
- }
131
- return createTarget(e, t, n, r).px;
132
- }
133
-
134
- /** Unwrap our own proxies to their current backing; leave everything else. */ function unwrapValue(e) {
135
- if (e == null || typeof e !== "object") return e;
136
- const t = e[$TARGET];
137
- if (t !== undefined && t.px === e && t.v !== undefined) {
138
- // A draft escaping into other storage must be a REAL container that
139
- // becomes this target's committed backing at fold (the shared-raw
140
- // contract) — a prototype overlay is neither.
141
- if (t.ovl) materializePB(t);
142
- return t.pb ?? t.v;
143
- }
144
- return e;
145
- }
146
-
147
- // ---------------------------------------------------------------------------
148
- // nodes: pure subscription points (values used only for equality gating)
149
- // Shared slot-node equality (create-floor diet): ONE function for every
150
- // store node — `this` is the node (method-call convention at every _equals
151
- // site), `_host` is the baked-in target backref. Logical-slot equality:
152
- // values resolving to the same child target are the same slot
153
- // (privatization/adoption swap raw identity without changing the logical
154
- // value — only changed leaves notify, R9).
155
- const slotNodeEquals = function(e, t) {
156
- return isEqual(e, t) || sameLogicalSlot(this.Bn, e, t);
157
- };
158
-
159
- // Shared slot-node unobserved handler (create-floor diet): registered once;
160
- // the core sweep dispatches CONFIG_SLOT_NODE nodes here instead of holding a
161
- // per-node closure in a per-node NodeExtension.
162
- setSlotUnobserved(e => {
163
- // A live affects() mark keeps the node addressable (sweep parity).
164
- if (e.o?.t) return;
165
- // An active override or a staged write is state only the node holds (an
166
- // optimistic signal keeps its override whether or not anything reads it —
167
- // store parity, posture-store-parity S7): defer the release to the flush
168
- // that resolves it (the scheduler's transient-node sweep).
169
- if (hasActiveOverride(e) || e._e !== NOT_PENDING) return deferSlotRelease(e);
170
- const t = e.Bn;
171
- const n = e.Kn;
172
- if (t.n && t.n[n] === e) {
173
- delete t.n[n];
174
- t.nc--;
175
- // Projection leaves also leave the firewall child chain (#3351): a
176
- // dropped node is unreachable through the store — a fresh read makes a
177
- // fresh node — so the chain would only retain it and its last value.
178
- unlinkFirewallChild(e);
179
- }
180
- });
181
-
182
- function getNode(e, t, n,
183
- // First-read dedupe (create-floor slice 2): the get trap probes
184
- // accessor-ness right before creating the node — pass the verdict through
185
- // so creation skips the second descriptor scan. -1 = unknown (other
186
- // callers), 0/1 = probed.
187
- r = -1) {
188
- const i = e.n ??= Object.create(null);
189
- let l = i[t];
190
- if (l === undefined) {
191
- // Born holding (#3330 store twin): a key first read while a live
192
- // transaction holds an ADOPTION on this target (adoptPB's `ht`) is
193
- // created as if it had existed when the hold was notified — the adopted
194
- // value already swapped into `v`, so committed is the held view
195
- // `hv[key]` and the adopted `v[key]` is staged under the transaction
196
- // (stageHeldKey). Its held-adoption notification (stageHeldAdoptions)
197
- // ran before the node existed and the drain has nothing left to say.
198
- // Without this the node was born from whichever view its first reader
199
- // saw and never learned the other. Two kinds of hold, one rule (#3336):
200
- // - a held FOLD (pb): a setter's write to an unobserved key landed only
201
- // in the pending backing; committed is `v[key]`, staged is `pb[key]`
202
- // (undefined for a deleted key);
203
- // - a held ADOPTION (ht): the adopted value already swapped into `v`;
204
- // committed is the held view `hv[key]`, staged is `v[key]`.
205
- const o = heldFoldTransition(e);
206
- let f = heldAdoptionTransition(e);
207
- if (f !== null) n = e.hv[t]; else if ((f = o) !== null) n = e.v[t];
208
- // Create-floor diet: slotSignal bakes the whole node into one literal —
209
- // no options object, no equals/unobserved closures, no NodeExtension,
210
- // no post-construction expandos (acc + the wrap cache px/pxv are
211
- // pre-shaped fields: the proxy last served for this key and the raw it
212
- // wrapped — one pointer compare replaces the per-read WeakMap lookup in
213
- // wrapNext). ownedWrite rides the literal's config: the setter carries
214
- // the owned-scope write guard; node-level setSignals are internal
215
- // notification machinery. Projection nodes carry the projection
216
- // computed as their firewall: reads through them link the derive's
217
- // status/lifecycle (§7b).
218
- const s = l = slotSignal(n, slotNodeEquals, e, t,
219
- // Accessor-ness resolved ONCE per node (no per-object descriptor
220
- // scan on reads): accessor keys serve through Reflect.get with the
221
- // proxy receiver.
222
- r === -1 ? isOwnAccessor(e.pb ?? e.v, t) : r === 1, e.fam?.node ?? undefined);
223
- // Optimistic families: arm the override slot — setSignal routes armed
224
- // nodes through the core engine (lanes, ownership, reverts all native).
225
- if (e.fam?.opt) {
226
- ext(s).Fe = NOT_PENDING;
227
- s.C |= CONFIG_OPTIMISTIC;
228
- }
229
- // A node born inside a live mark's identity scope inherits the mark
230
- // (the declaration walk could only cover nodes existing then).
231
- if (t !== $AFFECTS && affectsScopesLive()) inheritAffectsMarks(s, e.v, t);
232
- if (f !== null) stageHeldKey(s, o !== null ? e.del !== null && e.del.has(t) ? undefined : e.pb[t] : e.v[t], f);
233
- i[t] = l;
234
- e.nc++;
235
- markDescendants(e);
236
- }
237
- return l;
238
- }
239
-
240
- /** The live transaction holding an adoption on `target` (adoptPB's `ht`;
241
- * a latest()-pull PLAIN_HOLD is not a transaction), else null. */ function heldAdoptionTransition(e) {
242
- if (e.ht === null || e.ht === PLAIN_HOLD || heldMaskView(e) === null) return null;
243
- const t = currentTransition(e.ht);
244
- return t.Rn === false ? t : null;
245
- }
246
-
247
- /**
248
- * Materialization under a hold. A node created by a first tracked read while
249
- * a transaction holds this target must carry the same state a node that
250
- * existed when the hold was notified carries — a transaction-stamped
251
- * `_pendingValue` that core read() serves by ITS rules (a stale render reader
252
- * of a foreign transaction's write sees committed, an owner-context reader
253
- * inside the transaction sees the write) — or the reader's answer depends on
254
- * whether some OTHER reader had materialized the key before the hold. Stage
255
- * `nv` (the held value for the key — see getNode for which view it comes
256
- * from) as the holding transaction's write — directly, not through
257
- * setSignal: it is not a new write (it staged with the adoption's batch) and
258
- * it walks no subscriber (the node has none yet). Transition-stamped now, as
259
- * `runFolded` does — no parked-flush pass will stamp it.
260
- */
261
- /** The live transaction holding `target`'s pending backing (the #3089
262
- * write-time stamp, resolved through merges), else null. */ function liveFoldTransition(e) {
263
- if (e.pb === null) return null;
264
- const t = foldBatches.get(e);
265
- if (t === undefined) return null;
266
- const n = currentTransition(t);
267
- return n.Rn === false ? n : null;
268
- }
269
-
270
- /** liveFoldTransition for node materialization (stageHeldKey). Inside the
271
- * draft the backing is the setter's working copy, not a flushed hold — its
272
- * nodes take their writes at setter exit (notifyWrites). Optimistic families
273
- * hold at the backing: tentative writes are node overrides over a discarded
274
- * clone, and a truth-staged landing is served through the fold's own hold
275
- * (heldTruthMasked for lane passes, pendingBackingVisible's hold arms for
276
- * the rest) — neither is a plain staged write to mirror. Chained
277
- * backings serve the inner store's live value, never a node value. */ function heldFoldTransition(e) {
278
- if (e.ch || e.fam?.opt === true || inDraft(e)) return null;
279
- return liveFoldTransition(e);
280
- }
281
-
282
- /**
283
- * Rule 1 at the backing, the hold half (core serve()'s stale-of-foreign and
284
- * A29 arms, for a container instead of a node): given the transaction
285
- * holding what a reader `c` (a pass; callers serve context-free readers the
286
- * committed container themselves) is about to be served, is the STAGED
287
- * container its to see? Its own hold, or none — yes. A foreign hold — a
288
- * stale pass (render effect) keeps the committed frame and is recorded for
289
- * replay at the hold's commit (recordStaleReplay, A15 / A26); a deriving
290
- * pass takes the staged world and enters the transaction (enterStagedRead,
291
- * A29). Shared by both hold kinds: a setter's fold (committed `v`, staged
292
- * `pb`, the write-time stamp) and an adoption under a transaction
293
- * (committed = the held view `hv`, staged = the adopted `v`, #3074).
294
- */ function holdVisible(e, t) {
295
- if (e === null || ownsHold(e)) return true;
296
- if (stale) {
297
- recordStaleReplay(e, t);
298
- return false;
299
- }
300
- enterStagedRead(null, e);
301
- return true;
302
- }
303
-
304
- function stageHeldKey(e, t, n) {
305
- if (slotNodeEquals.call(e, e.ce, t)) return;
306
- e._e = t;
307
- e.we = n;
308
- n.Gn.push(e);
309
- }
310
-
311
- function sameLogicalSlot(e, t, n) {
312
- if (t === null || typeof t !== "object" || n === null || typeof n !== "object") return false;
313
- const r = lookupTarget(t, e.fam);
314
- return r !== undefined && r === lookupTarget(n, e.fam);
315
- }
316
-
317
- function getHasNode(e, t, n) {
318
- const r = e.h ??= Object.create(null);
319
- let i = r[t];
320
- if (i === undefined) {
321
- const l = i = signal(n, {
322
- equals: isEqual,
323
- unobserved() {
324
- if (l.o?.t) return;
325
- // The structural twin of the value slot's rule (setSlotUnobserved,
326
- // S7): an optimistic add/delete lives on this presence node as its
327
- // override — releasing it with the override on would let `in`,
328
- // `Object.keys` and descriptors fall back to committed structure
329
- // while the action is live. Defer to the flush that resolves it.
330
- if (hasActiveOverride(l) || l._e !== NOT_PENDING) return deferSlotRelease(l);
331
- if (e.h && e.h[t] === l) {
332
- delete e.h[t];
333
- unlinkFirewallChild(l);
334
- }
335
- }
336
- }, e.fam?.node ?? undefined);
337
- l.C |= CONFIG_OWNED_WRITE;
338
- if (e.fam?.opt) {
339
- ext(l).Fe = NOT_PENDING;
340
- l.C |= CONFIG_OPTIMISTIC;
341
- }
342
- if (affectsScopesLive()) inheritAffectsMarks(l, e.v, t);
343
- r[t] = i;
344
- markDescendants(e);
345
- }
346
- return i;
347
- }
348
-
349
- /** Did `obs` link `target`'s key-set node in its CURRENT pass? The mirror of
350
- * link()'s O(1) repeat-touch check: the node's newest subscriber link is
351
- * this observer's, and — during a recompute — carries this pass's dep
352
- * generation (a link left from a previous pass is stale: the observer may
353
- * not re-read the key set this time). No dep-list scan, no allocation. A
354
- * probe observer (isPending()/latest() sentinel) never links, so it always
355
- * falls through to the presence read. */ function observerHoldsKeySet(e, t) {
356
- const n = e.k;
357
- if (n === null) return false;
358
- const r = n.mn;
359
- return r !== null && r.ge === t && (!(t.oe & REACTIVE_RECOMPUTING_DEPS) || r.Be === t.Ye);
360
- }
361
-
362
- function getKeySetNode(e) {
363
- let t = e.k;
364
- if (t === null) {
365
- const n = t = signal(0, {
366
- equals: false,
367
- unobserved() {
368
- if (e.k === n) {
369
- e.k = null;
370
- unlinkFirewallChild(n);
371
- }
372
- }
373
- }, e.fam?.node ?? undefined);
374
- n.C |= CONFIG_OWNED_WRITE;
375
- if (e.fam?.opt) {
376
- ext(n).Fe = NOT_PENDING;
377
- n.C |= CONFIG_OPTIMISTIC;
378
- }
379
- e.k = t;
380
- markDescendants(e);
381
- }
382
- return t;
383
- }
384
-
385
- function getDeepNode(e) {
386
- let t = e.dk;
387
- if (t === null) {
388
- const n = t = signal(0, {
389
- equals: false,
390
- unobserved() {
391
- if (e.dk === n) {
392
- e.dk = null;
393
- unlinkFirewallChild(n);
394
- }
395
- }
396
- }, e.fam?.node ?? undefined);
397
- n.C |= CONFIG_OWNED_WRITE;
398
- if (e.fam?.opt) {
399
- ext(n).Fe = NOT_PENDING;
400
- n.C |= CONFIG_OPTIMISTIC;
401
- }
402
- if (affectsScopesLive()) inheritAffectsMarks(n, e.v, $TRACK);
403
- e.dk = t;
404
- markDescendants(e);
405
- }
406
- return t;
407
- }
408
-
409
- /** Deep-witness bump: any value/shape change on a record with a live deep()
410
- * subscriber notifies it. One null check when unused. */ function bumpDeep(e) {
411
- if (e.dk !== null) setSignal(e.dk, 1);
412
- }
413
-
414
- // ---------------------------------------------------------------------------
415
- // pending backing + fold (the single mutation point)
416
- /** target → committed backing at batch start (the fold diff's old side). */ const foldOlds = new Map;
417
-
418
- let hookInstalled = false;
419
-
420
- /** Shallow-clone `source` as a backing OWNED by `t` (stamped `$OWNER`, see
421
- * target.ts). Callers always pass their own committed backing as `source`. */ function cloneRaw(e, t) {
422
- // Plain-data fast path (#3360): a scanned `Object.prototype` container
423
- // whose own keys are all enumerable data clones by spread — the same
424
- // result as the descriptor walk below (own enumerable string+symbol keys,
425
- // normalized writable+configurable), at ~1/50th the cost.
426
- t.sc || scanAccessorsOnce(t);
427
- if (t.sc === 2) {
428
- const n = {
429
- ...e
430
- };
431
- n[$OWNER] = t;
432
- return n;
433
- }
434
- // Descriptor-preserving shallow clone (R29: installed getters stay live;
435
- // ruled 2026-08-17: frozen sources clone unfrozen — theirs stays frozen).
436
- // Data descriptors normalize to writable+configurable (the clone is OURS to
437
- // mutate — R51's "source-non-configurable is writable through the store");
438
- // enumerability and accessors are preserved. The scan doubles as the
439
- // accessor-flag detector (free — we're enumerating descriptors anyway).
440
- const n = Object.getOwnPropertyDescriptors(e);
441
- for (const r of Reflect.ownKeys(n)) {
442
- const i = n[r];
443
- if (r === "length" && Array.isArray(e)) continue;
444
- i.configurable = true;
445
- if (!i.get && !i.set) i.writable = true; else t.a = true;
446
- }
447
- const r = Array.isArray(e) ? Object.defineProperties([], n) : Object.create(Object.getPrototypeOf(e), n);
448
- r[$OWNER] = t;
449
- return r;
450
- }
451
-
452
- /** Copy own `key` from `from` onto `to`. A plain data slot (enumerable,
453
- * writable, configurable, no accessor) is a bare assignment — the common case
454
- * and the cheap one; anything else goes through defineProperty so accessors
455
- * and attribute flags survive the copy. */ function copyOwn(e, t, n) {
456
- const r = Object.getOwnPropertyDescriptor(t, n);
457
- if (r.get || r.set || !r.enumerable || !r.writable || !r.configurable) Object.defineProperty(e, n, r); else e[n] = r.value;
458
- }
459
-
460
- /** One-time own-accessor scan (Annex-B probes, no descriptor allocation);
461
- * returns true when the container is plain data (overlay-safe). Also grades
462
- * the container for the plain-data fast paths (`sc` = 2): `Object.prototype`
463
- * and every own key an enumerable data property — what a spread copies
464
- * exactly and what a bare assignment lands exactly. */ function scanAccessorsOnce(e) {
465
- const t = e.v;
466
- const n = Reflect.ownKeys(t);
467
- let r = Object.getPrototypeOf(t) === Object.prototype;
468
- for (const i of n) {
469
- // Own keys shadow prototype accessors, so the lookups are exact here.
470
- if (lookupGetter.call(t, i) !== undefined || lookupSetter.call(t, i) !== undefined) {
471
- e.a = true;
472
- r = false;
473
- break;
474
- }
475
- if (r && !propertyIsEnumerable.call(t, i)) r = false;
476
- }
477
- e.sc = r ? 2 : 1;
478
- e.kc = n.length;
479
- return !e.a;
480
- }
481
-
482
- /** Own-key count above which a draft opens as a prototype overlay rather
483
- * than a clone (#3360). Below it a spread clone of a plain container is
484
- * cheaper than the overlay's `Object.create` (V8 converts the committed
485
- * backing into a prototype, and every later flatten writes into that
486
- * prototype); above it the clone's O(keys) copy dominates (#3044). */ const OVERLAY_MIN_KEYS = 32;
487
-
488
- /** Downgrade a prototype-overlay pending backing to the clone path: builds
489
- * the real container (committed + overlay writes − deletes) that fold will
490
- * SWAP in as the committed backing, exactly as if the draft had started on
491
- * the clone path. Consumers that need a complete container (reconcile's
492
- * diff walks, drafts escaping into other storage) call this. */ function materializePB(e) {
493
- if (!e.ovl) return;
494
- const t = e.pb;
495
- const n = cloneRaw(e.v, e);
496
- for (const e of Reflect.ownKeys(t)) copyOwn(n, t, e);
497
- if (e.del !== null) {
498
- for (const t of e.del) delete n[t];
499
- e.del = null;
500
- }
501
- e.pb = n;
502
- e.ovl = false;
503
- }
504
-
505
- function ensurePB(e) {
506
- let t = e.pb;
507
- // Truth-staged backing hand-off (#3164 fold): a TENTATIVE draft opening on
508
- // a target whose pending backing is truth-staged (a landing folded into a
509
- // retaining transaction — it carries a foldBatches stamp) must not share
510
- // the container. Tentative writes would pollute staged truth, and the
511
- // tentative discard (notifyOptimisticWrites nulls pb) would destroy the
512
- // landing. Park the staged backing and open a fresh draft seeded from the
513
- // optimistic view below; the tentative discard restores it. The
514
- // tentativePBs guard scopes this to draft OPEN: the draft's own backing
515
- // (foldBatches-stamped by its first write when an action's transition is
516
- // ambient) must not be parked by its own later writes.
517
- if (t !== null && !tentativePBs.has(t) && e.fam?.opt === true && !projectionWriteActive && !getWriteOverride() && foldBatches.has(e)) {
518
- stagedTruthPB.set(e, t);
519
- t = e.pb = null;
520
- }
521
- if (activeTransition !== null) foldBatches.set(e, activeTransition);
522
- if (t === null) {
523
- // Prototype-chain overlay (#3044): plain-data non-array containers open
524
- // drafts in O(1) — own keys are the writes, reads fall through to
525
- // committed. Projection and derived-store families take it too (#3352:
526
- // a derive touching one root key of a wide keyed record paid an
527
- // O(keys) clone per recompute). Everything else keeps the descriptor
528
- // clone: arrays (splice/length semantics), OPTIMISTIC families (the
529
- // view-seeding below writes and deletes on the draft container, and the
530
- // tentative discard/truth-park hand whole backings around), chained
531
- // backings (the committed layer is another store's proxy — an overlay
532
- // would route every read-through into its traps), accessor containers
533
- // (live getters).
534
- // The overlay pays off only for a WIDE container over an OWNED committed
535
- // backing (#3360). Narrow containers clone cheaper than they overlay (a
536
- // spread is a fast-path copy; `Object.create` turns the backing into a
537
- // V8 prototype and every later flatten writes into that prototype). An
538
- // unowned backing has to be privatized (cloned) at commit anyway, so an
539
- // overlay there would cost the create PLUS the clone PLUS a per-key
540
- // copy — the clone path does the one clone and swaps it in. The first
541
- // write on a fresh store is exactly that case.
542
- const n = e.v;
543
- if (!e.fam?.opt && !e.ch && !Array.isArray(n) && (e.sc !== 0 ? !e.a : scanAccessorsOnce(e)) && e.kc > OVERLAY_MIN_KEYS && isOwned(n)) {
544
- // Inherits `v`'s $OWNER stamp — the overlay resolves and reads as
545
- // owned without a registration of its own.
546
- t = e.pb = Object.create(n);
547
- e.ovl = true;
548
- } else t = e.pb = cloneRaw(n, e);
549
- // Optimistic families: seed USER drafts from the OPTIMISTIC VIEW
550
- // (committed + active node overrides), so follow-up writes compose on
551
- // optimism instead of clobbering from base (#2951's compose half).
552
- // AUTHORITATIVE drafts (projection recompute / write-override landings)
553
- // seed from committed truth — seeding overrides there would fold a lane
554
- // value into the committed home ("authority wins at reveal" would break).
555
- if (e.fam?.opt && !projectionWriteActive && !getWriteOverride()) {
556
- tentativePBs.add(t);
557
- const n = e.n;
558
- if (n !== null) {
559
- for (const e of Reflect.ownKeys(n)) {
560
- const r = n[e];
561
- if (hasActiveOverride(r)) t[e] = unwrapOverride(r.o?.Fe);
562
- }
563
- }
564
- const r = e.h;
565
- if (r !== null) {
566
- for (const e of Reflect.ownKeys(r)) {
567
- const n = r[e];
568
- if (hasActiveOverride(n) && !unwrapOverride(n.o?.Fe)) delete t[e];
569
- }
570
- }
571
- }
572
- queueFold(e);
573
- }
574
- return t;
575
- }
576
-
577
- /** Sentinel holder for `t.ht`: a latest()-pull staged this adoption outside
578
- * any transition — the hold lasts until the fold commit (drainFolds). */ const PLAIN_HOLD = Symbol("plainHold");
579
-
580
- /** True while a latest() read is pulling the projection computed up to date
581
- * (see the get trap): adoptions landing during the pull are speculative
582
- * against the un-flushed batch and stage a held view. (Not injectable — the
583
- * derived createStore overload retains projection machinery in every store
584
- * bundle, see treeshake.test.ts.) */ let latestPullActive = false;
585
-
586
- /** Resolve the held committed view (#3074): answers the masked old backing
587
- * while the hold is live, and lazily clears a hold whose transition has
588
- * committed (transitions merge — resolve through currentTransition, same as
589
- * foldHeld's node stamps). */ function heldMaskView(e) {
590
- const t = e.ht;
591
- if (t === null) return null;
592
- if (t !== PLAIN_HOLD && currentTransition(t)?.Rn === true) return e.ht = e.hv = null;
593
- return e.hv;
594
- }
595
-
596
- /**
597
- * Adoption (2026-08-16c): the incoming object becomes the committed backing
598
- * IMMEDIATELY — reconcile is eagerly visible to every reader (shipped
599
- * contract; only its notifications batch), unlike setter writes which stay
600
- * pending until flush. Ownership resets (incoming is unowned/user data). Any
601
- * staged draft clone folds into the diff and is discarded — next is the
602
- * authoritative base (R21/R32).
603
- */ function adoptPB(e, t, n = false) {
604
- // Eager mode (plain-store adoption): the caller notifies inline after its
605
- // descent — no foldOlds queue/drain round trip (the reconcile diff IS the
606
- // fold diff; ~half of dbmon tick time was this duplication).
607
- if (!n) {
608
- queueFold(e);
609
- // records the pre-batch old before we swap
610
- // Diff base = the view the nodes were last told (#3296). A draft's
611
- // setter-exit notifications already moved them to its pending backing,
612
- // so a later adoption diffs against THAT — against committed, a key the
613
- // draft changed and the adoption restores would never re-notify. An
614
- // adoption with no draft leaves nodes where they were: keep an existing
615
- // base, else the pre-batch committed (foldOlds' entry itself stays the
616
- // committed identity for the path-copy CAS). Eager callers read t.pb
617
- // directly; this hand-off exists because pb is gone by drain time.
618
- if (e.pb !== null) {
619
- if (e.ovl) materializePB(e);
620
- e.ab = e.pb;
621
- } else e.ab ??= foldOlds.get(e);
622
- }
623
- // #3074/#3075: a projection recompute deriving from uncommitted inputs
624
- // swaps the backing SPECULATIVELY — committed-visibility readers must
625
- // keep the pre-hold view until the hold resolves (a source held by a
626
- // live transition, or a latest()-pull ahead of the flush). Post-await
627
- // landings (write-override) stay immediately visible — landed truth —
628
- // and clear any hold. Optimistic families hold too (#3330 store twin):
629
- // their tentative edits ride the lane machinery, but a sync derive
630
- // adopting under a transaction is held TRUTH like any projection's —
631
- // unheld, handlers read it early and an optimistic write equal to it
632
- // compared as a no-op against the swapped-in backing. A plain store's
633
- // reconcile inside an action holds the same way: its nodes stage under the
634
- // transaction (the inline notify), and the backing must not show handlers
635
- // and stale readers what the tracked read masks (signal parity, #3336).
636
- if (getWriteOverride()) {
637
- e.ht = e.hv = null;
638
- } else if (activeTransition !== null || !n && latestPullActive) {
639
- if (heldMaskView(e) === null) e.hv = e.v;
640
- e.ht = activeTransition ?? PLAIN_HOLD;
641
- if (!n && activeTransition !== null) heldAdoptions.add(e);
642
- }
643
- e.pb = null;
644
- // Overlay and accessor-scan state describe the OUTGOING backing — a
645
- // swapped container must not inherit them: a stale `ovl` beside a nulled
646
- // pb crashes materializePB (unwrapValue consults ovl before the
647
- // null-coalesce), a stale `del` would read the adoptee's keys as deleted
648
- // in the next draft, and a stale plain-data verdict (`sc`/`a`) could
649
- // admit an accessor-bearing adoptee to the overlay path. Reset; the next
650
- // draft rescans once (#3044 audit follow-up).
651
- e.ovl = false;
652
- e.del = null;
653
- e.sc = 0;
654
- e.a = false;
655
- e.wk = null;
656
- // adoption supersedes staged trap writes
657
- e.v = t;
658
- e.ch = t[$TARGET] !== undefined;
659
- // An adoptee we own within this family (a draft aliasing one of the
660
- // family's own backings) re-stamps to its new owner — the stamp is the
661
- // family's registration for it; everything else registers in the map.
662
- const r = t[$OWNER];
663
- if (r !== undefined && r.fam === e.fam) t[$OWNER] = e; else (e.fam?.map ?? storeNextLookup).set(t, e);
664
- }
665
-
666
- /** Sentinel for `t.wk`: the written-keys bound is unusable this batch (an
667
- * array length write implicitly deleted indices) — consumers full-scan. */ const WK_ALL = new Set;
668
-
669
- const plainProto = e => {
670
- const t = Object.getPrototypeOf(e);
671
- return t === Object.prototype || t === Array.prototype || t === null;
672
- };
673
-
674
- function queueFold(e) {
675
- if (foldOlds.has(e)) return;
676
- if (!hookInstalled) {
677
- hookInstalled = true;
678
- setStoreCommitHook(drainFolds);
679
- }
680
- // Always arm — "map non-empty ⇒ drain scheduled" is NOT an invariant: a
681
- // held re-queue, or an incomplete-transition flush (which skips
682
- // commitPendingNodes entirely), leaves entries behind after `scheduled`
683
- // was consumed. A size-gated arm then strands every LATER fold — queued
684
- // silently, never drained, committed base frozen at stale state while its
685
- // nodes commit (#3089). schedule() early-returns when already armed.
686
- schedule();
687
- foldOlds.set(e, e.v);
688
- }
689
-
690
- /** Fold write-attribution (#3089): a draft written while a transition is
691
- * active belongs to that transition — its fold must not commit before the
692
- * transition settles. Observed keys already defer through the held check in
693
- * drainFolds (their nodes carry _pendingValue); this write-time stamp is the
694
- * equivalent hold for UNOBSERVED keys, which have no node to consult.
695
- * Refreshed on every write; resolved through currentTransition at drain
696
- * (transitions merge — same rule as heldMaskView). */ const foldBatches = new WeakMap;
697
-
698
- /** Parked truth-staged pending backings (#3164 fold): a tentative draft that
699
- * opens while a folded landing's backing is live moves the staged container
700
- * here (see ensurePB); the tentative discard in notifyOptimisticWrites
701
- * restores it in place of the usual null. */ const stagedTruthPB = new WeakMap;
702
-
703
- /** Backings opened by TENTATIVE drafts (optimistic user setters): ensurePB's
704
- * truth-park must not fire against the draft's own container on its second
705
- * and later writes (the first write stamps foldBatches whenever an action's
706
- * transition is ambient). Entries die with their draft — tentative backings
707
- * are consumed at setter exit. */ const tentativePBs = new WeakSet;
708
-
709
- /** A draft read composes the live optimistic view until the draft has opened
710
- * its OWN view-seeded backing (ensurePB seeds that clone from the view and
711
- * registers it in tentativePBs; from then on reads must see the draft's
712
- * writes, not the overrides they superseded). A pending backing that exists
713
- * for any other reason is not that clone — a truth landing staged into a
714
- * retaining transaction (#3164 fold) is authoritative truth WITHOUT the
715
- * live overrides. ensurePB parks such a backing on the draft's first WRITE
716
- * and reseeds from the view, but the reads that precede that write went to
717
- * the staged truth: `votes++` read base, wrote base+1, and the override it
718
- * emitted landed on the value already displayed — a second in-flight
719
- * increment made after a sibling's landing was invisible (#2951's compose
720
- * half, one landing later). */ function draftSeesOverrides(e) {
721
- return e.pb === null || !tentativePBs.has(e.pb);
722
- }
723
-
724
- /** Committed-time privatization for parent-chain slot updates (path copying). */ function privatizeCommitted(e) {
725
- if (isOwned(e.v)) return;
726
- const t = e.v;
727
- const n = cloneRaw(t, e);
728
- e.v = n;
729
- e.ch = false;
730
- if (e.u) {
731
- privatizeCommitted(e.u);
732
- devAssertNeverUserMutation(e.u.v);
733
- // CAS, same as drainFolds' path copy: re-point the parent slot only
734
- // while it still holds the raw we cloned. A parent that folded EARLIER
735
- // in this drain may have replaced or deleted this slot (the same batch
736
- // wrote the child and then `parent.row = fresh` / `parent.length = 0`)
737
- // — an unconditional write resurrected the dropped child over it.
738
- const n = e.u.v;
739
- const r = parentSlotKey(e, t);
740
- if (n[r] === t) n[r] = e.v;
741
- }
742
- }
743
-
744
- /** Resolve the slot this child currently occupies in its parent's committed
745
- * backing (#3282). `pk` is stamped at wrap time and arrays MOVE: a reverse/
746
- * unshift/splice relocates the raw, and a fold that re-points the wrap-time
747
- * slot writes the clone over whichever row lives there now. Objects never
748
- * move keys, so the stamp is authoritative; for arrays, verify and re-locate
749
- * by identity when stale (fold-time only — never on a read path). */ function parentSlotKey(e, t) {
750
- const n = e.pk;
751
- const r = e.u.v;
752
- if (r[n] === t || !Array.isArray(r)) return n;
753
- const i = r.indexOf(t);
754
- if (i === -1) return n;
755
- e.pk = i;
756
- return i;
757
- }
758
-
759
- /** Overlay commit (#3044): apply the batch's writes onto an OWNED committed
760
- * backing in place — O(written), not O(container). Unowned backings
761
- * privatize first (clone once, parents re-slotted) — the never-mutate-user-
762
- * data contract holds. Shared by the deferred fold (drainFolds) and the
763
- * projection landing's immediate commit (notifyWrites). */ function flattenOverlay(e, t) {
764
- privatizeCommitted(e);
765
- const n = e.v;
766
- // Plain-data grade (sc 2): every own key on the overlay is an enumerable
767
- // writable data slot (set-trap writes; a non-plain defineProperty
768
- // downgrades the grade) — a bare assignment lands it without the
769
- // descriptor round trip.
770
- for (const r of Reflect.ownKeys(t)) e.sc === 2 ? n[r] = t[r] : copyOwn(n, t, r);
771
- if (e.del !== null) {
772
- for (const t of e.del) delete n[t];
773
- e.del = null;
774
- }
775
- e.pb = null;
776
- e.ovl = false;
777
- e.wk = null;
778
- // written-keys window closes with the commit
779
- }
780
-
781
- function drainFolds() {
782
- if (foldOlds.size === 0) return;
783
- const e = [ ...foldOlds ];
784
- foldOlds.clear();
785
- for (const [t, n] of e) {
786
- // A latest()-pull staging holds only until the fold commit: this flush
787
- // is committing the batch the pull ran ahead of. Transition holds stay —
788
- // they clear when their transition is done (heldMaskView).
789
- if (t.ht === PLAIN_HOLD) t.ht = t.hv = null;
790
- if (t.pb !== null) {
791
- // #3089: a fold written under a still-running transition defers to
792
- // that transition's settle (the write-time stamp covers unobserved
793
- // keys; observed keys also hit the pending-node held check below).
794
- const e = foldBatches.get(t);
795
- if (e !== undefined) {
796
- if (currentTransition(e).Rn === false) {
797
- foldOlds.set(t, n);
798
- continue;
799
- }
800
- foldBatches.delete(t);
801
- }
802
- // Setter path: nodes were setSignal'd at setter exit (write-time
803
- // notification — transitions/holds ride core machinery). Commit the
804
- // backing only for keys whose nodes have committed; a still-pending
805
- // node (transition-held) re-queues the target for the settling flush.
806
- let r = false;
807
- const i = t.pb;
808
- const l = t.n;
809
- if (l !== null) {
810
- // Only written keys can hold (their nodes took the setSignal); the
811
- // wk bound keeps this O(written) — see notifyWrites. Same fallback
812
- // rules as the notify (WK_ALL / accessors / non-plain prototypes).
813
- const e = t.wk;
814
- const n = e === null || e === WK_ALL || t.a === true ||
815
- // Overlay pbs chain to the COMMITTED object (#3044) — plainness is
816
- // the committed container's prototype, not the overlay's.
817
- !plainProto(t.ovl ? t.v : i) ? Reflect.ownKeys(l) : e;
818
- for (const e of n) {
819
- const t = l[e];
820
- if (t !== undefined && t._e !== NOT_PENDING) {
821
- r = true;
822
- break;
823
- }
824
- }
825
- }
826
- if (r) {
827
- foldOlds.set(t, n);
828
- // re-queue: commit happens when the hold settles
829
- continue;
830
- }
831
- if (t.ovl) {
832
- // Overlay flatten (#3044): the backing keeps its identity, so the
833
- // `t.v === old` gate below skips path copying (the parent slot
834
- // already points here) and the adopted-notify (setter notifications
835
- // happened at write time).
836
- flattenOverlay(t, i);
837
- } else if (t.v !== n) {
838
- // Privatized mid-batch (#3271): an earlier fold in this drain
839
- // path-copied THROUGH this target — privatizeCommitted cloned the
840
- // committed backing, re-pointed the parent slot at the clone, and
841
- // stitched the descendant's fold into it. The draft's pb predates
842
- // that: swapping it in would clobber the descendant's fold, and the
843
- // parent CAS below (still comparing against `old`) would fail and
844
- // orphan this fold entirely — a projection draft writing descendant-
845
- // then-ancestor silently lost the ancestor write. Merge the batch's
846
- // writes onto the current container instead (the parent slot already
847
- // points at it). privatize first: an adopt-then-write batch can land
848
- // here with an unowned adoptee as t.v.
849
- privatizeCommitted(t);
850
- const e = t.v;
851
- const r = t.wk;
852
- if (r !== null && r !== WK_ALL) {
853
- // The trap records every write/delete key — apply exactly those.
854
- for (const t of r) {
855
- if (hasOwn.call(i, t)) copyOwn(e, i, t); else delete e[t];
856
- }
857
- } else {
858
- // Array length write poisoned the bound (WK_ALL) — value-diff
859
- // against the pre-batch old. Slots the draft never touched hold
860
- // the same raw reference in both, so descendant folds stay put.
861
- // Not copyOwn: the plain-value write is GATED on "the draft changed
862
- // this slot" — an untouched slot in pb holds the pre-batch reference,
863
- // and writing it back would clobber a descendant fold stitched into v.
864
- for (const t of Reflect.ownKeys(i)) {
865
- const r = Object.getOwnPropertyDescriptor(i, t);
866
- if (r.get || r.set || !r.enumerable || !r.writable || !r.configurable) Object.defineProperty(e, t, r); else if (r.value !== n[t] || !hasOwn.call(n, t)) e[t] = r.value;
867
- }
868
- for (const t of Reflect.ownKeys(n)) {
869
- if (!hasOwn.call(i, t)) delete e[t];
870
- }
871
- }
872
- t.pb = null;
873
- t.wk = null;
874
- // written-keys window closes with the fold commit
875
- } else {
876
- t.v = i;
877
- t.ch = false;
878
- // pb is always a plain clone
879
- t.pb = null;
880
- t.wk = null;
881
- // written-keys window closes with the fold commit
882
- }
883
- }
884
- const e = t.ab;
885
- t.ab = null;
886
- if (t.v !== n) {
887
- // Path copying (CAS: see the eager-fold twin above). Slot resolved by
888
- // identity (#3282): an array move relocated the raw, so the wrap-time
889
- // pk may point at a sibling — a raw-slot CAS there both failed to
890
- // re-point AND (via privatizeCommitted's unguarded write) clobbered
891
- // the sibling.
892
- if (t.u) {
893
- const e = parentSlotKey(t, n);
894
- if (t.u.v[e] === n) {
895
- privatizeCommitted(t.u);
896
- devAssertNeverUserMutation(t.u.v);
897
- t.u.v[e] = t.v;
898
- }
899
- }
900
- }
901
- // Adoption notify against the base the nodes were last told (#3296). A
902
- // no-op adoption (A -> B -> A before flush, no draft) has base === v and
903
- // nothing to say; a draft superseded by an adoption back to the SAME raw
904
- // still has (base = pending backing) !== v and must notify.
905
- if (e !== null && e !== t.v) notifyFold(t, e, t.v);
906
- }
907
- }
908
-
909
- /**
910
- * Setter-exit notification (write channel): diff the draft's pending backing
911
- * against committed and setSignal every changed OBSERVED key — write-time
912
- * notification with commit deferred to node commit, so transition holds,
913
- * isPending, affects, and lane machinery ride the core natively (§3's
914
- * "pending home = the node when a node exists"). Unobserved keys stay in the
915
- * pending backing and fold directly at commit.
916
- */ function notifyWrites(e) {
917
- let t = e.pb;
918
- if (t === null) return;
919
- // Optimistic channel: user writes on an optimistic family become node-level
920
- // engine writes (armed nodes route setSignal through optimisticWrite) — the
921
- // committed backing is NEVER touched; the draft clone is discarded. Reverts,
922
- // per-transaction ownership, and flash-at-flush are all core-native.
923
- // Projection recompute writes (projectionWriteActive) and projection draft
924
- // writes (write-override, incl. post-await async landings) are
925
- // authoritative and take the plain channel below (they commit silently
926
- // under overrides per the engine's no-revert-stash contract).
927
- if (e.fam?.opt) {
928
- if (!projectionWriteActive && !getWriteOverride()) {
929
- optHooks.notifyOptimisticWrites(e, t);
930
- return;
931
- }
932
- // Authoritative path on an optimistic family: armed nodes must commit
933
- // silently (engine bypass) — without this, a landing's setSignals would
934
- // create lanes and block their own transition's settle.
935
- if (!projectionWriteActive) {
936
- setProjectionWriteActive(true);
937
- try {
938
- notifyWrites(e);
939
- } finally {
940
- setProjectionWriteActive(false);
941
- }
942
- return;
943
- }
944
- }
945
- const n = e.v;
946
- const r = e.n;
947
- // Written-keys bound: trap writes record their keys, so the notify visits
948
- // O(written) nodes instead of every subscription on the record (a selection
949
- // map with thousands of per-key subscribers pays two visits per select,
950
- // not a full scan). Falls back to the full node scan when the bound can't
951
- // hold: no trap granularity (wk null), an array length write (WK_ALL —
952
- // implicit index deletes), accessors on the record (t.a — a getter node's
953
- // value can change when ANY key is written), or a non-plain prototype
954
- // (class instances: prototype getters derive from arbitrary fields).
955
- const i = e.wk;
956
- // Overlay pbs chain to the COMMITTED object (#3044): a prototype-overlay
957
- // draft is plain data on its own layer, but its getPrototypeOf is the
958
- // committed container — judge plainness by the COMMITTED prototype or the
959
- // bound never engages for overlay writes (every plain-object setter batch
960
- // would full-scan: the exact selection-map workload wk exists for; jf
961
- // `select` regressed 2x on this).
962
- const l = i === WK_ALL || e.a === true || !plainProto(e.ovl ? e.v : t) ? null : i;
963
- if (r !== null) {
964
- const i = l ?? Reflect.ownKeys(r);
965
- for (const l of i) {
966
- const i = r[l];
967
- if (i === undefined) continue;
968
- // Per-key accessor handling: the node's cached flag plus ONE getter
969
- // probe on the incoming side (getters arriving via merge/adoption).
970
- // Setter-only props read as data (value undefined) so lookupSetter is
971
- // not consulted on this hot path; prototype getters never own nodes.
972
- if (i.acc === true || hasOwn.call(t, l) && lookupGetter.call(t, l) !== undefined) {
973
- i.acc = isOwnAccessor(t, l);
974
- const e = Object.getOwnPropertyDescriptor(n, l);
975
- const r = Object.getOwnPropertyDescriptor(t, l);
976
- if (e && (e.get || e.set) || r && (r.get || r.set)) {
977
- if (e?.get !== r?.get || e?.set !== r?.set || e?.value !== r?.value) setSignal(i, () => FORCE);
978
- continue;
979
- }
980
- if (!isEqual(e?.value, r?.value)) setSignal(i, () => r?.value);
981
- continue;
982
- }
983
- // No old-side pre-compare: t.v lags across multi-batch windows (a
984
- // projection recompute can run before the prior fold commits) — the
985
- // node's OWN current value is the true old side, and setSignal's
986
- // internal equality already checks exactly that.
987
- const o = e.del !== null && e.del.has(l) ? undefined : t[l];
988
- // The derived store's setter reaching a leaf its own fold staged under
989
- // another transaction: not a proposal — the fold re-runs (#3612).
990
- if (derivedSetter !== null && i.De === derivedSetter && heldDerivation(i)) heldDerivationHit = true;
991
- setSignal(i, () => o);
992
- }
993
- }
994
- const o = e.h;
995
- if (o !== null) {
996
- const n = l ?? Reflect.ownKeys(o);
997
- for (const r of n) {
998
- const n = o[r];
999
- if (n !== undefined) setSignal(n, r in t && !(e.del !== null && e.del.has(r)));
1000
- }
1001
- }
1002
- // Deep-witness (dk): setter writes must notify a deep() subscriber even on
1003
- // keys with no node. O(written/pb keys) equality only when a witness exists.
1004
- if (e.dk !== null) {
1005
- if (e.del !== null && e.del.size !== 0) bumpDeep(e); else for (const r of l ?? Reflect.ownKeys(t)) {
1006
- if (r === $OWNER) continue;
1007
- const i = t[r];
1008
- const l = n[r];
1009
- if (i !== null && typeof i === "object" ? !targetsEqual(l, i) : !isEqual(l, i)) {
1010
- bumpDeep(e);
1011
- break;
1012
- }
1013
- }
1014
- }
1015
- if (e.k !== null) {
1016
- let r;
1017
- if (e.ovl) {
1018
- // Overlay membership: only NEW own keys or deletes can change it.
1019
- r = e.del !== null && e.del.size !== 0;
1020
- if (!r) {
1021
- for (const e of Reflect.ownKeys(t)) {
1022
- if (!hasOwn.call(n, e)) {
1023
- r = true;
1024
- break;
1025
- }
1026
- }
1027
- }
1028
- } else {
1029
- r = Array.isArray(t) && Array.isArray(n) ? arrayStructureChanged(n, t) : membershipChanged(n, t);
1030
- }
1031
- if (r) setSignal(e.k, e => e + 1);
1032
- }
1033
- // Projection backing folds split by channel (two pinned contracts):
1034
- // - sync-derive drafts (recompute body): NEVER eager — a downstream async
1035
- // hold can form LATER in the same flush and the leaf must stay at stale
1036
- // committed for context-free readers (spec-async "pends only the written
1037
- // leaf"). drainFolds commits when held-ness is knowable.
1038
- // - post-await async LANDINGS (write-override per-op, microtask context —
1039
- // no enclosing flush can capture them): the data-level commit is
1040
- // IMMEDIATE — landed truth shows to untracked readers even while a
1041
- // downstream consumer's own async still holds the effect-level reveal
1042
- // (spec-async "verdicts never inherit consumers' in-flight state").
1043
- // EXCEPT under an active transaction (#3164 fold): a landing riding a
1044
- // retaining transaction (the optimistic module's aroundWrite binds it)
1045
- // stages instead — ensurePB stamped foldBatches, so the backing commits
1046
- // with the transaction and the reveal is atomic at settle. The pinned
1047
- // immediate-commit contract is stated over the no-transaction microtask
1048
- // posture, which `activeTransition === null` is exactly.
1049
- if (e.fam !== null && e.pb !== null && getWriteOverride() && activeTransition === null) {
1050
- // Landed truth (post-await write-override): immediately visible to every
1051
- // reader — any staged held view is superseded.
1052
- if (e.ht !== null) e.ht = e.hv = null;
1053
- // Overlay landing (#3352): flatten in place — identity-stable, and
1054
- // privatizeCommitted re-slots the parent itself when it has to clone an
1055
- // unowned seed, so no path copy is needed.
1056
- if (e.ovl) return flattenOverlay(e, t);
1057
- const n = e.v;
1058
- e.pb = null;
1059
- e.v = t;
1060
- e.ch = false;
1061
- if (e.u) {
1062
- // Identity-resolved slot (#3282) — see drainFolds' path-copy twin.
1063
- const r = parentSlotKey(e, n);
1064
- if (e.u.v[r] === n) {
1065
- privatizeCommitted(e.u);
1066
- devAssertNeverUserMutation(e.u.v);
1067
- e.u.v[r] = t;
1068
- }
1069
- }
1070
- }
1071
- }
1072
-
1073
- const FORCE = Symbol();
1074
-
1075
- /** Same logical slot: both values resolve to one (re-pointed) child target —
1076
- * adoption preserved identity, so the slot did not change (R9). */ function targetsEqual(e, t) {
1077
- if (e === null || typeof e !== "object" || t === null || typeof t !== "object") return false;
1078
- const n = lookupTarget(e, null);
1079
- return n !== undefined && n === lookupTarget(t, null);
1080
- }
1081
-
1082
- function arrayStructureChanged(e, t) {
1083
- if (e.length !== t.length) return true;
1084
- for (let n = 0; n < t.length; n++) {
1085
- const r = e[n];
1086
- const i = t[n];
1087
- if (!isEqual(r, i) && !targetsEqual(r, i)) return true;
1088
- }
1089
- return false;
1090
- }
1091
-
1092
- function membershipChanged(e, t) {
1093
- const n = Reflect.ownKeys(t);
1094
- // The $OWNER stamp is not membership: an owned side counts one key more.
1095
- if (Reflect.ownKeys(e).length - +isOwned(e) !== n.length - +isOwned(t)) return true;
1096
- for (const t of n) if (t !== $OWNER && !(t in e)) return true;
1097
- return false;
1098
- }
1099
-
1100
- /**
1101
- * The fold diff walks SUBSCRIPTION KEYS ONLY (legacy parity: `for key in
1102
- * nodes`): nodes exist exactly where something tracked, so unobserved data
1103
- * costs nothing here regardless of object size. Accessor safety rides the
1104
- * sticky `t.a` flag — a node's key was necessarily read, so the get trap has
1105
- * already seen whether it is an accessor.
1106
- */
1107
- /** One node's fold notification (shared by notifyFold's walk and the fused
1108
- * adoption walk): accessor-aware compare + equality/identity-gated setSignal. */ function notifyKeyDiff(e, t, n, r,
1109
- // The incoming-side getter probe covers SETTER-channel arrivals (return-
1110
- // form merges, defineProperty) — those flow through notifyWrites/
1111
- // notifyFold, which probe. The RECONCILE channel (fused walk) passes
1112
- // false: reconcile adopts immutable data by contract (R2a) and the pinned
1113
- // getter-preservation tests are all setter-channel; skipping ~2 Annex-B
1114
- // calls per key per tick is a measured dbmon win.
1115
- i = true) {
1116
- if (e.acc === true || i && hasOwn.call(r, t) && lookupGetter.call(r, t) !== undefined) {
1117
- e.acc = isOwnAccessor(r, t);
1118
- const i = Object.getOwnPropertyDescriptor(n, t);
1119
- const l = Object.getOwnPropertyDescriptor(r, t);
1120
- if (i && (i.get || i.set) || l && (l.get || l.set)) {
1121
- // Accessor involved: never invoke; force-notify on shape change so
1122
- // subscribers re-read (and re-track) through the trap.
1123
- if (i?.get !== l?.get || i?.set !== l?.set || i?.value !== l?.value) setSignal(e, () => FORCE);
1124
- return;
1125
- }
1126
- const o = i?.value;
1127
- const f = l?.value;
1128
- if (!isEqual(o, f) && !targetsEqual(o, f)) setSignal(e, typeof f === "function" ? () => f : f);
1129
- } else {
1130
- const i = n[t];
1131
- const l = r[t];
1132
- // Direct value write when not a function (setSignal treats functions as
1133
- // updaters) — saves a closure allocation per changed key on the fold
1134
- // hot path.
1135
- if (!isEqual(i, l) && !targetsEqual(i, l)) setSignal(e, typeof l === "function" ? () => l : l);
1136
- }
1137
- }
1138
-
1139
- /** Accessor-flag probe for the fused walk's early-continue (accessor keys
1140
- * can never identity-skip: their VALUE is the descriptor's product). */ function hasAccessorFlag(e) {
1141
- return e.acc === true;
1142
- }
1143
-
1144
- /** Fused-walk per-key notification with values already in hand: the caller
1145
- * fetched both sides and handled the identity skip; this applies the
1146
- * accessor branch (cached flag only — reconcile channel) or the plain
1147
- * equality/identity-gated write. */ function notifyKeyValue(e, t, n, r, i, l) {
1148
- if (e.acc === true) {
1149
- notifyKeyDiff(e, t, i, l, false);
1150
- return;
1151
- }
1152
- // The pre-compare is NOT redundant with the node's equals: setSignal parks
1153
- // a pending value and registers with the batch before equality applies at
1154
- // commit (RUL-1), so identity-preserved slots (adopted child containers —
1155
- // every row's fresh `queries` array) must be gated out HERE or each one
1156
- // pays the full write machinery every tick (measured: +0.5ms/tick dbmon).
1157
- if (!isEqual(n, r) && !targetsEqual(n, r)) setSignal(e, typeof r === "function" ? () => r : r);
1158
- }
1159
-
1160
- /** Presence + membership halves of a fold notification (shared tail). */ function notifyFoldTail(e, t, n) {
1161
- const r = e.h;
1162
- if (r !== null) {
1163
- for (const e of Reflect.ownKeys(r)) setSignal(r[e], e in n);
1164
- }
1165
- if (e.k !== null) {
1166
- const r = Array.isArray(n) && Array.isArray(t) ? arrayStructureChanged(t, n) : membershipChanged(t, n);
1167
- if (r) setSignal(e.k, e => e + 1);
1168
- }
1169
- }
1170
-
1171
- function notifyFold(e, t, n) {
1172
- if (e.dk !== null && t !== n) bumpDeep(e);
1173
- // Optimistic targets: adoption notifications are authoritative landings —
1174
- // bypass the engine (commit into _value; active overrides keep shadowing
1175
- // until their transaction settles, per the no-revert-stash contract).
1176
- if (e.fam?.opt && !projectionWriteActive) {
1177
- setProjectionWriteActive(true);
1178
- try {
1179
- notifyFold(e, t, n);
1180
- } finally {
1181
- setProjectionWriteActive(false);
1182
- }
1183
- return;
1184
- }
1185
- const r = e.n;
1186
- if (r !== null) {
1187
- for (const e of Reflect.ownKeys(r)) {
1188
- notifyKeyDiff(r[e], e, t, n);
1189
- }
1190
- }
1191
- const i = e.h;
1192
- if (i !== null) {
1193
- for (const e of Reflect.ownKeys(i)) setSignal(i[e], e in n);
1194
- }
1195
- if (e.k !== null) {
1196
- // Key-set/$TRACK: objects notify on membership; arrays on any index or
1197
- // length change (mapArray and iteration re-read values — R15).
1198
- const r = Array.isArray(n) && Array.isArray(t) ? arrayStructureChanged(t, n) : membershipChanged(t, n);
1199
- if (r) setSignal(e.k, e => e + 1);
1200
- }
1201
- }
1202
-
1203
- // ---------------------------------------------------------------------------
1204
- // traps
1205
- /** >0 while inside a setter: writes allowed, reads are read-your-writes. */ let writing = 0;
1206
-
1207
- /** Write scope keys (a family object or a plain store's root target): draft
1208
- * semantics — write permission, read-your-writes, tracking suppression —
1209
- * apply ONLY to targets under a scope being written. Reads of OTHER stores
1210
- * inside a setter track normally (they are dependencies: a projection derive
1211
- * reading another store must link it). */ let writeScopes = null;
1212
-
1213
- function scopeKey(e) {
1214
- if (e.fam !== null) return e.fam;
1215
- let t = e;
1216
- while (t.u !== null) t = t.u;
1217
- return t;
1218
- }
1219
-
1220
- function inDraft(e) {
1221
- return writeScopes !== null && writeScopes.has(scopeKey(e));
1222
- }
1223
-
1224
- /** Shallow serve rule (#2932): raw-marked data serves VERBATIM, but a
1225
- * store-proxy slot value gets a boundary wrapper in THIS store's own family —
1226
- * write isolation through derived chains (downstream writes must never land
1227
- * upstream). markRawOne skips proxies for exactly this reason. */ function serveShallow(e, t, n) {
1228
- if (n !== null && typeof n === "object" && n[$TARGET] !== undefined) return draftServe(e, wrapNext(n, e, t));
1229
- return n;
1230
- }
1231
-
1232
- /** Draft reads extend write permission to reachable stores (legacy Writing
1233
- * semantics: wrapping a child through a draft get admits it — cross-store
1234
- * writes like `s.inner.a = 10` work when `inner` is another store's proxy). */ function draftServe(e, t) {
1235
- if (writeScopes !== null && inDraft(e)) {
1236
- const e = t?.[$TARGET];
1237
- if (e !== undefined && e.v !== undefined) writeScopes.add(scopeKey(e));
1238
- }
1239
- return t;
1240
- }
1241
-
1242
- /** Targets written during the current (outermost) setter — notified at exit. */ const pendingNotify = new Set;
1243
-
1244
- /** Targets adopted under a live transaction this setter (adoptPB set a
1245
- * transaction hold) — their nodes are notified at the outermost exit. */ const heldAdoptions = new Set;
1246
-
1247
- /**
1248
- * Write-time notification for a transaction-held adoption (#3330 store twin;
1249
- * the setter path's notifyWrites twin for adoptions). A projection's fold
1250
- * normally notifies its nodes at the drain — and the drain of a batch parked
1251
- * in a live transaction is the transaction's COMMIT, so the nodes took the
1252
- * adopted values as fresh writes at commit time: every subscriber was
1253
- * re-marked and re-ran against a frame the lane had already published (a
1254
- * third `v=1 d=2`), where a signal's write had staged at write time and
1255
- * promoted silently. Staging here, inside the transaction's batch, makes the
1256
- * two paths one: the nodes carry transition-stamped `_pendingValue`s, their
1257
- * subscribers recompute in this flush and park with the transaction, and the
1258
- * commit promotes without re-notifying. `ab` moves to the adopted backing —
1259
- * the view the nodes were last told (#3296) — so the drain has nothing left
1260
- * to say and only path-copies.
1261
- */ function stageHeldAdoptions() {
1262
- const e = [ ...heldAdoptions ];
1263
- heldAdoptions.clear();
1264
- for (const t of e) {
1265
- const e = t.ab;
1266
- if (e === null || e === t.v) continue;
1267
- notifyFold(t, e, t.v);
1268
- t.ab = t.v;
1269
- }
1270
- }
1271
-
1272
- const UNSAFE_KEYS = new Set([ "__proto__", "prototype", "constructor" ]);
1273
-
1274
- /** Mirror of core read()'s context rule: the OWNER context (not the tracking
1275
- * observer) decides pending visibility, with Roots resolving to their parent
1276
- * computed (#2687 — untracked reads inside mapArray Roots see in-flight
1277
- * values mid-flush). CHILDREN_FORBIDDEN execution scopes (createTrackedEffect
1278
- * / onSettled callbacks) get COMMITTED visibility (#3006), same as core. */
1279
- /** Core read()'s reader: the current computation, a root reading as its
1280
- * parent computed (`context` persists under untrack — an untracked read
1281
- * inside an effect is still that effect's read). */ function readerContext() {
1282
- const e = getOwner();
1283
- return e === null ? null : e.Wn ? e.qn ?? null : e;
1284
- }
1285
-
1286
- /** A pending fold is transition-held when any written node's parked value is
1287
- * stamped by a live transition (a plain batch parking — the lazy-recompute
1288
- * read case — has no transition stamp and serves fresh). */ function foldHeld(e) {
1289
- const t = e.n;
1290
- if (t === null) return false;
1291
- for (const e of Reflect.ownKeys(t)) {
1292
- const n = t[e];
1293
- if (n._e !== NOT_PENDING && n.we != null && n.we.Rn !== true) return true;
1294
- }
1295
- return false;
1296
- }
1297
-
1298
- function readSource(e) {
1299
- // Adoption hold first (#3074): an adoption staged under a live transaction
1300
- // (or a latest()-pull, PLAIN_HOLD) serves the pre-hold committed view to
1301
- // committed-visibility readers — context-free and children-forbidden ones,
1302
- // and stale passes off a foreign hold. Drafts, write-override and latest()
1303
- // see the adopted backing.
1304
- const t = e.ht;
1305
- if (t !== null && !latestReadActive && !inDraft(e) && !getWriteOverride()) {
1306
- const n = heldMaskView(e);
1307
- if (n !== null) {
1308
- const e = readerContext();
1309
- if (e === null || e.C & CONFIG_CHILDREN_FORBIDDEN || !holdVisible(t === PLAIN_HOLD ? null : currentTransition(t), e)) return n;
1310
- }
1311
- }
1312
- return pendingBackingVisible(e, false) ? e.pb : e.v;
1313
- }
1314
-
1315
- /** The single pb-vs-committed visibility decision (#3147), shared by per-key
1316
- * backing reads (readSource) and deep()/snapshot composition (snapshotWalk)
1317
- * so the two reader families can never disagree about a HELD landing.
1318
- *
1319
- * Signal-parity visibility (core read(): owner-context reads serve
1320
- * _pendingValue, context-free reads serve committed — effects recompute
1321
- * BEFORE commitPendingNodes in the flush, so the pending view must be
1322
- * servable). Drafts (setter window OR projection write-override) and
1323
- * owner-context reads see the pending backing; context-free reads see
1324
- * committed. Node reads apply the same rule, so all homes agree.
1325
- *
1326
- * `speculative` is deep()/snapshot's posture: an untrack/deep PEEK that sees
1327
- * ordinary pending staging regardless of owner context (the documented
1328
- * divergence from context-free per-key reads) — but never through a hold:
1329
- * held truth stays masked exactly as it is for per-key readers. */ function pendingBackingVisible(e, t) {
1330
- if (e.pb === null) return false;
1331
- // The writer's own channels compose on the pending backing regardless.
1332
- if (inDraft(e) || getWriteOverride()) return true;
1333
- // HELD truth on an optimistic family (#3164 fold) is masked from LANE
1334
- // passes until the transaction's reveal (the backing-level twin of core
1335
- // serve()'s CONFIG_HELD_TRUTH arm; authoritative postures and latest()
1336
- // tunnel through inside heldTruthMasked). Every other reader takes the
1337
- // hold arms below: committed for no pass, the staged backing for the
1338
- // holding pass, stale-of-foreign or A29 for a foreign one.
1339
- if (heldTruthMasked(e)) return false;
1340
- const n = readerContext();
1341
- if (n === null || n.C & CONFIG_CHILDREN_FORBIDDEN) {
1342
- // No pass, or a children-forbidden one: the committed frame (A32) —
1343
- // except the speculative peek (deep()/snapshot()), which sees ordinary
1344
- // pending staging but never through a live foreign hold, and a
1345
- // projection's pending backing, authoritative-elect for context-free
1346
- // readers UNLESS a transition holds the node commits (downstream async
1347
- // hold — stale committed is the contract; the write-time stamp covers
1348
- // keys with no node, #3336) or the scope is children-forbidden (#3082).
1349
- const r = liveFoldTransition(e);
1350
- if (t) return r === null || ownsHold(r);
1351
- return e.fam !== null && n === null && !foldHeld(e) && r === null;
1352
- }
1353
- return holdVisible(liveFoldTransition(e), n);
1354
- }
1355
-
1356
- /** #3164 fold: HELD truth on an optimistic family — a pending backing
1357
- * stamped by a live transition that retains optimism — is masked from LANE
1358
- * passes only (a lane paints display-ahead at the park, so it keeps
1359
- * committed until the transaction's reveal — owning transaction or not);
1360
- * the authoritative postures and latest() tunnel through, and every other
1361
- * reader is served by the ordinary hold arms (a deriving pass is held with
1362
- * the truth, A29). Un-stamped backings and optimism-free transitions keep
1363
- * ordinary mid-batch/speculation visibility. */ function heldTruthMasked(e) {
1364
- if (e.fam?.opt !== true || currentOptimisticLane === null || latestReadActive || authoritativeServe()) return false;
1365
- const t = foldBatches.get(e);
1366
- // opt families are only created by createOptimisticStore, whose module
1367
- // install populates optHooks — the assertion holds by construction.
1368
- return t !== undefined && optHooks.retainsOptimism(t);
1369
- }
1370
-
1371
- const hasOwn = Object.prototype.hasOwnProperty;
1372
-
1373
- // Allocation-free own-accessor probe (replaces eager descriptor scans — the
1374
- // single biggest creation cost in the uibench profile): Annex-B lookups
1375
- // return the fn or undefined with no descriptor object. Own data properties
1376
- // shadow prototype accessors, so hasOwn + lookup is an exact own-check.
1377
- const lookupGetter = Object.prototype.__lookupGetter__;
1378
-
1379
- const lookupSetter = Object.prototype.__lookupSetter__;
1380
-
1381
- const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
1382
-
1383
- function isOwnAccessor(e, t) {
1384
- return hasOwn.call(e, t) && (lookupGetter.call(e, t) !== undefined || lookupSetter.call(e, t) !== undefined);
1385
- }
1386
-
1387
- /** Authoritative-write wrapper exported for the optimistic module: sets the
1388
- * scheduler's projectionWriteActive through THIS module's binding (proven to
1389
- * share the instance core reads — cross-module live-binding writes from other
1390
- * store modules were observed not to propagate under the test transform). */ function runAuthoritative(e) {
1391
- const t = projectionWriteActive;
1392
- setProjectionWriteActive(true);
1393
- try {
1394
- return e();
1395
- } finally {
1396
- setProjectionWriteActive(t);
1397
- }
1398
- }
1399
-
1400
- /** The reading computation is until()'s authoritative-view predicate — same
1401
- * source of truth as core read()'s A17 carve-out (`context`, which persists
1402
- * under untrack). optimisticView()'s composition gate consults exactly this:
1403
- * write-side machinery (patch emission, tentative re-application) must keep
1404
- * composing even when it runs inside an authoritative-write bracket. */ function authoritativeRead() {
1405
- const e = context;
1406
- return e !== null && (e.C & CONFIG_AUTHORITATIVE_READ) !== 0;
1407
- }
1408
-
1409
- /** Serve-side authoritative gate: until()'s predicate PLUS truth authors —
1410
- * the projection derive's draft (wrapDraft trap brackets, runAuthoritative;
1411
- * the same posture pair ensurePB classifies drafts by). A source computing
1412
- * the next truth must never read its callers' tentative overlays: a derive
1413
- * continuation's `store.push` computing its index from an action's
1414
- * optimistic row landed truth in the wrong slot and corrupted committed
1415
- * state (#3108). Trap-level overlay serves gate on this so values, length,
1416
- * membership, and keys leave the authoritative view together. */ function authoritativeServe() {
1417
- return projectionWriteActive || getWriteOverride() || authoritativeRead();
1418
- }
1419
-
1420
- /** Context-aware node view for reads outside tracking: active override >
1421
- * held pending (owner context) > the BACKING value. Committed truth lives in
1422
- * the backing (single-home rule, O6) — node `_value` is never served here,
1423
- * so a lazy recompute's landing is immediately visible to the untracked
1424
- * reader that forced it (backing commits eagerly; node values fold at flush).
1425
- * FORCE sentinels never surface (they only bump subscribers of accessor
1426
- * keys, which are served by the trap, not the node). */ function nodeValue(e, t) {
1427
- // Store-only tunnels, ahead of Rule 1: truth authors (authoritativeServe —
1428
- // the projection derive's draft, the write-override continuation) see
1429
- // staged truth and never an override; latest() reaching this untracked
1430
- // path for a store key sees the in-flight parked value like an
1431
- // owner-context reader does (#3075), the visible override first.
1432
- let n;
1433
- if (authoritativeServe()) n = e._e !== NOT_PENDING ? e._e : t; else if (latestReadActive) n = visibleOverride(e) ? unwrapOverride(e.o?.Fe) : e._e !== NOT_PENDING ? e._e : t;
1434
- // Otherwise the one slow selection core read() uses (serve): override,
1435
- // lane gate, A28, readerSeesCommitted / A29 — with the BACKING as the
1436
- // committed value (single-home rule, O6).
1437
- else n = serve(e, readerContext(), e.De || e, t);
1438
- return n === FORCE ? t : n;
1439
- }
1440
-
1441
- /** The override a composed READER view (keys, descriptors, snapshot/deep,
1442
- * optimisticView) takes from an armed node whose override is active — the
1443
- * override itself, unless the node's own source superseded it (#3331,
1444
- * CONFIG_OVERRIDE_SUPERSEDED): then the reader-aware selection `serve`
1445
- * makes through nodeValue, so the composed view agrees with what `get`,
1446
- * `in` and `length` serve the same reader (a deriving pass: the staged
1447
- * truth; a lane pass or a context-free read: the override, A18). Composing
1448
- * the raw override left Object.keys / snapshot() / deep() one row behind the
1449
- * traps at a landing whose shape differed from the optimistic frame (F5
1450
- * parity cases). Draft and authoritative callers never reach here — the
1451
- * writer composes on hasActiveOverride, truth authors on the backing. */ function readerOverride(e, t) {
1452
- return e.C & CONFIG_OVERRIDE_SUPERSEDED ? nodeValue(e, t) : unwrapOverride(e.o?.Fe);
1453
- }
1454
-
1455
- /** §7b: a chained target's child found as a RAW — from its pending backing
1456
- * (a cloneRaw of the inner proxy, whose descriptors yield the inner store's
1457
- * raws) or from the deep() walk's descriptor read through the chain — must
1458
- * resolve to the inner family's proxy for that object before this family
1459
- * wraps it, so the overlay serves the same chained targets as the settled
1460
- * state. Otherwise every row re-wrapped as a fresh non-chained target keyed
1461
- * by the raw: identities churned for the life of an optimistic action and
1462
- * snapped back at settle, and writes landed on those orphans while deep()'s
1463
- * witnesses sat on the chained targets (#3323). The inner family owns the
1464
- * raw iff it has served it or its backing holds that exact object at `key`;
1465
- * anything else is a draft's own replacement object — view-owned, correctly
1466
- * non-chained — and is returned as is. Recurses down further chains. */ function resolveChainedRaw(e, t, n) {
1467
- const r = e.v[$TARGET];
1468
- if (r.ch) {
1469
- const e = resolveChainedRaw(r, t, n);
1470
- return e === n ? n : wrapNext(e, r, t);
1471
- }
1472
- const i = lookupTarget(n, r.fam);
1473
- if (i !== undefined) return i.px;
1474
- if ((r.v[t] === n || r.pb?.[t] === n) && isWrappable(n)) return wrapNext(n, r, t);
1475
- return n;
1476
- }
1477
-
1478
- /** Serve an own data key: node-first when a node exists (pending visibility,
1479
- * holds, lanes ride the node); backing otherwise. Chained backings (§7b: the
1480
- * backing IS another store's proxy) serve the read-through value — the outer
1481
- * node is linked only for adoption-swap notification, its value never
1482
- * shadows the live chain. */ function serveDataKey(e, t, n, r, i, l = -1) {
1483
- const o = e.ch && r === e.v;
1484
- let f = n;
1485
- // §6: on optimistic arrays LENGTH IS A VIEW, not a node value — one home
1486
- // (backing ± presence overrides) for both length and indices makes torn
1487
- // iteration impossible (a length node's value rides different visibility
1488
- // rails than index overrides mid-settle). The node carries subscriptions;
1489
- // its committed `_value` is never served here.
1490
- if (t === "length" && e.fam?.opt === true && !o && Array.isArray(r)) {
1491
- if (!inDraft(e)) {
1492
- const i = e.n?.length;
1493
- if (i !== undefined) {
1494
- // An override-covered length node answers through the node's
1495
- // reader-aware selection (serve: A17, the lane gate, A18
1496
- // supersession) with the backing's length as committed — the rule
1497
- // every index node takes through `get`, so `length` and indices
1498
- // resolve against ONE rule for this reader. The view composition
1499
- // below used to answer here and knows nothing of supersession
1500
- // (#3331): at a landing whose length differs from the optimistic
1501
- // frame, mapArray's tracked `length` (the get trap's node path) saw
1502
- // the staged truth while its untracked `slice` inside its owner saw
1503
- // the override, so `_items` came up short and the next pass keyed an
1504
- // undefined row (F5, optimistic-list-mutation-matrix `differ`).
1505
- if (getObserver() !== null) {
1506
- const e = read(i);
1507
- if (hasActiveOverride(i) && !authoritativeServe()) return e === FORCE ? r.length : e;
1508
- } else if (hasActiveOverride(i) && !authoritativeServe()) {
1509
- return nodeValue(i, r.length);
1510
- }
1511
- } else if (getObserver() !== null) {
1512
- read(getNode(e, t, n));
1513
- }
1514
- }
1515
- // Truth authors read the backing's own length — an optimistic row from
1516
- // the caller's transaction must not shift where the author's next write
1517
- // lands (#3108). A tentative draft that has seeded its backing from the
1518
- // view already carries the overrides in `src` (the draft arm every other
1519
- // channel gates on draftSeesOverrides — `get`, `has`, visibleKeys,
1520
- // snapshotWalk, #3665): composing them again put a slot the draft had
1521
- // just spliced out back on top of its shrunken backing, so `length` read
1522
- // one too long mid-splice and the second splice left a hole (F3,
1523
- // optimistic-list-mutation-matrix "move head->tail + move middle").
1524
- return (authoritativeServe() || inDraft(e) && !draftSeesOverrides(e) ? r : optHooks.optimisticView(e, r, inDraft(e))).length;
1525
- }
1526
- if (inDraft(e)) {
1527
- // Optimistic drafts before their first write have no pending backing yet;
1528
- // reads must still see the live optimistic view (compose, not clobber —
1529
- // #2951). Once ensurePB runs, the seeded clone carries the view.
1530
- // AUTHORITATIVE drafts (projection derive) never overlay — ensurePB's
1531
- // seeding rule, applied to the read side (#3108).
1532
- if (e.fam?.opt && draftSeesOverrides(e) && !authoritativeServe()) {
1533
- const n = e.n?.[t];
1534
- if (n !== undefined && hasActiveOverride(n)) f = unwrapOverride(n.o?.Fe);
1535
- }
1536
- } else {
1537
- // §7b: a lane value on the outer node SHADOWS read-through — an active
1538
- // override pierces the chained gate; otherwise chained backings always
1539
- // serve the live inner value.
1540
- if (getObserver() !== null) {
1541
- // First tracked read: create + link (the wrap-cache branch below
1542
- // populates px/pxv so read #2 skips wrapNext, slice 2). The value is
1543
- // served THROUGH the node from this read on — a node born under a held
1544
- // fold carries the hold (getNode, #3336), and the backing it was read
1545
- // from does not.
1546
- if (i === undefined) i = getNode(e, t, n, l);
1547
- // read()'s plain-signal fast path hoisted over the call (legacy trap
1548
- // parity): READ_SLOW = a global read window or non-plain node.
1549
- let r = readNodeFast(i);
1550
- if (r === READ_SLOW) r = read(i);
1551
- if (!o || hasActiveOverride(i)) f = r === FORCE ? n : r;
1552
- } else if (i !== undefined && (!o || hasActiveOverride(i))) {
1553
- f = nodeValue(i, n);
1554
- }
1555
- }
1556
- // Shallow stores serve data raw; store-proxy slots get boundary wrappers.
1557
- if (e.s) return serveShallow(e, t, f);
1558
- if (e.ch && !o && f !== null && typeof f === "object" && f[$TARGET] === undefined) f = resolveChainedRaw(e, t, f);
1559
- if (i !== undefined) {
1560
- // Wrap cache (see getNode): only wrappables are ever cached, so a hit
1561
- // skips isWrappable too — pointer-compare replaces both checks.
1562
- if (i.pxv === f && f !== undefined) return draftServe(e, i.px);
1563
- if (!isWrappable(f)) return f;
1564
- const n = wrapNext(f, e, t);
1565
- i.px = n;
1566
- i.pxv = f;
1567
- return draftServe(e, n);
1568
- }
1569
- if (!isWrappable(f)) return f;
1570
- return draftServe(e, wrapNext(f, e, t));
1571
- }
1572
-
1573
- /** §6c store-wide status gate for reads that DON'T flow through a node:
1574
- * untracked/raw fallthrough must still throw while the derive is
1575
- * uninitialized (seed invisibility, proj R23) or errored (memo parity).
1576
- * TRACKED reads never call this — store nodes carry `_firewall`, so core
1577
- * read() links the node and throws the firewall's error itself (the node
1578
- * link is what wakes async-memo readers when the landing writes values;
1579
- * the firewall link rides the same read). */ function firewallGate(e) {
1580
- // Own-draft ops are exempt: an async derive's continuation (generator body
1581
- // after an `await`/`yield`) runs OUTSIDE the sync write scope (inDraft is
1582
- // already false), but its draft-proxy traps mark every op with the write
1583
- // override. Those reads are the derive working its own draft (state.push
1584
- // reading .length) — gating them throws NotReadyError back into the derive
1585
- // itself, which the post-await read diagnostic (#2987) then escalates to a
1586
- // reactivity halt. The gate exists for EXTERNAL readers (seed invisibility,
1587
- // proj R23); the derive is the author.
1588
- if (projectionWriteActive || getWriteOverride()) return;
1589
- const t = e.fam?.node;
1590
- if (t != null && t.h & (STATUS_UNINITIALIZED | STATUS_ERROR)) read(t);
1591
- }
1592
-
1593
- /** latest() pull (#3075): bring the projection computed up to date so the
1594
- * read serves the IN-FLIGHT derivation — signal/memo parity, where core
1595
- * read() routes latest() through a companion that recomputes speculatively.
1596
- * The latest flag is suspended for the recompute (the derive's own reads
1597
- * are normal reads), and latestPullActive marks any adoption it commits as
1598
- * staged (see adoptPB) — the speculative swap must not leak to
1599
- * committed-visibility readers before the flush. */ function pullProjectionForLatest(e) {
1600
- const t = e.fam.node;
1601
- if (t == null) return;
1602
- const n = latestReadActive;
1603
- setLatestReadActive(false);
1604
- const r = latestPullActive;
1605
- latestPullActive = true;
1606
- try {
1607
- prepareComputed(t, true);
1608
- } finally {
1609
- latestPullActive = r;
1610
- setLatestReadActive(n);
1611
- }
1612
- }
1613
-
1614
- const traps = {
1615
- get(e, t, n) {
1616
- // One typeof gates every brand-symbol compare off the hot string path
1617
- // (four symbol comparisons per property read otherwise).
1618
- if (typeof t !== "string") {
1619
- if (t === $TARGET) return e;
1620
- if (t === $PROXY) return n;
1621
- if (t === $OWNER) return undefined;
1622
- // ownership stamp: never a user key
1623
- if (t === $RECORD) return undefined;
1624
- // a store is no view (see `viewOf`)
1625
- // refresh()/isPending resolve the projection computed through $REFRESH.
1626
- if (t === $REFRESH) return e.fam?.node ?? undefined;
1627
- if (t === $TRACK) {
1628
- if (pendingCheckActive) witnessAffectsMark(e, t);
1629
- if (e.fam !== null && getObserver() === null && !inDraft(e)) firewallGate(e);
1630
- if (!inDraft(e) && getObserver() !== null) {
1631
- read(getKeySetNode(e));
1632
- // Structural chaining (§7b, #2864 / core R21): a chained backing's
1633
- // $TRACK reads through to the INNER store's key-set — structural
1634
- // notifications land on the source's own node, never on this
1635
- // wrapper view's.
1636
- const t = readSource(e);
1637
- if (t[$TARGET] !== undefined) t[$TRACK];
1638
- }
1639
- return undefined;
1640
- }
1641
- // user symbols fall through to the generic path
1642
- }
1643
- if (pendingCheckActive) witnessAffectsMark(e, t);
1644
- if (e.fam !== null && getObserver() === null && !inDraft(e)) firewallGate(e);
1645
- // latest() pull (#3075): store traps never reach core read() without an
1646
- // observer, so bring the projection computed up to date here — signal/
1647
- // memo parity for latest() reads through a projection.
1648
- if (e.fam !== null && latestReadActive && !inDraft(e) && !getWriteOverride()) pullProjectionForLatest(e);
1649
- const r = readSource(e);
1650
- // Overlay delete (#3044): a prototype overlay cannot shadow a delete, so
1651
- // deleted keys are tracked aside and read as absent in the pending view.
1652
- if (e.del !== null && r === e.pb && e.del.has(t)) {
1653
- if (!inDraft(e) && getObserver() !== null) read(getNode(e, t, undefined));
1654
- return undefined;
1655
- }
1656
- // Hot inline case: existing PLAIN node (non-accessor), unchained backing,
1657
- // tracked read of a present data key — the dbmon/uibench effect re-read
1658
- // shape. Skips serveDataKey's frame, the FORCE compare (only accessor
1659
- // keys ever hold the sentinel), and isWrappable for primitives.
1660
- // ONE node-map lookup serves this block and the accessor probe below
1661
- // (nothing between them creates nodes).
1662
- const i = e.n?.[t];
1663
- if (e.ch === false && writeScopes === null) {
1664
- const n = i;
1665
- if (n !== undefined && n.acc !== true && getObserver() !== null) {
1666
- let r = readNodeFast(n);
1667
- if (r === READ_SLOW) r = read(n);
1668
- if (r === null || typeof r !== "object") return r;
1669
- if (e.s) return serveShallow(e, t, r);
1670
- if (n.pxv === r) return n.px;
1671
- if (isWrappable(r)) {
1672
- const i = wrapNext(r, e, t);
1673
- n.px = i;
1674
- n.pxv = r;
1675
- return i;
1676
- }
1677
- return r;
1678
- }
1679
- }
1680
- // Accessor keys serve through Reflect.get with the PROXY receiver
1681
- // (R20/R29: internal reads track; the node is linked for shape-change
1682
- // notification but its value is never served). Accessor-ness comes from
1683
- // the node's cached flag; the first TRACKED read (which creates the
1684
- // node) probes once — untracked node-less reads take the plain path,
1685
- // where a raw-receiver getter still returns correct committed values.
1686
- // Tracking suppression is PER-TARGET (inDraft), never global: `writing`
1687
- // counts every open setter anywhere, and a projection derive runs its
1688
- // whole body inside one — a global gate silently swallowed EXTERNAL
1689
- // absent-key/accessor subscriptions for every store read during any
1690
- // derive, leaving nested projections permanently dependency-less when
1691
- // their sources hadn't materialized yet (#3037).
1692
- // First-read dedupe (create-floor slice 2): remember the probe verdict —
1693
- // node creation downstream reuses it instead of re-scanning the
1694
- // descriptor, but only when the probed object IS the one getNode would
1695
- // scan (pb ?? v).
1696
- let l = -1;
1697
- {
1698
- let o;
1699
- if (i !== undefined) o = i.acc === true; else if (!inDraft(e) && getObserver() !== null) {
1700
- o = isOwnAccessor(r, t);
1701
- if (r === (e.pb ?? e.v)) l = o ? 1 : 0;
1702
- } else o = false;
1703
- if (o) {
1704
- if (!inDraft(e) && getObserver() !== null) read(i ?? getNode(e, t, undefined, l));
1705
- const o = Reflect.get(r, t, n);
1706
- if (e.s) return serveShallow(e, t, o);
1707
- return isWrappable(o) ? draftServe(e, wrapNext(o, e, t)) : o;
1708
- }
1709
- }
1710
- // Plain-data fast path: no descriptor allocation per read.
1711
- // Inherited pollution keys are never served (core R30) — checked before
1712
- // the proto-function branch can leak `constructor`. Interned-string
1713
- // compares beat a Set hash on this per-read path. Overlay pending
1714
- // backings chain to the committed backing, so "own in the view" means
1715
- // own on either layer (ownInView) — a genuine prototype method is one
1716
- // that is own on NEITHER.
1717
- const o = e.ovl && r === e.pb;
1718
- if ((t === "constructor" || t === "__proto__" || t === "prototype") && !hasOwn.call(r, t) && !(o && hasOwn.call(e.v, t))) return undefined;
1719
- let f = r[t];
1720
- if (f === undefined ? !hasOwn.call(r, t) && !(o && hasOwn.call(e.v, t)) : false) {
1721
- // Inherited: prototype getters/methods run with the proxy receiver.
1722
- f = Reflect.get(r, t, n);
1723
- if (typeof f === "function") return f;
1724
- // proto methods untracked
1725
- // Reading a currently-absent own key subscribes to it (R12) — for any
1726
- // target OUTSIDE its own draft scope, even mid-setter (#3037, above).
1727
- if (f === undefined && !inDraft(e)) {
1728
- if (getObserver() !== null) read(getNode(e, t, undefined, l));
1729
- const n = e.n?.[t];
1730
- if (n) {
1731
- const r = nodeValue(n, undefined);
1732
- if (e.s) return serveShallow(e, t, r);
1733
- return isWrappable(r) ? draftServe(e, wrapNext(r, e, t)) : r;
1734
- }
1735
- } else if (f === undefined && inDraft(e) && e.fam?.opt && draftSeesOverrides(e) &&
1736
- // AUTHORITATIVE drafts (landing folds) never seed from overrides —
1737
- // the caller's optimism is not truth (has-trap twin below).
1738
- !authoritativeServe()) {
1739
- const n = e.n?.[t];
1740
- // The draft is a WRITER channel (hasActiveOverride's rule): it
1741
- // composes on the tick's own unflushed adds, as the has trap's draft
1742
- // arm, visibleKeys and optimisticView do. Reader-gating this arm
1743
- // (visibleOverride) made a second setter in the same action read
1744
- // `undefined` where the first had pushed a row (#3665, rc.9).
1745
- if (n !== undefined && hasActiveOverride(n)) f = unwrapOverride(n.o?.Fe);
1746
- }
1747
- if (e.s) return serveShallow(e, t, f);
1748
- return isWrappable(f) ? draftServe(e, wrapNext(f, e, t)) : f;
1749
- }
1750
- if (typeof f === "function" && !hasOwn.call(r, t) && !(o && hasOwn.call(e.v, t))) return f;
1751
- // proto method
1752
- return serveDataKey(e, t, f, r, i, l);
1753
- },
1754
- has(e, t) {
1755
- if (t === $TARGET || t === $PROXY || t === $TRACK) return true;
1756
- if (t === $OWNER || t === $RECORD) return false;
1757
- if (pendingCheckActive) witnessAffectsMark(e, t);
1758
- if (e.fam !== null && getObserver() === null && !inDraft(e)) firewallGate(e);
1759
- const n = readSource(e);
1760
- let r = t in n;
1761
- // Overlay deletes read as absent in the pending view (#3044).
1762
- if (r && e.del !== null && n === e.pb && e.del.has(t)) r = false;
1763
- if (!inDraft(e)) {
1764
- if (getObserver() !== null) {
1765
- const n = getHasNode(e, t, r);
1766
- // Authoritative-view readers get the right answer for free: core read()
1767
- // skips the override arm for them, so nv is authoritative presence.
1768
- const i = read(n);
1769
- if (hasActiveOverride(n)) r = !!i;
1770
- } else if (!authoritativeServe()) {
1771
- const n = e.h?.[t];
1772
- // The get trap's untracked selection (nodeValue → serve), so `in`
1773
- // agrees with the tracked branch above and with `get`: a presence
1774
- // override the landing superseded (#3331 — an optimistic delete
1775
- // whose slot the truth refilled) answers the staged truth for a
1776
- // deriving reader. Reading the override directly said "absent" while
1777
- // `get` served the landed row, and HasProperty-driven copies (slice,
1778
- // spread, map) left a hole (F5 `differ: delete *`).
1779
- if (n !== undefined && hasActiveOverride(n)) r = !!nodeValue(n, r);
1780
- }
1781
- } else if (e.fam?.opt && draftSeesOverrides(e) && !authoritativeServe()) {
1782
- const n = e.h?.[t];
1783
- if (n !== undefined && hasActiveOverride(n)) r = !!unwrapOverride(n.o?.Fe);
1784
- }
1785
- return r;
1786
- },
1787
- ownKeys(e) {
1788
- if (pendingCheckActive) witnessAffectsMark(e);
1789
- if (e.fam !== null && getObserver() === null && !inDraft(e)) firewallGate(e);
1790
- if (!inDraft(e) && getObserver() !== null) read(getKeySetNode(e));
1791
- return visibleKeys(e, readSource(e));
1792
- },
1793
- getOwnPropertyDescriptor(e, t) {
1794
- if (t === $OWNER || t === $RECORD) return undefined;
1795
- // A descriptor read is a PRESENCE read: it subscribes to the key's
1796
- // presence node and witnesses affects()/isPending() exactly as `in` does
1797
- // (structural oracle, 2026-09-17 — the trap read no node before, so a
1798
- // render effect inspecting a key through getOwnPropertyDescriptor never
1799
- // re-ran for an optimistic add or delete, and an isPending() probe over
1800
- // it witnessed nothing). The value it reports rides the value node's
1801
- // view through visibleDescriptor.
1802
-
1803
- // EXCEPT for an enumerator (#3664): Object.keys / for...in / spread /
1804
- // Object.entries / JSON.stringify take this trap once per key right
1805
- // after `ownKeys`, which already subscribed the observer to the key-set
1806
- // node — and that node bumps on every membership change, committed or
1807
- // optimistic, so a presence node per key adds nothing the enumerator
1808
- // can observe (rc.9 birthed one per key per object: ~640 B and a graph
1809
- // node each, 10x the memory of a 30-key row's reader). When the
1810
- // observer holds the key-set node in THIS pass, skip the presence read;
1811
- // a lone descriptor read keeps its per-key precision.
1812
- if (pendingCheckActive) witnessAffectsMark(e, t);
1813
- const n = getObserver();
1814
- if (e.fam !== null && n === null && !inDraft(e)) firewallGate(e);
1815
- const r = readSource(e);
1816
- const i = visibleDescriptor(e, r, t);
1817
- if (!inDraft(e) && n !== null && !observerHoldsKeySet(e, n)) {
1818
- // The node is born from the source's presence (as `has` births it),
1819
- // not the override-adjusted answer.
1820
- let n = t in r;
1821
- if (n && e.del !== null && r === e.pb && e.del.has(t)) n = false;
1822
- read(getHasNode(e, t, n));
1823
- }
1824
- if (i === undefined) return undefined;
1825
- // Array targets carry a real non-configurable `length` the proxy
1826
- // invariant forces us to report faithfully; everything else reports
1827
- // configurable via target indirection (core R51).
1828
- if (!(t === "length" && Array.isArray(e))) i.configurable = true;
1829
- return i;
1830
- },
1831
- set(e, t, n) {
1832
- // Writes require the target's draft scope OR the projection write
1833
- // override (post-await async draft writes arrive outside any window);
1834
- // everything else is silently ignored (R23).
1835
- const r = inDraft(e);
1836
- const i = !r && getWriteOverride();
1837
- if (!r && !i) return true;
1838
- if (t === "__proto__") return true;
1839
- // pollution guard (core R30)
1840
- // Unwrap BEFORE ensurePB: unwrapValue materializes a self-referencing
1841
- // draft's overlay (replacing target.pb), so a pb local captured earlier
1842
- // would be the abandoned overlay and the write would vanish.
1843
- // Shallow slots store what was written VERBATIM — another store's proxy
1844
- // passes through by reference (#2932; markRawOne skips proxies), while
1845
- // deep stores unwrap to raw backings.
1846
- const l = e.s ? n : unwrapValue(n);
1847
- const o = ensurePB(e);
1848
- pendingNotify.add(e);
1849
- // Array length writes implicitly delete indices — the written-keys bound
1850
- // can't see them, so poison to the full scan for this batch. Index
1851
- // writes implicitly GROW length, so arrays always record it alongside.
1852
- if (Array.isArray(o)) {
1853
- if (t === "length") e.wk = WK_ALL; else if (e.wk !== WK_ALL) {
1854
- const n = e.wk ??= new Set;
1855
- n.add(t);
1856
- n.add("length");
1857
- }
1858
- } else {
1859
- if (e.wk !== WK_ALL) (e.wk ??= new Set).add(t);
1860
- // Live own-key estimate for the overlay/clone choice (#3360): `in`
1861
- // sees through an overlay to the committed keys, so this counts keys
1862
- // NEW to the container. Deletes are not un-counted (a stale high
1863
- // count only picks the overlay a little early).
1864
- if (!(t in o)) e.kc++;
1865
- }
1866
- // Own data keys literally named "prototype"/"constructor" land as data —
1867
- // defineProperty sidesteps a proto-chain setter named the same.
1868
- if (UNSAFE_KEYS.has(t)) {
1869
- Object.defineProperty(o, t, {
1870
- value: l,
1871
- writable: true,
1872
- enumerable: true,
1873
- configurable: true
1874
- });
1875
- if (e.del !== null) e.del.delete(t);
1876
- return true;
1877
- }
1878
- // Overlay first-write DEFINES the own key: assignment through the proto
1879
- // chain would reject on a non-writable committed property (the clone
1880
- // path normalized descriptors for exactly this — R51 parity). A
1881
- // plain-data-graded backing (sc 2, owned: every slot writable) takes the
1882
- // bare assignment — it lands as an own key on the overlay all the same.
1883
- if (e.ovl && e.sc !== 2 && !hasOwn.call(o, t)) {
1884
- Object.defineProperty(o, t, {
1885
- value: l,
1886
- writable: true,
1887
- enumerable: true,
1888
- configurable: true
1889
- });
1890
- } else o[t] = l;
1891
- if (e.del !== null) e.del.delete(t);
1892
- // Shallow ingest: written records are sticky raw-marked (one entity is
1893
- // never both deep-wrapped and raw — R41/#2932, shared invariant).
1894
- if (e.s && l !== null && typeof l === "object") markRawOne(l);
1895
- // Override-mode (post-await draft) writes have no setter exit — notify
1896
- // per-op (setSignal equality-gates repeats).
1897
- if (i) notifyWrites(e);
1898
- return true;
1899
- },
1900
- defineProperty(e, t, n) {
1901
- const r = inDraft(e);
1902
- const i = !r && getWriteOverride();
1903
- if (!r && !i) return true;
1904
- if (t === "__proto__") return true;
1905
- if (n.get || n.set) e.a = true;
1906
- // Unwrap before ensurePB (see the set trap: self-reference materializes).
1907
- if ("value" in n) n = {
1908
- ...n,
1909
- value: unwrapValue(n.value)
1910
- };
1911
- const l = ensurePB(e);
1912
- // A non-default data descriptor (or an accessor) leaves the plain-data
1913
- // grade: the key reaches the committed backing as defined, so the spread
1914
- // clone and bare-assignment paths no longer describe it.
1915
- if (e.a || !(n.enumerable && n.writable && n.configurable)) e.sc = 1;
1916
- pendingNotify.add(e);
1917
- if (e.wk !== WK_ALL) (e.wk ??= new Set).add(t);
1918
- Object.defineProperty(l, t, n);
1919
- if (e.del !== null) e.del.delete(t);
1920
- if (i) notifyWrites(e);
1921
- return true;
1922
- },
1923
- deleteProperty(e, t) {
1924
- const n = inDraft(e);
1925
- const r = !n && getWriteOverride();
1926
- if (!n && !r) return true;
1927
- const i = ensurePB(e);
1928
- pendingNotify.add(e);
1929
- if (e.wk !== WK_ALL) (e.wk ??= new Set).add(t);
1930
- delete i[t];
1931
- // A prototype overlay cannot shadow a delete of a committed key —
1932
- // record it aside (#3044); reads/has/ownKeys/commit consult the set.
1933
- if (e.ovl && hasOwn.call(e.v, t)) (e.del ??= new Set).add(t);
1934
- if (r) notifyWrites(e);
1935
- return true;
1936
- }
1937
- };
1938
-
1939
- /** Low-level setter primitive: opens write mode on a next proxy, runs `fn`,
1940
- * emits write-time notifications at outermost exit, applies returned
1941
- * replacements as adoptions. `guard=false` skips the dev guards (owned-scope
1942
- * write, thenable result) — projection recomputes legitimately write from
1943
- * inside their computed, and their async derive is handled by the recompute,
1944
- * not returned through here. */
1945
- /** The derived store (`createStore(fn)`) whose user setter is running, and
1946
- * whether one of its leaf notifications hit a staging another transaction
1947
- * holds as the fold's result (A34 amendment, #3612; core heldDerivation). */ let derivedSetter = null;
1948
-
1949
- let heldDerivationHit = false;
1950
-
1951
- /** The derived store's setter (CS-R31): within a synchronous frame the manual
1952
- * write wins — the projection's recompute is masked for the tick. Across a
1953
- * hold the write is not a proposal: a leaf the fold staged under another
1954
- * transaction re-runs the fold under it, the write being the draft's prior
1955
- * state (#3612). Decided from the leaf notifications, so the mask lands
1956
- * after them (in the `finally`: a throwing setter's writes before the throw
1957
- * were notified, and are masked as before). */ function derivedStoreWrite(e, t, n) {
1958
- derivedSetter = e;
1959
- heldDerivationHit = false;
1960
- try {
1961
- storeSetterNext(t, n);
1962
- } finally {
1963
- derivedSetter = null;
1964
- heldDerivationHit ? rederiveHeld(e) : suppressComputedRecompute(e);
1965
- }
1966
- }
1967
-
1968
- function storeSetterNext(e, t, n = true) {
1969
- const r = e[$TARGET];
1970
- const i = writeScopes;
1971
- writeScopes = new Set;
1972
- writeScopes.add(scopeKey(r));
1973
- writing++;
1974
- let l;
1975
- try {
1976
- // No untrack: the writing flag already disables store-node linking
1977
- // (draft reads never self-track, proj R2), while EXTERNAL reads (signals
1978
- // inside a projection derive) must keep tracking — they are the derive's
1979
- // dependencies.
1980
- l = t(e);
1981
- } finally {
1982
- writing--;
1983
- writeScopes = i;
1984
- // Outermost setter exit: emit write-time notifications (setSignal per
1985
- // changed observed key) so transition holds and lanes engage now.
1986
- if (writing === 0 && pendingNotify.size) {
1987
- const e = [ ...pendingNotify ];
1988
- pendingNotify.clear();
1989
- for (const t of e) notifyWrites(t);
1990
- }
1991
- }
1992
- if (l !== undefined && l !== e && isWrappable(l)) {
1993
- // Returned replacement: on an optimistic family (outside authoritative
1994
- // writes) the replacement is itself an optimistic edit — diff it against
1995
- // the visible view as engine writes (reverts at settle). Otherwise it is
1996
- // an adoption of the incoming object (unowned).
1997
- if (r.fam?.opt && !projectionWriteActive && !getWriteOverride()) {
1998
- optHooks.notifyOptimisticWrites(r, unwrapValue(l));
1999
- } else {
2000
- adoptPB(r, unwrapValue(l));
2001
- }
2002
- }
2003
- if (writing === 0 && heldAdoptions.size) stageHeldAdoptions();
2004
- }
2005
-
2006
- // Affects integration: the legacy affects machinery reads next targets
2007
- // structurally (aliased field names); only node CREATION dispatches here.
2008
- setNextAffectsNodeResolver((e, t) => t === $AFFECTS ? getNode(e, $AFFECTS, undefined) : getNode(e, t, (e.pb ?? e.v)[t]));
2009
-
2010
- function createStoreNext(e, t = false) {
2011
- const n = wrapNext(e);
2012
- if (t) {
2013
- n[$TARGET].s = true;
2014
- markRawIngest(e);
2015
- }
2016
- const setter = e => storeSetterNext(n, e);
2017
- return [ n, setter ];
2018
- }
2019
-
2020
- // ---------------------------------------------------------------------------
2021
- // snapshot (next targets): the backing IS the plain raw graph — zero copy.
2022
- // Sees pending (R27) by reading pb. Chained/owned-copy caching lands with the
2023
- // utilities increment; this covers the createStore-suite contract.
2024
- /** True when `proxy` is a SHALLOW store (children served verbatim, slots
2025
- * replaced by reference — #2932). The list driver uses this to choose the
2026
- * slot-patch channel (collected row bodies) over per-record registration. */ function storeIsShallow(e) {
2027
- const t = e?.[$TARGET];
2028
- return t !== undefined && t.s === true;
2029
- }
2030
-
2031
- /** True when `proxy` belongs to a projection/optimistic FAMILY. The list
2032
- * driver must DECLINE family arrays (external audit finding): family
2033
- * structural changes never emit row/slot ops (the setter channel is
2034
- * fam-gated; optimistic writes ride node overrides), and the proxy identity
2035
- * is stable so the each-watch cannot catch the change either — an engaged
2036
- * list would freeze on optimistic/projection structural updates. Record-
2037
- * level family patches are unaffected (they have their own emission). */ function storeHasFamily(e) {
2038
- const t = e?.[$TARGET];
2039
- return t !== undefined && t.fam !== null;
2040
- }
2041
-
2042
- /** True when `proxy` belongs to an OPTIMISTIC family specifically. The list
2043
- * driver declines these (audit finding, narrowed): optimistic user writes
2044
- * ride node-level overrides — they never enter the reconcile walk, so no
2045
- * row/slot ops are emitted and an engaged list would freeze on optimistic
2046
- * structural changes. PROJECTION (non-optimistic) families are drivable:
2047
- * their recomputes go through the reconcile walk, whose emissions are
2048
- * transition-stamped in the apply queue like any other (equivalence-matrix
2049
- * gated). Re-admitting optimistic families requires a lane-timed structural
2050
- * emission mirroring emitPatchOptimistic, plus revert resync. */ function storeHasOptimisticFamily(e) {
2051
- const t = e?.[$TARGET];
2052
- return t !== undefined && t.fam?.opt === true;
2053
- }
2054
-
2055
- // ---------------------------------------------------------------------------
2056
- // visibility: what a reader sees on a record — ONE rule for the ownKeys /
2057
- // getOwnPropertyDescriptor traps and the deep() walk (#3323). The walk used
2058
- // to re-derive the trap rules over raw backings and missed each new one in
2059
- // turn (the #3044 overlay merge, then #3323's chain, then optimistic presence
2060
- // and value overrides); sharing the body makes parity structural.
2061
- /** Keys visible on `target` served from `src` (= readSource(target)): the
2062
- * pending-overlay merge (#3044) and, on optimistic families, presence-node
2063
- * overrides (§6, FINDING-2's fix). Draft reads before the first write overlay
2064
- * too (pb, once created, is seeded with the view). Authoritative-view reads
2065
- * (until()'s predicate, truth-author drafts) skip the overlay. */ function visibleKeys(e, t) {
2066
- let n;
2067
- if (e.ovl && t === e.pb) {
2068
- // Overlay merge (#3044): committed keys in their order, then this
2069
- // batch's NEW keys, minus deletes.
2070
- n = Reflect.ownKeys(e.v);
2071
- const r = e.del;
2072
- if (r !== null && r.size !== 0) n = n.filter(e => !r.has(e));
2073
- for (const r of Reflect.ownKeys(t)) {
2074
- if (!hasOwn.call(e.v, r)) n.push(r);
2075
- }
2076
- } else n = Reflect.ownKeys(t);
2077
- // Drop the $OWNER stamp (owned backings carry it as an own enumerable
2078
- // symbol). Symbols enumerate last, so the scan stops at the first string.
2079
- for (let e = n.length - 1; e >= 0 && typeof n[e] === "symbol"; e--) {
2080
- if (n[e] === $OWNER) {
2081
- n.splice(e, 1);
2082
- break;
2083
- }
2084
- }
2085
- if (!authoritativeServe() && e.fam?.opt && e.h !== null && (!inDraft(e) || draftSeesOverrides(e))) {
2086
- let t = null;
2087
- const r = inDraft(e);
2088
- for (const i of Reflect.ownKeys(e.h)) {
2089
- const l = e.h[i];
2090
- if (!(r ? hasActiveOverride(l) : visibleOverride(l))) continue;
2091
- t ??= new Set(n);
2092
- // Reader arm: a superseded presence override answers as `in` does
2093
- // (readerOverride, #3331) — Object.keys listed one row fewer than the
2094
- // traps served after a landing refilled an optimistically deleted slot.
2095
- const o = r ? unwrapOverride(l.o?.Fe) : readerOverride(l, t.has(i));
2096
- if (o) t.add(i); else t.delete(i);
2097
- }
2098
- if (t !== null) return [ ...t ];
2099
- }
2100
- return n;
2101
- }
2102
-
2103
- /** The data/accessor descriptor visible for `key` on `target` served from
2104
- * `src`: overlay (#3044 — unwritten keys live on the committed backing,
2105
- * deleted keys are absent) and optimistic presence overrides (an opt delete
2106
- * hides the key; an opt add synthesizes a data descriptor from the value
2107
- * node). `configurable` is the trap's concern (proxy invariant). */ function visibleDescriptor(e, t, n) {
2108
- let r = Object.getOwnPropertyDescriptor(t, n);
2109
- if (e.ovl && t === e.pb) {
2110
- if (e.del !== null && e.del.has(n)) return undefined;
2111
- if (r === undefined) r = Object.getOwnPropertyDescriptor(e.v, n);
2112
- }
2113
- // Draft arm, the twin of visibleKeys' (#3665): a draft still composing on
2114
- // the live view (no view-seeded backing yet) serves the writer rule
2115
- // (hasActiveOverride); once ensurePB has seeded the draft's own backing,
2116
- // `src` carries the view. Without it ownKeys listed a key the descriptor
2117
- // reported absent, and every enumerator — Object.keys, spread, entries,
2118
- // JSON.stringify, deep() — dropped the row a previous setter had added.
2119
- const i = inDraft(e);
2120
- if (!authoritativeServe() && e.fam?.opt && (!i || draftSeesOverrides(e))) {
2121
- const t = e.h?.[n];
2122
- if (t !== undefined && (i ? hasActiveOverride(t) : visibleOverride(t))) {
2123
- // Reader arm: presence as `in` serves it (readerOverride, #3331).
2124
- const l = i ? unwrapOverride(t.o?.Fe) : readerOverride(t, r !== undefined);
2125
- if (!l) return undefined;
2126
- // opt delete
2127
- if (r === undefined) {
2128
- const t = e.n?.[n];
2129
- return {
2130
- // In a draft the value is the override itself (nodeValue routes
2131
- // through serve — the reader rule — and would answer undefined).
2132
- value: t === undefined ? undefined : i ? hasActiveOverride(t) ? unwrapOverride(t.o?.Fe) : undefined : nodeValue(t, undefined),
2133
- writable: true,
2134
- enumerable: true,
2135
- configurable: true
2136
- };
2137
- }
2138
- }
2139
- }
2140
- return r;
2141
- }
2142
-
2143
- /** Tracking deep snapshot (`deep()` for next targets): subscribes to the
2144
- * key-set and deep-witness node at every reachable level, then returns the
2145
- * plain view. Shared references and cycles handled via the visited set. */ function deepNext(e) {
2146
- const t = e?.[$TARGET];
2147
- if (t === undefined || t.px !== e) return e;
2148
- const n = new Set;
2149
- // One membership node + one deep-witness node PER RECORD (legacy $TRACK
2150
- // parity): the walk stays O(records) in subscriptions instead of O(paths)
2151
- // in per-key nodes, and it walks TARGETS directly — no per-child proxy
2152
- // round-trip (wrapNext → proxy → $TARGET trap) on the re-walk every
2153
- // effect run performs.
2154
- // Resolve `child` (a raw or a stored proxy found under `t`) to the target
2155
- // that `t`'s family serves for it — created on first visit, as the get trap
2156
- // would. Undefined = leaf (unwrappable or raw-marked).
2157
- const childTarget = (e, t, n) => {
2158
- let r = lookupTarget(t, e.fam);
2159
- if (r === undefined) {
2160
- if (!isWrappable(t)) return undefined;
2161
- wrapNext(t, e, n);
2162
- // Stored proxies (chained slots) that this family passes through
2163
- // resolve to their own target.
2164
- r = lookupTarget(t, e.fam) ?? t[$TARGET];
2165
- }
2166
- return r;
2167
- };
2168
- const walkT = e => {
2169
- const t = readSource(e);
2170
- if (n.has(t)) return;
2171
- n.add(t);
2172
- read(getKeySetNode(e));
2173
- read(getDeepNode(e));
2174
- // Chained backing (§7b): the committed backing IS another store's proxy.
2175
- // Writes to the inner store bump the INNER record's witnesses; this
2176
- // target's k/dk hear only its own family's writes (optimistic overrides).
2177
- // Read through the whole chain — the $TRACK trap's rule (#2864 / R21)
2178
- // applied to deep() (#3323). From `t.v`, not `src`: a pending backing is a
2179
- // raw clone and would hide the chain. Non-chained targets pay one flag.
2180
- for (let t = e; t.ch; ) {
2181
- t = t.v[$TARGET];
2182
- read(getKeySetNode(t));
2183
- read(getDeepNode(t));
2184
- }
2185
- // Keys and children exactly as the traps serve them — the overlay merge
2186
- // (#3044/#3283: a bare ownKeys over a pending backing mid-flush dropped
2187
- // every untouched child from the re-subscribing effect's dependencies)
2188
- // and optimistic presence overrides (a row added under a held action
2189
- // lives in `h`/`n`, not the committed backing; the walk never reached its
2190
- // record, so deep() was deaf to every write on it until settle).
2191
- const r = e.fam?.opt === true;
2192
- for (const n of visibleKeys(e, t)) {
2193
- const i = visibleDescriptor(e, t, n);
2194
- if (i === undefined) continue;
2195
- if (i.get || i.set) {
2196
- e.a = true;
2197
- continue;
2198
- // accessors track through their own reads when invoked
2199
- }
2200
- let l = i.value;
2201
- // An active value override on an optimistic node shadows the backing's
2202
- // child (an optimistic replacement `d[i] = {...}`) — follow what reads
2203
- // serve, as nodeValue's leading arm does.
2204
- if (r) {
2205
- const t = e.n?.[n];
2206
- if (t !== undefined && hasActiveOverride(t) && !authoritativeServe()) l = unwrapOverride(t.o.Fe);
2207
- }
2208
- if (l === null || typeof l !== "object") continue;
2209
- // Through a chain the descriptor yields the INNERMOST raw: resolve it to
2210
- // the inner family's proxy so this level wraps the chained `view[i]` the
2211
- // get trap serves, never a fresh non-chained wrapper of the base raw.
2212
- if (e.ch && l[$TARGET] === undefined) l = resolveChainedRaw(e, n, l);
2213
- const o = childTarget(e, l, n);
2214
- if (o === undefined) continue;
2215
- // raw-marked: leaf by contract
2216
- walkT(o);
2217
- }
2218
- };
2219
- walkT(t);
2220
- return snapshotNext(e);
2221
- }
2222
-
2223
- /**
2224
- * Snapshot with per-object registration resolution (RUL-12 DAG ruling): every
2225
- * reachable wrappable resolves through its target's CURRENT backing, so
2226
- * privatized subtrees are seen through any parent path. Identity-preserving:
2227
- * a subtree with no substitutions below returns its own object (zero copy for
2228
- * settled, never-diverged graphs).
2229
- */ function snapshotNext(e) {
2230
- const t = e?.[$TARGET];
2231
- return snapshotWalk(e, new Map, t?.fam ?? null);
2232
- }
2233
-
2234
- function snapshotWalk(e, t, n) {
2235
- if (e === null || typeof e !== "object") return e;
2236
- // Resolve through the registration: proxies AND raws map to their target's
2237
- // current backing (stale raw pointers through other parents resolve here).
2238
- // Loops for chained backings (§7b: a projection's backing can be another
2239
- // store's proxy — snapshot unwraps to the base raw).
2240
- let r = e;
2241
- // Chained backings can pass through several targets; optimistic overrides
2242
- // on OUTER targets shadow the chain (§7b), so collect every opt target
2243
- // encountered and compose their views over the resolved base, innermost
2244
- // outward.
2245
- let i = null;
2246
- for (let e = true; ;e = false) {
2247
- const t = r?.[$TARGET]?.v !== undefined;
2248
- let l = t ? r[$TARGET] : undefined;
2249
- if (l === undefined && n !== null) l = lookupTarget(r, n);
2250
- if (l === undefined) l = lookupTarget(r, null);
2251
- if (l === undefined) break;
2252
- // Entering a level from a RAW below a chained family: the raw resolves to
2253
- // the INNER store's target, but this family's wrapper for it — keyed by
2254
- // the inner proxy, §7b — is where the outer overrides live. Start from
2255
- // the wrapper so they compose (#3323: a nested optimistic write on a view
2256
- // row was served by reads but missing from deep()/snapshot()). Entry only:
2257
- // the descent then runs wrapper → inner → raw and terminates.
2258
- if (e && !t && n !== null && l.fam !== n) {
2259
- const e = n.map.get(l.px);
2260
- if (e !== undefined) l = e;
2261
- }
2262
- if (l.fam !== null) n = l.fam;
2263
- if (l.fam?.opt === true) (i ??= []).push(l);
2264
- // The shared visibility decision (#3147): the speculative peek serves
2265
- // pending staging, but a HELD landing is masked to committed exactly as
2266
- // it is for per-key readers — the two families must answer alike while
2267
- // a transaction holds store landings.
2268
- const o = pendingBackingVisible(l, true);
2269
- // Snapshot runs mid-flush (tracked memos execute before commit), so a
2270
- // pending prototype overlay must present as a REAL merged container.
2271
- if (o && l.ovl) materializePB(l);
2272
- const f = o ? l.pb : l.v;
2273
- if (f === r) break;
2274
- r = f;
2275
- }
2276
- if (!isWrappable(r)) return r;
2277
- // Optimistic families: compose the visible view; a composed view is a fresh
2278
- // object and snapshots via the owned/copy path (pinned `not.toBe` identity).
2279
- if (i !== null) {
2280
- let e = r;
2281
- for (let t = i.length - 1; t >= 0; t--) {
2282
- const n = i[t];
2283
- // Draft twin (#3665): deep()/snapshot() inside a setter is the writer's
2284
- // channel and composes on the tick's own unflushed adds
2285
- // (hasActiveOverride). A draft that has seeded its own backing from
2286
- // the view already carries them in `src` — composing again would
2287
- // clobber the draft's later writes with the overrides they superseded.
2288
- const r = inDraft(n);
2289
- if (r && !draftSeesOverrides(n)) continue;
2290
- e = optHooks.optimisticView(n, e, r);
2291
- }
2292
- if (e !== r) {
2293
- const i = t.get(r);
2294
- if (i !== undefined) return i;
2295
- const l = Array.isArray(e);
2296
- const o = l ? [] : Object.create(Object.getPrototypeOf(e));
2297
- t.set(r, o);
2298
- for (const r of Reflect.ownKeys(e)) {
2299
- if (l && r === "length" || r === $OWNER) continue;
2300
- const i = e[r];
2301
- o[r] = i !== null && typeof i === "object" ? snapshotWalk(i, t, n) : i;
2302
- }
2303
- if (l) o.length = e.length;
2304
- return o;
2305
- }
2306
- }
2307
- const l = t.get(r);
2308
- if (l !== undefined) return l;
2309
- // OWNED (written) subtrees snapshot as copies (§7b: identity is only for
2310
- // subtrees "unmodified relative to source"): non-enumerable symbols are
2311
- // excluded (recon-snap R29), and the copy registers BEFORE descent so
2312
- // cycles keep identity (FINDING-3).
2313
- if (isOwned(r)) {
2314
- const e = Array.isArray(r);
2315
- const i = e ? [] : Object.create(Object.getPrototypeOf(r));
2316
- t.set(r, i);
2317
- for (const l of Reflect.ownKeys(r)) {
2318
- if (e && l === "length" || l === $OWNER) continue;
2319
- const o = Object.getOwnPropertyDescriptor(r, l);
2320
- if (typeof l === "symbol" && !o.enumerable) continue;
2321
- if (o.get || o.set) {
2322
- Object.defineProperty(i, l, o);
2323
- continue;
2324
- }
2325
- const f = o.value;
2326
- const s = f !== null && typeof f === "object" ? snapshotWalk(f, t, n) : f;
2327
- if (o.enumerable && o.writable && o.configurable) i[l] = s; else Object.defineProperty(i, l, {
2328
- ...o,
2329
- value: s
2330
- });
2331
- }
2332
- if (e && i.length !== r.length) i.length = r.length;
2333
- return i;
2334
- }
2335
- // UNOWNED (shared/user) subtrees keep identity unless a descendant
2336
- // substituted; copy-on-substitution preserves the documented CoW contract.
2337
- t.set(r, r);
2338
- let o = null;
2339
- for (const e of Reflect.ownKeys(r)) {
2340
- const i = Object.getOwnPropertyDescriptor(r, e);
2341
- if (!i || i.get || i.set) continue;
2342
- const l = i.value;
2343
- if (l === null || typeof l !== "object") continue;
2344
- const f = snapshotWalk(l, t, n);
2345
- if (f !== l) {
2346
- if (o === null) {
2347
- o = Array.isArray(r) ? [ ...r ] : Object.create(Object.getPrototypeOf(r), Object.getOwnPropertyDescriptors(r));
2348
- t.set(r, o);
2349
- }
2350
- o[e] = f;
2351
- }
2352
- }
2353
- return o ?? r;
2354
- }
2355
-
2356
- export { adoptPB, arrayStructureChanged, authoritativeRead, authoritativeServe, bumpDeep, createStoreNext, deepNext, derivedStoreWrite, getHasNode, getKeySetNode, getNode, hasAccessorFlag, hasActiveOverride, heldMaskView, materializePB, membershipChanged, notifyFold, notifyFoldTail, notifyKeyDiff, notifyKeyValue, readerOverride, runAuthoritative, snapshotNext, stagedTruthPB, storeHasFamily, storeHasOptimisticFamily, storeIsShallow, storeSetterNext, targetsEqual, unwrapValue, visibleOverride, wrapNext };