@solidjs/signals 2.0.0-rc.2 → 2.0.0-rc.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/dist/dev-shared.js +6505 -0
  2. package/dist/dev.attribution.js +3484 -0
  3. package/dist/dev.js +10227 -9508
  4. package/dist/observe/affects-marks.js +86 -0
  5. package/dist/observe/affects.js +112 -0
  6. package/dist/observe/attribution.js +7 -0
  7. package/dist/observe/boundaries.js +696 -0
  8. package/dist/observe/core/action.js +182 -0
  9. package/dist/observe/core/async.js +824 -0
  10. package/dist/observe/core/attribution-costs.js +66 -0
  11. package/dist/observe/core/attribution-feedback.js +280 -0
  12. package/dist/observe/core/attribution-hooks.js +83 -0
  13. package/dist/observe/core/attribution-queries.js +43 -0
  14. package/dist/observe/core/attribution.js +3079 -0
  15. package/dist/observe/core/constants.js +269 -0
  16. package/dist/observe/core/context.js +63 -0
  17. package/dist/observe/core/copies.js +10 -0
  18. package/dist/observe/core/core.js +1707 -0
  19. package/dist/observe/core/dev.js +394 -0
  20. package/dist/observe/core/effect.js +193 -0
  21. package/dist/observe/core/error-hooks.js +71 -0
  22. package/dist/observe/core/error.js +82 -0
  23. package/dist/observe/core/external.js +97 -0
  24. package/dist/observe/core/graph.js +162 -0
  25. package/dist/observe/core/heap.js +124 -0
  26. package/dist/observe/core/identities.js +62 -0
  27. package/dist/observe/core/invariants.js +12 -0
  28. package/dist/observe/core/lanes.js +972 -0
  29. package/dist/observe/core/owner.js +371 -0
  30. package/dist/observe/core/scheduler.js +1157 -0
  31. package/dist/observe/core/truth.js +47 -0
  32. package/dist/observe/core/verdict.js +470 -0
  33. package/dist/observe/deferred.js +186 -0
  34. package/dist/observe/flatten.js +79 -0
  35. package/dist/observe/from.js +248 -0
  36. package/dist/observe/index.js +71 -0
  37. package/dist/observe/map.js +593 -0
  38. package/dist/observe/reveal.js +212 -0
  39. package/dist/observe/signals.js +717 -0
  40. package/dist/observe/store/affects.js +142 -0
  41. package/dist/observe/store/edges.js +39 -0
  42. package/dist/observe/store/hooks.js +16 -0
  43. package/dist/observe/store/index.js +47 -0
  44. package/dist/observe/store/optimistic.js +713 -0
  45. package/dist/observe/store/projection.js +345 -0
  46. package/dist/observe/store/reconcile.js +281 -0
  47. package/dist/observe/store/store.js +2127 -0
  48. package/dist/observe/store/storePath.js +103 -0
  49. package/dist/observe/store/target.js +46 -0
  50. package/dist/observe/store/types.js +83 -0
  51. package/dist/observe/store/utils.js +1063 -0
  52. package/dist/prod/affects-marks.js +86 -0
  53. package/dist/prod/affects.js +98 -112
  54. package/dist/prod/attribution.js +45 -0
  55. package/dist/prod/boundaries.js +578 -468
  56. package/dist/prod/core/action.js +92 -59
  57. package/dist/prod/core/async.js +522 -336
  58. package/dist/prod/core/attribution-hooks.js +3 -0
  59. package/dist/prod/core/constants.js +168 -44
  60. package/dist/prod/core/context.js +12 -16
  61. package/dist/prod/core/copies.js +10 -0
  62. package/dist/prod/core/core.js +1345 -627
  63. package/dist/prod/core/dev.js +123 -1
  64. package/dist/prod/core/effect.js +113 -80
  65. package/dist/prod/core/error-hooks.js +71 -0
  66. package/dist/prod/core/error.js +18 -4
  67. package/dist/prod/core/external.js +6 -7
  68. package/dist/prod/core/graph.js +119 -72
  69. package/dist/prod/core/heap.js +85 -109
  70. package/dist/prod/core/identities.js +62 -0
  71. package/dist/prod/core/invariants.js +1 -32
  72. package/dist/prod/core/lanes.js +931 -132
  73. package/dist/prod/core/owner.js +192 -129
  74. package/dist/prod/core/scheduler.js +948 -642
  75. package/dist/prod/core/truth.js +47 -0
  76. package/dist/prod/core/verdict.js +409 -420
  77. package/dist/prod/deferred.js +184 -0
  78. package/dist/prod/flatten.js +77 -0
  79. package/dist/prod/from.js +248 -0
  80. package/dist/prod/index.js +40 -16
  81. package/dist/prod/map.js +481 -211
  82. package/dist/prod/reveal.js +212 -0
  83. package/dist/prod/signals.js +401 -84
  84. package/dist/prod/store/affects.js +142 -0
  85. package/dist/prod/store/edges.js +39 -0
  86. package/dist/prod/store/hooks.js +16 -0
  87. package/dist/prod/store/index.js +27 -18
  88. package/dist/prod/store/optimistic.js +707 -0
  89. package/dist/prod/store/projection.js +331 -0
  90. package/dist/prod/store/reconcile.js +281 -0
  91. package/dist/prod/store/store.js +1952 -267
  92. package/dist/prod/store/storePath.js +1 -1
  93. package/dist/prod/store/target.js +43 -0
  94. package/dist/prod/store/types.js +83 -0
  95. package/dist/prod/store/utils.js +997 -138
  96. package/dist/types/affects-marks.d.ts +14 -0
  97. package/dist/types/affects.d.ts +12 -30
  98. package/dist/types/attribution.d.ts +19 -0
  99. package/dist/types/attribution.prod.d.ts +22 -0
  100. package/dist/types/boundaries.d.ts +99 -124
  101. package/dist/types/carved.d.ts +1 -0
  102. package/dist/types/core/action.d.ts +23 -9
  103. package/dist/types/core/async.d.ts +34 -6
  104. package/dist/types/core/attribution-costs.d.ts +35 -0
  105. package/dist/types/core/attribution-feedback.d.ts +133 -0
  106. package/dist/types/core/attribution-hooks.d.ts +307 -8
  107. package/dist/types/core/attribution-queries.d.ts +24 -0
  108. package/dist/types/core/attribution.d.ts +802 -72
  109. package/dist/types/core/constants.d.ts +195 -38
  110. package/dist/types/core/copies.d.ts +54 -0
  111. package/dist/types/core/core.d.ts +295 -65
  112. package/dist/types/core/dev.d.ts +509 -22
  113. package/dist/types/core/effect.d.ts +1 -1
  114. package/dist/types/core/error-hooks.d.ts +71 -0
  115. package/dist/types/core/error.d.ts +18 -1
  116. package/dist/types/core/graph.d.ts +40 -1
  117. package/dist/types/core/heap.d.ts +5 -5
  118. package/dist/types/core/identities.d.ts +17 -0
  119. package/dist/types/core/index.d.ts +11 -7
  120. package/dist/types/core/invariants.d.ts +1 -26
  121. package/dist/types/core/lanes.d.ts +74 -43
  122. package/dist/types/core/owner.d.ts +27 -9
  123. package/dist/types/core/scheduler.d.ts +353 -102
  124. package/dist/types/core/truth.d.ts +18 -0
  125. package/dist/types/core/types.d.ts +97 -71
  126. package/dist/types/core/verdict.d.ts +35 -0
  127. package/dist/types/deferred.d.ts +28 -0
  128. package/dist/types/flatten.d.ts +8 -0
  129. package/dist/types/from.d.ts +47 -0
  130. package/dist/types/index.d.ts +33 -7
  131. package/dist/types/map.d.ts +4 -2
  132. package/dist/types/reveal.d.ts +19 -0
  133. package/dist/types/signals.d.ts +196 -45
  134. package/dist/types/store/affects.d.ts +12 -0
  135. package/dist/types/store/edges.d.ts +9 -0
  136. package/dist/types/store/hooks.d.ts +62 -0
  137. package/dist/types/store/index.d.ts +42 -15
  138. package/dist/types/store/optimistic.d.ts +24 -45
  139. package/dist/types/store/projection.d.ts +17 -68
  140. package/dist/types/store/reconcile.d.ts +3 -46
  141. package/dist/types/store/store.d.ts +133 -136
  142. package/dist/types/store/storePath.d.ts +1 -1
  143. package/dist/types/store/target.d.ts +166 -0
  144. package/dist/types/store/types.d.ts +105 -0
  145. package/dist/types/store/utils.d.ts +188 -0
  146. package/package.json +31 -26
  147. package/dist/node.cjs +0 -8745
  148. package/dist/prod/core/optimistic.js +0 -265
  149. package/dist/prod/store/next/optimistic.js +0 -385
  150. package/dist/prod/store/next/projection.js +0 -172
  151. package/dist/prod/store/next/reconcile.js +0 -331
  152. package/dist/prod/store/next/store.js +0 -1499
  153. package/dist/prod/store/next/target.js +0 -20
  154. package/dist/types/core/optimistic.d.ts +0 -6
  155. package/dist/types/store/next/optimistic.d.ts +0 -20
  156. package/dist/types/store/next/projection.d.ts +0 -8
  157. package/dist/types/store/next/reconcile.d.ts +0 -3
  158. package/dist/types/store/next/store.d.ts +0 -77
  159. package/dist/types/store/next/target.d.ts +0 -117
  160. package/dist/types-cjs/affects.d.cts +0 -47
  161. package/dist/types-cjs/boundaries.d.cts +0 -175
  162. package/dist/types-cjs/core/action.d.cts +0 -64
  163. package/dist/types-cjs/core/async.d.cts +0 -21
  164. package/dist/types-cjs/core/attribution-hooks.d.cts +0 -52
  165. package/dist/types-cjs/core/attribution.d.cts +0 -186
  166. package/dist/types-cjs/core/constants.d.cts +0 -97
  167. package/dist/types-cjs/core/context.d.cts +0 -36
  168. package/dist/types-cjs/core/core.d.cts +0 -163
  169. package/dist/types-cjs/core/dev.d.cts +0 -81
  170. package/dist/types-cjs/core/effect.d.cts +0 -30
  171. package/dist/types-cjs/core/error.d.cts +0 -47
  172. package/dist/types-cjs/core/external.d.cts +0 -15
  173. package/dist/types-cjs/core/graph.d.cts +0 -6
  174. package/dist/types-cjs/core/heap.d.cts +0 -22
  175. package/dist/types-cjs/core/index.d.cts +0 -13
  176. package/dist/types-cjs/core/invariants.d.cts +0 -59
  177. package/dist/types-cjs/core/lanes.d.cts +0 -44
  178. package/dist/types-cjs/core/optimistic.d.cts +0 -6
  179. package/dist/types-cjs/core/owner.d.cts +0 -124
  180. package/dist/types-cjs/core/scheduler.d.cts +0 -180
  181. package/dist/types-cjs/core/types.d.cts +0 -192
  182. package/dist/types-cjs/core/verdict.d.cts +0 -2
  183. package/dist/types-cjs/index.d.cts +0 -10
  184. package/dist/types-cjs/map.d.cts +0 -69
  185. package/dist/types-cjs/package.json +0 -3
  186. package/dist/types-cjs/signals.d.cts +0 -624
  187. package/dist/types-cjs/store/index.d.cts +0 -19
  188. package/dist/types-cjs/store/next/optimistic.d.cts +0 -20
  189. package/dist/types-cjs/store/next/projection.d.cts +0 -8
  190. package/dist/types-cjs/store/next/reconcile.d.cts +0 -3
  191. package/dist/types-cjs/store/next/store.d.cts +0 -77
  192. package/dist/types-cjs/store/next/target.d.cts +0 -117
  193. package/dist/types-cjs/store/optimistic.d.cts +0 -45
  194. package/dist/types-cjs/store/projection.d.cts +0 -70
  195. package/dist/types-cjs/store/reconcile.d.cts +0 -46
  196. package/dist/types-cjs/store/store.d.cts +0 -138
  197. package/dist/types-cjs/store/storePath.d.cts +0 -58
  198. package/dist/types-cjs/store/utils.d.cts +0 -74
@@ -0,0 +1,2127 @@
1
+ import { attrHooks } from "../core/attribution-hooks.js";
2
+
3
+ import { CONFIG_MANUAL_WRITE, CONFIG_OVERRIDE, CONFIG_HELD, $REFRESH, NOT_PENDING, STATUS_PENDING, STATUS_ERROR, EFFECT_RENDER, REACTIVE_RECOMPUTING_DEPS, REACTIVE_STAGED_READ, CONFIG_VERDICT, CONFIG_CHILDREN_FORBIDDEN, STATUS_UNINITIALIZED, CONFIG_PROMOTED, REACTIVE_DIRTY, REACTIVE_CHECK } from "../core/constants.js";
4
+
5
+ import { writeHook, setSignal, isEqual, read, verdict, snapshotCaptureActive, captureWriteSnapshot, context, notePromotedWrite, untrack, slotSignal, stagedScreen, stagedRead, tracking } from "../core/core.js";
6
+
7
+ import { registerGraph } from "../core/dev.js";
8
+
9
+ import { signalHook } from "../core/identities.js";
10
+
11
+ import { link, setSlotUnobserved } from "../core/graph.js";
12
+
13
+ import { getOwner, getObserver } from "../core/owner.js";
14
+
15
+ import { insertIntoHeap } from "../core/heap.js";
16
+
17
+ import { txOf, flushTransaction, holdNode, joinFuture, insertSubs, schedule, passLane, globalQueue, queuePendingNode, resolveTx, GlobalQueue, installStoreHooks, joinPassTx, dirtyQueue } from "../core/scheduler.js";
18
+
19
+ import { lookupTarget, storeOwners, setLookup, $OWNER, isOwned, markDescendants } from "./target.js";
20
+
21
+ import { optHooks, affectsHooks } from "./hooks.js";
22
+
23
+ import { $TARGET, rawValuesUsed, isRawValue, isWrappable, markRawIngest, writeOverride, markRawOne, $RECORD, $PROXY, $TRACK, installShallowView } from "./types.js";
24
+
25
+ export { markRaw } from "./types.js";
26
+
27
+ /**
28
+ * Store — plain stores on L2 (plan sec. 31, S1).
29
+ *
30
+ * A store is a tree of L2 nodes and nothing else carries reactive state:
31
+ *
32
+ * - **Leaf nodes** (one per tracked-or-written key, lazy; `slotSignal`, one
33
+ * literal): `_value` committed, `_pendingValue` this batch's staging.
34
+ * - **Presence nodes** (`in` tracks presence, not value — R13) and the
35
+ * **deep witness** (`deep()` subscribes one node per record).
36
+ * - The **container node** `k` (plan sec. 31.3, Q-A): the `$TRACK` node given a
37
+ * value. `_value` is the committed backing, `_pendingValue` the pending
38
+ * backing the draft mutates — so a write to a key nobody reads has a
39
+ * staging home the scheduler owns, and park/commit/revert of the backing
40
+ * is the hold model's, not a fold ledger of the store's. Structural
41
+ * readers (`ownKeys`, iteration, `$TRACK`, `length`, `deep`) subscribe to
42
+ * it; value readers never do — a key with no leaf reads through the
43
+ * container's visible backing without subscribing (R1: node existence is
44
+ * unobservable).
45
+ *
46
+ * The committed backing (`t.v`) is owned raw (CoW: a user object is shared
47
+ * until the first write privatizes it and its ancestor chain). It mutates at
48
+ * exactly one moment — the flush's commit (`_storeCommit`), after the
49
+ * container node committed its staging. Reads: context-free readers see the
50
+ * committed backing until the flush; a pass sees the staging (signal
51
+ * parity, RUL-1); the draft reads its own staging.
52
+ *
53
+ * Perf contracts (plan sec. 31.4): lazy nodes; one literal per node; O(written) per
54
+ * setter (`wk`); overlay drafts for large plain records; no new named
55
+ * fields on array targets; zero allocation for adopted-but-unread objects.
56
+ */
57
+ /** Key of the deep-witness slot node (never a user key). */ const $DEEP = Symbol(0);
58
+
59
+ // ---------------------------------------------------------------------------
60
+ // wrap / dedupe
61
+ /** Pre-shaped constructor for OBJECT proxy targets: V8 tips a bare `{}` into
62
+ * dictionary mode once ~19 named properties are assigned onto it (the #3044
63
+ * `ovl`/`del` fields crossed that line — every trap's field read became a
64
+ * hash lookup, a 15% deep-dbmon tick regression). Declaring every field in a
65
+ * constructor pre-allocates in-object slots so the map stays fast, with
66
+ * headroom for future fields. The prototype is reset to `Object.prototype`
67
+ * so proxy-forwarded semantics (getPrototypeOf, constructor) are exactly a
68
+ * plain object's. Array targets keep the bare-`[]` path — they must carry
69
+ * the array exotic class for `Array.isArray(proxy)`.
70
+ *
71
+ * ARRAY SHAPE RULE: arrays normalize their named properties to dictionary
72
+ * mode as the count grows (V8 13.x: counts ≡ 0 mod 3 from 18 up), so the
73
+ * target's named field count is capped at 20 — any future write-side state
74
+ * beyond `wk` must ride an extension object (see target.ts), never new
75
+ * named fields here. */ function TargetShape() {
76
+ this.v = undefined;
77
+ this.ch = undefined;
78
+ this.pb = undefined;
79
+ this.n = undefined;
80
+ this.h = undefined;
81
+ this.k = undefined;
82
+ this.dk = undefined;
83
+ this.u = undefined;
84
+ this.pk = undefined;
85
+ this.px = undefined;
86
+ this.d = undefined;
87
+ this.a = undefined;
88
+ this.sc = undefined;
89
+ this.kc = undefined;
90
+ this.nc = undefined;
91
+ this.fam = undefined;
92
+ this.s = undefined;
93
+ this.ovl = undefined;
94
+ this.del = undefined;
95
+ this.wk = undefined;
96
+ }
97
+
98
+ TargetShape.prototype = Object.prototype;
99
+
100
+ function createTarget(e, t, n, r = t?.fam ?? null) {
101
+ // The proxy target carries the array exotic class when the value is an
102
+ // array, so Array.isArray(proxy) is true; the fields live on it directly.
103
+ // Direct field assignment in one fixed order (no Object.assign literal
104
+ // copy): every target shares a hidden-class transition chain — createTarget
105
+ // was the #2 store cost in the uibench creation profile.
106
+ const o = Array.isArray(e) ? [] : new TargetShape;
107
+ o.v = e;
108
+ // Chained-backing flag (backing IS another store's proxy, §7b) — cached so
109
+ // the hot read path never does a per-read symbol lookup on the backing.
110
+ o.ch = e[$TARGET] !== undefined;
111
+ o.pb = null;
112
+ o.n = null;
113
+ o.h = null;
114
+ o.k = null;
115
+ o.dk = null;
116
+ o.wk = null;
117
+ o.u = t;
118
+ o.pk = n;
119
+ o.px = null;
120
+ o.d = false;
121
+ o.a = false;
122
+ o.sc = 0;
123
+ o.kc = 0;
124
+ o.nc = 0;
125
+ o.fam = r;
126
+ o.s = false;
127
+ o.ovl = false;
128
+ o.del = null;
129
+ o.px = new Proxy(o, traps);
130
+ setLookup(e, o);
131
+ return o;
132
+ }
133
+
134
+ function wrap(e, t = null, n = null, r = t?.fam ?? null) {
135
+ // markRaw'd values never wrap through ANY store (R42; sticky raw-marking
136
+ // is one half of the never-both-wrapped-and-raw invariant, RUL-12).
137
+ if (rawValuesUsed && isRawValue(e)) return e;
138
+ const o = lookupTarget(e, r);
139
+ if (o !== undefined) return o.px;
140
+ const i = e[$TARGET];
141
+ if (i?.px === e) {
142
+ // Foreign-family proxies re-wrap into THIS family (writes stay isolated);
143
+ // same-family and plain-store proxies pass through.
144
+ if (r === null || i.fam === r) return e;
145
+ return createTarget(e, t, n, r).px;
146
+ }
147
+ return createTarget(e, t, n, r).px;
148
+ }
149
+
150
+ /** Unwrap our own proxies to their current backing; leave everything else.
151
+ * A projection draft wrapper is registered as its target's alias
152
+ * (projection.ts `wrapDraft`) — adopted as a backing, it would forward to
153
+ * itself (#3767). */ function unwrapValue(e) {
154
+ if (e == null || typeof e !== "object") return e;
155
+ const t = e[$TARGET];
156
+ if (t !== undefined && (t.px === e || lookupTarget(e, t.fam) === t) && t.v !== undefined) {
157
+ // A draft escaping into other storage must be a REAL container that
158
+ // becomes this target's committed backing at fold (the shared-raw
159
+ // contract) — a prototype overlay is neither.
160
+ if (t.ovl) materializePB(t);
161
+ return t.pb ?? t.v;
162
+ }
163
+ return e;
164
+ }
165
+
166
+ // ---------------------------------------------------------------------------
167
+ // nodes: pure subscription points (values used only for equality gating)
168
+ // Shared slot-node equality (create-floor diet): ONE function for every
169
+ // leaf — `this` is the node (method-call convention at every _equals site),
170
+ // `_host` is the baked-in target backref. Logical-slot equality: values
171
+ // resolving to the same child target are the same slot (privatization/
172
+ // adoption swap raw identity without changing the logical value — only
173
+ // changed leaves notify, R9).
174
+ const slotNodeEquals = function(e, t) {
175
+ return isEqual(e, t) || sameLogicalSlot(this.O, e, t);
176
+ };
177
+
178
+ /** The deep witness: the store decides when it notifies. */ const never = false;
179
+
180
+ function sameKey(e, t) {
181
+ return e === t || e !== e && t !== t;
182
+ }
183
+
184
+ /** The container node's arrangement guess: the rows (or keys) the setter
185
+ * left the container with, in order. Its comparator judges a landing or a
186
+ * revert against it by row identity — by key when the family has one
187
+ * (the server's object for a guessed row is the same row). */ class LaneView {
188
+ rows;
189
+ base;
190
+ removed;
191
+ constructor(e,
192
+ /** The backing the guess is shown over: the committed one at the
193
+ * guess, the composed arrangement after a re-base (optimistic.ts). */
194
+ t,
195
+ /** Keys the setter REMOVED from the view it saw (a deliberate delete,
196
+ * not a row the truth has yet to show): a re-base does not restore
197
+ * them. `null` when none. */
198
+ n = null) {
199
+ this.rows = e;
200
+ this.base = t;
201
+ this.removed = n;
202
+ }
203
+ }
204
+
205
+ /** A container node's value as a backing: the base an arrangement guess is
206
+ * shown over, else the value itself. */ function asBacking(e) {
207
+ return e instanceof LaneView ? e.base : e;
208
+ }
209
+
210
+ /** The container node's comparator: a staging is a backing (identity — a
211
+ * pending backing is never "equal" to the committed one); a lane value is
212
+ * an arrangement (`LaneView`), judged by the optimistic machinery. */ const containerEquals = function(e, t) {
213
+ return e === t || !!optHooks?.arrangement(this.O, e, t);
214
+ };
215
+
216
+ function sameLogicalSlot(e, t, n) {
217
+ if (t === null || typeof t !== "object" || n === null || typeof n !== "object") return false;
218
+ const r = lookupTarget(t, e.fam);
219
+ return r !== undefined && r === lookupTarget(n, e.fam);
220
+ }
221
+
222
+ /** Slot nodes whose last subscriber left while they still held a staging:
223
+ * released at the commit that resolves it (`drainFolds`). */ const deferredReleases = new Set;
224
+
225
+ function releaseSlot(e) {
226
+ const t = e.O;
227
+ const n = e.gt;
228
+ if (n === $TRACK) {
229
+ if (t.k === e) t.k = null;
230
+ } else if (n === $DEEP) {
231
+ if (t.dk === e) t.dk = null;
232
+ } else if (t.n?.[n] === e) {
233
+ delete t.n[n];
234
+ t.nc--;
235
+ } else if (t.h?.[n] === e) delete t.h[n];
236
+ // The family's index keeps only targets that still carry a node.
237
+ if (t.fam !== null && t.nc === 0 && t.k === null && t.dk === null && (t.h === null || Reflect.ownKeys(t.h).length === 0)) t.fam.live.delete(t);
238
+ }
239
+
240
+ /** A node was born on `target`: a family target enters the live index. */ function noteNode(e, t, n) {
241
+ if (e.fam !== null) e.fam.live.add(e);
242
+ markDescendants(e);
243
+ if (affectsHooks !== null) affectsHooks.ro(e, t, n);
244
+ }
245
+
246
+ // Shared slot-node release handler: registered once; the core sweep
247
+ // dispatches CONFIG_SLOT_NODE nodes here (graph.ts) instead of holding a
248
+ // per-node closure in a per-node NodeExtension. A dropped node is
249
+ // unreachable through the store — a fresh read makes a fresh node.
250
+ // A staged write — or a lane's value (a guess, S4) — is state only the node
251
+ // holds: its release waits for the commit that resolves it. So does a leaf of
252
+ // a held container (#4146): a mainline write it carried is in the held
253
+ // staging too, where it would read as the hold's until the landing.
254
+ const slotKept = e => e.ue !== NOT_PENDING || e.l & CONFIG_OVERRIDE || e.O.k?.l & CONFIG_HELD;
255
+
256
+ setSlotUnobserved(e => {
257
+ if (slotKept(e)) deferredReleases.add(e); else releaseSlot(e);
258
+ });
259
+
260
+ /** A node born while its container has a staging is born from the two
261
+ * frames (A29): committed from the committed backing, the staging from the
262
+ * pending one — and held by the container's transaction when the container
263
+ * is held (#3706: the unit of the hold is the key — a key the batch left
264
+ * unchanged stages nothing and holds no one). */ function bornStaged(e, t, n, r) {
265
+ if (t.je && t.je(t.le, n)) return;
266
+ queuePendingNode(t);
267
+ t.ue = n;
268
+ const o = e.k;
269
+ if (o === null) return;
270
+ // a node-less staging holds nothing (`stageOn`)
271
+ // Held by the container's transaction only for a key the HELD staging
272
+ // changed — a mainline layer's change above it stages mainline. (A
273
+ // container carrying a guess is a lane's: the flush's park decides —
274
+ // unless a correction folded under the guess, D-34: the key is that
275
+ // correction's hold's.)
276
+ if (o.l & CONFIG_HELD && heldKeyChanged(e, r)) {
277
+ const e = txOf(o);
278
+ if (!(o.l & CONFIG_OVERRIDE) || e.folded) holdNode(t, resolveTx(e._parent ?? e));
279
+ }
280
+ // A28 (4): a batch written inside a creation-time pass is promoted — its
281
+ // late-materialized leaf with it.
282
+ if (o.l & CONFIG_PROMOTED) notePromotedWrite(t);
283
+ }
284
+
285
+ function getNode(e, t,
286
+ // First-read dedupe (create-floor slice 2): the get trap probes
287
+ // accessor-ness right before creating the node — pass the verdict through
288
+ // so creation skips the second descriptor scan. -1 = unknown (other
289
+ // callers), 0/1 = probed.
290
+ n = -1) {
291
+ const r = e.n ??= Object.create(null);
292
+ let o = r[t];
293
+ if (o === undefined) {
294
+ // Accessor-ness resolved ONCE per node (no per-object descriptor scan
295
+ // on reads): accessor keys serve through Reflect.get with the proxy
296
+ // receiver.
297
+ const i = e.pb;
298
+ const l = o = slotSignal(committed(e)[t], slotNodeEquals, e, t, n === -1 ? isOwnAccessor(i ?? e.v, t) : n === 1);
299
+ if (i !== null && !l.acc) bornStaged(e, l, e.del?.has(t) ? undefined : i[t], t);
300
+ // Attribution-only: name store property nodes by path segment so
301
+ // attribution chains and wide-scope warnings read "store.todos" (or
302
+ // "todos.title" when the store was declared with a name), not
303
+ // "signal". Gated on the engine being installed — node creation is
304
+ // the hottest store path, and the disabled cost must stay one null
305
+ // check (nodes created before enable() stay generically named).
306
+ if (attrHooks !== null) {
307
+ const n = storeRoot(e);
308
+ l._name = (storeNames.get(n) ?? "store") + "." + String(t);
309
+ l._loc = storeLocs.get(n);
310
+ stampNodeOwner(l, e);
311
+ }
312
+ r[t] = o;
313
+ e.nc++;
314
+ noteNode(e, l, t);
315
+ }
316
+ return o;
317
+ }
318
+
319
+ function getHasNode(e, t) {
320
+ const n = e.h ??= Object.create(null);
321
+ let r = n[t];
322
+ if (r === undefined) {
323
+ const o = r = slotSignal(t in committed(e), isEqual, e, t, false);
324
+ const i = e.pb;
325
+ if (i !== null) bornStaged(e, o, t in i && !e.del?.has(t), t);
326
+ if (attrHooks !== null) stampNodeOwner(o, e);
327
+ n[t] = r;
328
+ noteNode(e, o, t);
329
+ }
330
+ return r;
331
+ }
332
+
333
+ /** The container node (plan sec. 31.3): created by the first structural subscription
334
+ * or the first write that needs to stage. Its committed value is the
335
+ * committed backing. */ function getContainerNode(e) {
336
+ let t = e.k;
337
+ if (t === null) {
338
+ t = e.k = slotSignal(e.v, containerEquals, e, $TRACK, false);
339
+ if (attrHooks !== null) stampNodeOwner(t, e);
340
+ noteNode(e, t, $TRACK);
341
+ // Born while a batch is staged without a node (`stageOn`): the node
342
+ // takes the staging as its own — the pre-batch backing committed, the
343
+ // pending backing staged — so whoever asked for it reads the frame it
344
+ // would have read had the node been there from the write.
345
+ if (e.pb !== null && !directCommit) {
346
+ t.le = preBatch(e);
347
+ t.ue = e.pb;
348
+ queuePendingNode(t);
349
+ }
350
+ }
351
+ return t;
352
+ }
353
+
354
+ /** Q-A's escape hatch (plan sec. 33; measured plan sec. 41.4): a staging needs the container
355
+ * node as its home only when something can read the frame through it — a
356
+ * structural subscriber (the node exists), lane work (the staging is the
357
+ * lane's), a pass outside a flush (its write is promoted at the pass's end,
358
+ * A28 (4)), or a derive a transaction holds (the staging is the derive's,
359
+ * `holdWithDerive`). Otherwise the backing and the fold queue are the whole
360
+ * staging: nothing allocates, nothing is swept at the commit (dbmon's tick
361
+ * re-created, queued, swept and dropped ~7000 container nodes). If the
362
+ * flush parks, the seam materializes and holds the node then
363
+ * (`storeParkHook`) — the one case the staging home exists for. */ function stageOn(e, t) {
364
+ if (e.k === null && passLane === null && (context === null || globalQueue.Y) && !deriveHolds(e)) return;
365
+ const n = getContainerNode(e);
366
+ if (n.ue === NOT_PENDING) queuePendingNode(n);
367
+ n.ue = t;
368
+ // A28 (4): a write issued inside a pass outside a flush (a creation-time
369
+ // derive) is promoted at the pass's end — the batch with it.
370
+ if (context !== null) notePromotedWrite(n);
371
+ if (e.fam !== null) holdWithDerive(e, n);
372
+ }
373
+
374
+ /** The projection's derive is held by a transaction and this is its own
375
+ * write (not a user setter's): the staging is the derive's (`holdWithDerive`). */ function deriveHolds(e) {
376
+ const t = e.fam?.node;
377
+ return t != null && !userWrite && (t.l & CONFIG_HELD) !== 0;
378
+ }
379
+
380
+ const storePark = e => {
381
+ for (let t = 0; t < foldList.length; t++) {
382
+ const n = foldList[t];
383
+ if (n.k === null && n.pb !== null) holdNode(getContainerNode(n), e);
384
+ }
385
+ optHooks?.park(e);
386
+ };
387
+
388
+ function getDeepNode(e) {
389
+ let t = e.dk;
390
+ if (t === null) {
391
+ t = e.dk = slotSignal(0, never, e, $DEEP, false);
392
+ if (attrHooks !== null) stampNodeOwner(t, e);
393
+ noteNode(e, t, $DEEP);
394
+ }
395
+ return t;
396
+ }
397
+
398
+ function bumpDeep(e) {
399
+ if (e.dk !== null) setSignal(e.dk, 1);
400
+ }
401
+
402
+ /** The structural readers' one notification: a membership/arrangement
403
+ * change on the container (the staging is already on the node; `setSignal`
404
+ * re-stages the same backing and walks the subscribers — and, on later
405
+ * steps, routes a held or lane-owned container the way any node is). */ function notifyContainer(e, t) {
406
+ // A container carrying an arrangement guess takes the write as a landing
407
+ // on the guess (`laneWrite` → `supersede`, judged by `containerEquals`);
408
+ // otherwise the structural readers are walked (the staging is already on
409
+ // the node; the comparator is the store's, done).
410
+ if (e.l & CONFIG_OVERRIDE) setSignal(e, t); else {
411
+ if (e.l & CONFIG_HELD) joinFuture(txOf(e));
412
+ if (snapshotCaptureActive) captureWriteSnapshot(e, e.le);
413
+ insertSubs(e);
414
+ schedule();
415
+ }
416
+ }
417
+
418
+ // Observe-tier labels and owners (attribution).
419
+ function storeRoot(e) {
420
+ let t = e;
421
+ while (t.u !== null) t = t.u;
422
+ return t;
423
+ }
424
+
425
+ function storeRootOwner(e) {
426
+ return storeOwners.get(storeRoot(e));
427
+ }
428
+
429
+ function stampNodeOwner(e, t) {
430
+ e.H = storeRootOwner(t) ?? null;
431
+ }
432
+
433
+ const storeNames = new WeakMap;
434
+
435
+ // The store's `loc` option, carried onto every property node beside its
436
+ // label (the same gate: nodes created before the engine is installed carry
437
+ // neither).
438
+ const storeLocs = new WeakMap;
439
+
440
+ function nameStore(e, t, n) {
441
+ if (attrHooks !== null) {
442
+ const r = e[$TARGET];
443
+ if (t) storeNames.set(r, t);
444
+ if (n) storeLocs.set(r, n);
445
+ }
446
+ }
447
+
448
+ // ---------------------------------------------------------------------------
449
+ // clones (CoW privatization)
450
+ const hasOwn = Object.prototype.hasOwnProperty;
451
+
452
+ const lookupGetter = Object.prototype.__lookupGetter__;
453
+
454
+ const lookupSetter = Object.prototype.__lookupSetter__;
455
+
456
+ const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
457
+
458
+ function isOwnAccessor(e, t) {
459
+ return hasOwn.call(e, t) && (lookupGetter.call(e, t) !== undefined || lookupSetter.call(e, t) !== undefined);
460
+ }
461
+
462
+ /** Shallow clone into store ownership. Plain data (grade 2) spreads; anything
463
+ * else copies descriptors (accessors kept, everything made configurable). */ function cloneRaw(e, t) {
464
+ t.sc || scanAccessorsOnce(t);
465
+ if (t.sc === 2) {
466
+ const n = {
467
+ ...e
468
+ };
469
+ n[$OWNER] = t;
470
+ return n;
471
+ }
472
+ // A plain array with no accessors and no named keys beyond `length`
473
+ // (`kc` counts own keys) clones by `slice()`: the descriptor path
474
+ // allocates one descriptor per element, O(n) per first draft write (10k
475
+ // rows: ~10 ms per push).
476
+ let n;
477
+ if (!t.a && t.kc <= e.length + 1 && Object.getPrototypeOf(e) === Array.prototype) n = e.slice(); else {
478
+ const r = Object.getOwnPropertyDescriptors(e);
479
+ for (const n of Reflect.ownKeys(r)) {
480
+ const o = r[n];
481
+ if (n === "length" && Array.isArray(e)) continue;
482
+ o.configurable = true;
483
+ if (!o.get && !o.set) o.writable = true; else t.a = true;
484
+ }
485
+ n = Array.isArray(e) ? Object.defineProperties([], r) : Object.create(Object.getPrototypeOf(e), r);
486
+ }
487
+ n[$OWNER] = t;
488
+ return n;
489
+ }
490
+
491
+ /** Wide plain clone (many keys): null-prototype build, then reset. */ function wideClone(e, t) {
492
+ const n = Object.create(null);
493
+ for (const t of Reflect.ownKeys(e)) n[t] = e[t];
494
+ Object.setPrototypeOf(n, Object.prototype);
495
+ n[$OWNER] = t;
496
+ return n;
497
+ }
498
+
499
+ function copyOwn(e, t, n) {
500
+ const r = Object.getOwnPropertyDescriptor(t, n);
501
+ if (r.get || r.set || !r.enumerable || !r.writable || !r.configurable) Object.defineProperty(e, n, r); else e[n] = r.value;
502
+ }
503
+
504
+ /** One descriptor scan per container (grade 1/2, `a`, `kc`). */ function scanAccessorsOnce(e) {
505
+ const t = e.v;
506
+ const n = Reflect.ownKeys(t);
507
+ let r = Object.getPrototypeOf(t) === Object.prototype;
508
+ for (const o of n) {
509
+ if (isOwnAccessor(t, o)) {
510
+ e.a = true;
511
+ r = false;
512
+ break;
513
+ }
514
+ if (r && !propertyIsEnumerable.call(t, o)) r = false;
515
+ }
516
+ e.sc = r ? 2 : 1;
517
+ e.kc = n.length;
518
+ return !e.a;
519
+ }
520
+
521
+ // ---------------------------------------------------------------------------
522
+ // the pending backing
523
+ const OVERLAY_MIN_KEYS = 32;
524
+
525
+ const OVERLAY_REBUILD_MAX_KEYS = 1024;
526
+
527
+ const OVERLAY_REBUILD_MIN_ADDS = 16;
528
+
529
+ const WK_ALL = new Set;
530
+
531
+ const plainProto = e => {
532
+ const t = Object.getPrototypeOf(e);
533
+ return t === Object.prototype || t === Array.prototype || t === null;
534
+ };
535
+
536
+ /** An overlay with many adds (or any delete) rebuilds the committed
537
+ * container at flatten so its shape stays fast (#3689). */ function overlayRebuilds(e, t) {
538
+ if (e.kc > OVERLAY_REBUILD_MAX_KEYS) return false;
539
+ if (e.del?.size) return true;
540
+ const n = e.wk;
541
+ if (n !== null && n !== WK_ALL && n.u === undefined && n.size <= OVERLAY_REBUILD_MIN_ADDS) return false;
542
+ const r = e.v;
543
+ let o = 0;
544
+ for (const e of Reflect.ownKeys(t)) if (!hasOwn.call(r, e) && ++o > OVERLAY_REBUILD_MIN_ADDS) return true;
545
+ return false;
546
+ }
547
+
548
+ /** Downgrade an overlay to a real clone (a consumer needs a container). */ function materializePB(e) {
549
+ const t = e.pb;
550
+ if (!e.ovl) return t;
551
+ const n = overlayOnto(e, e.sc === 2 ? wideClone(e.v, e) : cloneRaw(e.v, e), t);
552
+ e.pb = n;
553
+ // The staging IS the pending backing: re-point the container node.
554
+ if (e.k?.ue === t) e.k.ue = n;
555
+ return n;
556
+ }
557
+
558
+ /** The batch's pending backing, created on the first draft write: an overlay
559
+ * of an owned plain-data record above the key threshold, a clone otherwise.
560
+ * Staged on the container node (the scheduler owns its lifetime) and queued
561
+ * for the fold at the flush's commit. */ function ensurePB(e) {
562
+ let t = e.pb;
563
+ // An optimistic edit composes on the view the user saw and becomes
564
+ // guesses at the setter's exit: nothing is staged, nothing folds
565
+ // (optimistic.ts).
566
+ if (optHooks && userWrite && e.fam?.opt === true && !pendingNotify.has(e)) return e.pb = optHooks.draft(e);
567
+ if (t !== null && !isOwned(t)) {
568
+ // An adopted object awaiting its fold is the user's: the draft writes a
569
+ // clone of it. The clone is the batch's staging — unless the adoption is
570
+ // HELD (another transaction's): then the held staging stays the
571
+ // adoption and the draft is a mainline layer above it (#3612/#3688: a
572
+ // mainline setter mid-hold publishes mainline; the held keys stay held).
573
+ t = e.pb = cloneRaw(t, e);
574
+ const n = e.k;
575
+ if (n !== null && !(n.l & CONFIG_HELD)) n.ue = t;
576
+ } else if (t === null) {
577
+ const n = e.v;
578
+ if (!directCommit) queueFold(e);
579
+ if (!e.ch && !Array.isArray(n) && (e.sc !== 0 ? !e.a : scanAccessorsOnce(e)) && e.kc > OVERLAY_MIN_KEYS && isOwned(n)) {
580
+ t = e.pb = Object.create(n);
581
+ e.ovl = true;
582
+ } else t = e.pb = cloneRaw(n, e);
583
+ if (!directCommit) stageOn(e, t);
584
+ }
585
+ return t;
586
+ }
587
+
588
+ /** Privatize `target`'s committed backing (and its ancestor chain — a shared
589
+ * parent cannot point at an owned child). Not a reactive event. */ function privatizeCommitted(e) {
590
+ if (isOwned(e.v)) return;
591
+ const t = e.v;
592
+ const n = cloneRaw(t, e);
593
+ // Mid-batch (a staging is open on this target) the fold still sees the
594
+ // backing the batch started from: the queue recorded it (`foldOlds`).
595
+ e.v = n;
596
+ e.ch = false;
597
+ if (e.u) {
598
+ privatizeCommitted(e.u);
599
+ const n = e.u.v;
600
+ const r = parentSlotKey(e, t);
601
+ if (n[r] === t) n[r] = e.v;
602
+ }
603
+ }
604
+
605
+ /** The key this target sits under in its parent — re-resolved by identity
606
+ * when an array parent moved it (#3282). Not searched when the slot already
607
+ * holds this target's backing (a parent adopted whole carries its children's
608
+ * new raws before the children fold — the miss was O(rows) per changed row
609
+ * of a keyed reconcile). */ function parentSlotKey(e, t) {
610
+ const n = e.pk;
611
+ const r = e.u.v;
612
+ if (r[n] === t || r[n] === e.v || !Array.isArray(r)) return n;
613
+ const o = r.indexOf(t);
614
+ if (o === -1) return n;
615
+ e.pk = o;
616
+ return o;
617
+ }
618
+
619
+ /** Overlay flatten (#3044): own keys onto the owned committed backing in
620
+ * place — the backing keeps its identity. */ function flattenOverlay(e, t) {
621
+ privatizeCommitted(e);
622
+ overlayOnto(e, e.v, t);
623
+ // the caller (`foldTarget`) closes the staging
624
+ }
625
+
626
+ /** An overlay's own keys and deletions onto `to` (the two are disjoint: a
627
+ * write un-deletes its key); the overlay is closed. */ function overlayOnto(e, t, n) {
628
+ for (const r of Reflect.ownKeys(n)) e.sc === 2 ? t[r] = n[r] : copyOwn(t, n, r);
629
+ if (e.del !== null) {
630
+ for (const n of e.del) delete t[n];
631
+ e.kc -= e.del.size;
632
+ e.del = null;
633
+ }
634
+ e.ovl = false;
635
+ return t;
636
+ }
637
+
638
+ /** Adoption (a setter's returned replacement; reconcile and projection
639
+ * landings in S3): the incoming object becomes the backing by reference at
640
+ * the fold — no clones, no user-object mutation, ownership resets to
641
+ * shared. It is the container's staging until then (signal parity: a
642
+ * handler reads the committed frame until the flush; a hold keeps it
643
+ * staged with the rest). The nodes are told now, diffed against the view
644
+ * they were LAST told (#3296): the draft's pending backing when one
645
+ * preceded the adoption this batch — its node writes were real, and the
646
+ * adoption's re-stage cancels or confirms each — else the committed
647
+ * backing. */ function adoptPB(e, t, n = true) {
648
+ if (!directCommit) queueFold(e);
649
+ let r;
650
+ if (e.pb !== null) {
651
+ if (e.ovl) materializePB(e);
652
+ r = e.pb;
653
+ } else r = e.v;
654
+ if (directCommit) {
655
+ e.pb = null;
656
+ if (e.k !== null) e.k.le = t;
657
+ } else {
658
+ // Eager (INTERNALS §3): the backing swaps now — handlers read the
659
+ // adopted object before the flush — while the container node, when
660
+ // there is one (`stageOn`), keeps the committed frame (`_value`) for
661
+ // readers a hold keeps on it, and stages the adoption for the fold's
662
+ // path copy.
663
+ e.pb = t;
664
+ stageOn(e, t);
665
+ }
666
+ e.v = t;
667
+ e.ch = t[$TARGET] !== undefined;
668
+ e.ovl = false;
669
+ e.del = null;
670
+ e.sc = 0;
671
+ e.a = false;
672
+ e.wk = null;
673
+ // adoption supersedes staged trap writes
674
+ const o = t[$OWNER];
675
+ if (o?.fam === e.fam) t[$OWNER] = e; else setLookup(t, e);
676
+ if (n && r !== t) notifyFold(e, r, t);
677
+ }
678
+
679
+ // ---------------------------------------------------------------------------
680
+ // the fold: owned raw's one mutation point, at the flush's commit
681
+ /** The fold queue: the targets with a batch open (`pb !== null` IS the
682
+ * membership — a target is queued exactly while it carries a staging; the
683
+ * drain skips one already folded), in two reusable arrays. Nothing here
684
+ * allocates per batch once warm: a dbmon tick queues ~7000 containers, and
685
+ * a map built and drained per tick cost ~540 KB of table allocation — most
686
+ * of the tick's young-generation GC, and the whole of its gap to `next`
687
+ * (plan sec. 41.5).
688
+ *
689
+ * Beside each target, its PRE-BATCH committed backing — `t.v` as the batch
690
+ * opened on it (`foldOlds`, parallel to `foldList`). That one record is the
691
+ * fold's base in every case: a draft's backing does not move until the fold
692
+ * (so the record is `t.v` still), an adoption swaps it eagerly (the record
693
+ * is the frame committed readers keep), a mid-batch privatization or a
694
+ * draft over an adopted raw moves it again (the record is what the batch
695
+ * started from, which is all the fold wants to know). The record lives
696
+ * exactly as long as the batch: a weak map keyed by target kept every
697
+ * container's LAST adopted-away backing alive until its next adoption —
698
+ * for a keyed reconcile, the whole previous tree promoted out of the
699
+ * nursery every tick (the saturated listened-paths shape ran 15–25% over
700
+ * the fork, all of it this; plan sec. 43.2) — and for a store adopted
701
+ * once, for its lifetime.
702
+ *
703
+ * The target carries no slot for its record (ARRAY SHAPE RULE, target.ts),
704
+ * so a lookup by target (`preBatch`: a container node born onto an open
705
+ * staging, a committed-frame read of a node-less staging, a park) indexes
706
+ * the list through a map built on first use per batch — the drain itself
707
+ * walks the two lists in step and never builds it. */ let foldList = [];
708
+
709
+ let foldOlds = [];
710
+
711
+ let foldSpare = [];
712
+
713
+ let foldOldsSpare = [];
714
+
715
+ let foldIndex = null;
716
+
717
+ /** Before the staging is set and before an adoption swaps the backing. */ function queueFold(e) {
718
+ if (e.pb !== null) return;
719
+ schedule();
720
+ if (foldIndex !== null) foldIndex.set(e, foldList.length);
721
+ foldList.push(e);
722
+ foldOlds.push(e.v);
723
+ }
724
+
725
+ /** The committed backing before this batch (valid while `pb !== null`). */ function preBatch(e) {
726
+ if (foldIndex === null) {
727
+ foldIndex = new Map;
728
+ for (let e = 0; e < foldList.length; e++) foldIndex.set(foldList[e], e);
729
+ }
730
+ const t = foldIndex.get(e);
731
+ return t === undefined ? e.v : foldOlds[t];
732
+ }
733
+
734
+ /** The flush committed its pending nodes: fold every pending backing whose
735
+ * container node committed (its staging is gone — `_value` is the backing
736
+ * now) into the committed home; one a later step holds (its node still
737
+ * staged) waits for the flush that commits it. Then the deferred slot
738
+ * releases. */ function drainFolds() {
739
+ if (foldList.length !== 0) {
740
+ const e = foldList;
741
+ const t = foldOlds;
742
+ foldList = foldSpare;
743
+ foldOlds = foldOldsSpare;
744
+ foldSpare = e;
745
+ foldOldsSpare = t;
746
+ foldIndex = null;
747
+ for (let n = 0; n < e.length; n++) {
748
+ const r = e[n];
749
+ if (r.pb === null) continue;
750
+ // folded already (a direct commit)
751
+ const o = r.k;
752
+ if (o !== null && o.ue !== NOT_PENDING) {
753
+ // held: commits with its hold
754
+ foldList.push(r);
755
+ foldOlds.push(t[n]);
756
+ } else foldTarget(r, t[n]);
757
+ }
758
+ e.length = 0;
759
+ t.length = 0;
760
+ }
761
+ if (deferredReleases.size !== 0) {
762
+ for (const e of deferredReleases) {
763
+ if (e.o === null && !slotKept(e)) {
764
+ deferredReleases.delete(e);
765
+ releaseSlot(e);
766
+ }
767
+ }
768
+ }
769
+ }
770
+
771
+ /** Core asks: is a slot node's truth in flight? Its family's derive is —
772
+ * lanes (A17, #2864: a guess mid-refetch stands in for the flight) and
773
+ * marks (an ambient `affects()` over a declared reload lives to the
774
+ * landing) read it. */ GlobalQueue.k = e => {
775
+ const t = e.O?.fam;
776
+ return (t?.node?.m & STATUS_PENDING) !== 0;
777
+ };
778
+
779
+ /** Core asks (lanes.ts `covered`): the value a slot node's guess covered.
780
+ * A chained link's committed value is the inner store's at the last
781
+ * read-through — refreshed here, since a guess served the reads since
782
+ * (#3672: the revert compares against the base's live value). */ GlobalQueue.an = e => {
783
+ const t = e.O;
784
+ if (t.ch) {
785
+ const n = e.gt;
786
+ const r = t.v;
787
+ if (t.h?.[n] === e) e.le = untrack(() => n in r); else if (t.n?.[n] === e) e.le = untrack(() => unwrapValue(r[n]));
788
+ }
789
+ return e.le;
790
+ };
791
+
792
+ installStoreHooks(storePark, () => {
793
+ drainFolds();
794
+ optHooks?.sweep();
795
+ });
796
+
797
+ /** The store half of `affects()` (store/affects.ts) makes its marks' nodes
798
+ * and walks records through these: it never imports the engine. */ GlobalQueue.so = getNode;
799
+
800
+ GlobalQueue.fo = isWrappable;
801
+
802
+ /** A projection's creation run commits directly (a memo's first value is
803
+ * its `_value`, not a staging): the draft still writes a clone of the seed,
804
+ * nothing is staged on the nodes, and the setter's exit folds the touched
805
+ * containers at once. Only the synchronous first run. */ let directCommit = false;
806
+
807
+ function runDirect(e) {
808
+ const t = directCommit;
809
+ directCommit = true;
810
+ try {
811
+ e();
812
+ } finally {
813
+ directCommit = t;
814
+ }
815
+ }
816
+
817
+ /** Fold one target's pending backing into its committed home. */ function foldTarget(e, t) {
818
+ {
819
+ {
820
+ const n = e.k;
821
+ const r = e.pb;
822
+ if (r !== null) {
823
+ if (r === e.v) ; else if (e.ovl && (e.v !== t || !overlayRebuilds(e, r) && n?.T?.st !== t)) {
824
+ // Overlay flatten (#3044): the backing keeps its identity, so the
825
+ // `t.v === old` gate below skips path copying (the parent slot
826
+ // already points here) and the adopted-notify (setter
827
+ // notifications happened at write time). A backing privatized
828
+ // mid-batch is a fresh clone the parent already points at.
829
+ flattenOverlay(e, r);
830
+ } else if (e.v !== t) {
831
+ // Privatized mid-batch (a child's path copy repointed us): the
832
+ // owned backing takes the written keys.
833
+ privatizeCommitted(e);
834
+ const n = e.v;
835
+ const o = e.wk;
836
+ if (o !== null && o !== WK_ALL) {
837
+ for (let e = o; e !== undefined; e = e.u) for (const t of e) {
838
+ if (hasOwn.call(r, t)) copyOwn(n, r, t); else delete n[t];
839
+ }
840
+ } else {
841
+ for (const e of Reflect.ownKeys(r)) if (e !== $OWNER) copyOwn(n, r, e);
842
+ for (const e of Reflect.ownKeys(t)) {
843
+ if (e !== $OWNER && !hasOwn.call(r, e)) delete n[e];
844
+ }
845
+ }
846
+ } else {
847
+ // The clone (or adopted object) becomes the committed backing
848
+ // (path copy below).
849
+ e.v = e.ovl ? materializePB(e) : r;
850
+ e.ch = e.v[$TARGET] !== undefined;
851
+ }
852
+ e.pb = null;
853
+ e.wk = null;
854
+ // the written-keys window closes with the commit
855
+ }
856
+ if (n !== null) {
857
+ // The node's committed value is the backing. A container nobody
858
+ // subscribes to was a transient staging home (INTERNALS §5: an
859
+ // observer-less write holds a transient record until the fold,
860
+ // discarded with it) — gone with the batch.
861
+ if (n.o === null) e.k = null; else if (n.le instanceof LaneView) n.le.base = e.v; else n.le = e.v;
862
+ }
863
+ if (e.v !== t && e.u) {
864
+ const n = parentSlotKey(e, t);
865
+ if (e.u.v[n] === t) {
866
+ privatizeCommitted(e.u);
867
+ e.u.v[n] = e.v;
868
+ }
869
+ }
870
+ }
871
+ }
872
+ }
873
+
874
+ // ---------------------------------------------------------------------------
875
+ // write-time notification (setter exit)
876
+ /** Devtools and attribution: the containers a setter replaced wholesale. */ function storePathOf(e) {
877
+ let t = "";
878
+ for (let n = e; n !== null; n = n.u) t = n.pk === null ? "store" + t : "." + String(n.pk) + t;
879
+ return t;
880
+ }
881
+
882
+ const REPLACED_CENSUS_MAX = 64;
883
+
884
+ function sameLeaf(e, t) {
885
+ if (e === t) return true;
886
+ if (e === null || t === null || typeof e !== "object" || typeof t !== "object") return false;
887
+ return unwrapValue(e) === unwrapValue(t) || targetsEqual(e, t);
888
+ }
889
+
890
+ function reportReplacedContainers(e, t, n, r) {
891
+ const o = r ?? Reflect.ownKeys(n);
892
+ const i = Array.isArray(n);
893
+ const l = storeRootOwner(e);
894
+ // Items the array held before the write: an index that now holds one of
895
+ // them was moved into place (an in-place splice / shift), not replaced.
896
+ let u = null;
897
+ for (const r of o) {
898
+ if (i && r === "length" || r === $OWNER) continue;
899
+ if (e.del?.has(r)) continue;
900
+ const o = unwrapValue(t[r]);
901
+ const f = unwrapValue(n[r]);
902
+ if (o === null || f === null || typeof o !== "object" || typeof f !== "object" || o === f || targetsEqual(o, f)) continue;
903
+ if (i) {
904
+ if (u === null) {
905
+ u = new Set;
906
+ for (const e of t) u.add(unwrapValue(e));
907
+ }
908
+ if (u.has(f)) continue;
909
+ }
910
+ const s = Array.isArray(f);
911
+ if (s !== Array.isArray(o)) continue;
912
+ let a;
913
+ let c = 0;
914
+ if (s) {
915
+ a = f.length;
916
+ if (a > REPLACED_CENSUS_MAX) continue;
917
+ const e = new Set;
918
+ for (const t of o) e.add(unwrapValue(t));
919
+ for (const t of f) if (e.has(unwrapValue(t))) c++;
920
+ } else {
921
+ const e = Object.keys(f);
922
+ a = e.length;
923
+ if (a > REPLACED_CENSUS_MAX) continue;
924
+ for (const t of e) if (sameLeaf(o[t], f[t])) c++;
925
+ }
926
+ attrHooks.storeReplaced(storePathOf(e) + "." + String(r), s, a, c, s ? o.length : Object.keys(o).length, l);
927
+ }
928
+ }
929
+
930
+ /** A user setter wrote a key whose staging is a derivation another
931
+ * transaction holds (A34 (3), #3612): the hold re-derives over the write. */ let heldDerivationHit = false;
932
+
933
+ /** The open setter is a USER'S (`storeSetter(…, guard = true)`), not the
934
+ * derive's draft or landing. */ let userWrite = false;
935
+
936
+ /** Is the open setter a user's? (reconcile.ts: a user's reconcile on an
937
+ * optimistic family writes the draft.) */ function userWriting() {
938
+ return userWrite;
939
+ }
940
+
941
+ function heldDerivation(e, t, n) {
942
+ if (e.fam === null || !userWrite) return false;
943
+ if (t !== undefined) return (t.l & (CONFIG_HELD | CONFIG_MANUAL_WRITE)) === CONFIG_HELD && txOf(t) !== flushTransaction;
944
+ const r = e.k;
945
+ return r !== null && (r.l & CONFIG_HELD) !== 0 && txOf(r) !== flushTransaction && heldKeyChanged(e, n);
946
+ }
947
+
948
+ /** Setter exit: one node write per changed observed key (the leaves stage
949
+ * like signals — holds and lanes engage here on later steps), presence and
950
+ * deep witnesses, and the container when membership changed. */ function notifyWrites(e) {
951
+ let t = e.pb;
952
+ if (t === null) return;
953
+ // Optimistic channel: a user's writes on an optimistic family become
954
+ // guesses on the nodes (lanes); the draft is discarded, the committed
955
+ // backing never touched.
956
+ if (optHooks && e.fam?.opt === true) {
957
+ if (userWrite) {
958
+ // The draft is discarded first: the diff reads the target (its nodes
959
+ // are born from the committed frame, not the draft).
960
+ e.pb = null;
961
+ e.wk = null;
962
+ e.pb = optHooks.writes(e, t);
963
+ return;
964
+ }
965
+ // The derive's draft: new rows under guessed rows are paired with them
966
+ // first (D-34, #4329).
967
+ optHooks.pair(e, t);
968
+ }
969
+ const n = e.v;
970
+ const r = e.n;
971
+ // Written-keys bound: trap writes record their keys, so the notify visits
972
+ // O(written) nodes instead of every subscription on the record (a selection
973
+ // map with thousands of per-key subscribers pays two visits per select,
974
+ // not a full scan). Falls back to the full node scan when the bound can't
975
+ // hold: no trap granularity (wk null), an array length write (WK_ALL —
976
+ // implicit index deletes), accessors on the record (t.a — a getter node's
977
+ // value can change when ANY key is written), or a non-plain prototype
978
+ // (class instances: prototype getters derive from arbitrary fields).
979
+ // Overlay pbs chain to the COMMITTED object (#3044) — plainness is the
980
+ // committed container's prototype, not the overlay's. THIS setter's keys
981
+ // only (D-8, #3743): a key an earlier setter of the batch wrote and this
982
+ // one did not is no second proposal — its node was told then, and telling
983
+ // a held node its unchanged value again would join this tick to the hold.
984
+ const o = e.wk;
985
+ const i = o === WK_ALL || e.a === true || !plainProto(e.ovl ? e.v : t) ? null : o;
986
+ if (attrHooks !== null) reportReplacedContainers(e, n, t, i);
987
+ if (e.fam !== null && !heldDerivationHit && i !== null) for (const t of i) if (heldDerivation(e, r?.[t], t)) {
988
+ heldDerivationHit = true;
989
+ break;
990
+ }
991
+ if (r !== null) {
992
+ const o = i ?? Reflect.ownKeys(r);
993
+ for (const i of o) {
994
+ const o = r[i];
995
+ if (o === undefined) continue;
996
+ if (o.acc === true || hasOwn.call(t, i) && lookupGetter.call(t, i) !== undefined) {
997
+ // Accessor keys: the node is linked for shape-change notification,
998
+ // its value is never served (the getter runs with the proxy
999
+ // receiver on read) — FORCE wakes the readers. A data descriptor
1000
+ // falls through so the cleared-acc hot read serves it (#3949);
1001
+ // an own getter's descriptor has no `value`, so the compare below
1002
+ // would otherwise keep the getter's result (or FORCE) for `undefined`.
1003
+ o.acc = isOwnAccessor(t, i);
1004
+ const e = Object.getOwnPropertyDescriptor(n, i);
1005
+ const r = Object.getOwnPropertyDescriptor(t, i);
1006
+ if (!r || r.get || r.set) {
1007
+ if (e?.get !== r?.get || e?.set !== r?.set || e?.value !== r?.value) setSignal(o, () => FORCE);
1008
+ continue;
1009
+ }
1010
+ if (e?.get || e?.set || !isEqual(e?.value, r.value)) setSignal(o, () => r.value);
1011
+ continue;
1012
+ }
1013
+ const l = e.del?.has(i) ? undefined : t[i];
1014
+ setSignal(o, () => l);
1015
+ if (e.fam !== null) {
1016
+ // The derive's write is a derivation; a user setter's is a proposal
1017
+ // (A34 (3)'s discriminator).
1018
+ if (!userWrite) {
1019
+ o.l &= ~CONFIG_MANUAL_WRITE;
1020
+ holdWithDerive(e, o);
1021
+ } else o.l |= CONFIG_MANUAL_WRITE;
1022
+ }
1023
+ }
1024
+ }
1025
+ const l = e.h;
1026
+ if (l !== null) {
1027
+ const n = i ?? Reflect.ownKeys(l);
1028
+ for (const r of n) {
1029
+ const n = l[r];
1030
+ if (n !== undefined) {
1031
+ setSignal(n, r in t && !e.del?.has(r));
1032
+ if (e.fam !== null) holdWithDerive(e, n);
1033
+ }
1034
+ }
1035
+ }
1036
+ if (e.dk !== null) {
1037
+ if (e.del?.size) bumpDeep(e); else for (const r of i ?? Reflect.ownKeys(t)) {
1038
+ if (r === $OWNER) continue;
1039
+ const o = t[r];
1040
+ const i = n[r];
1041
+ if (o !== null && typeof o === "object" ? !targetsEqual(i, o) : !isEqual(i, o)) {
1042
+ bumpDeep(e);
1043
+ break;
1044
+ }
1045
+ }
1046
+ }
1047
+ // The container: structural readers hear membership/arrangement changes
1048
+ // only (R9) — the compare is the store's, against the committed frame. A
1049
+ // container carrying an arrangement guess is told whether or not anyone
1050
+ // subscribes: the write is a landing on the guess (`notifyContainer`).
1051
+ const u = e.k;
1052
+ if (u !== null && (u.o !== null || u.l & CONFIG_OVERRIDE)) {
1053
+ let r;
1054
+ if (e.ovl) {
1055
+ r = !!e.del?.size;
1056
+ if (!r) {
1057
+ for (const e of Reflect.ownKeys(t)) {
1058
+ if (!hasOwn.call(n, e)) {
1059
+ r = true;
1060
+ break;
1061
+ }
1062
+ }
1063
+ }
1064
+ } else {
1065
+ r = Array.isArray(t) && Array.isArray(n) ? arrayStructureChanged(n, t) : membershipChanged(n, t);
1066
+ }
1067
+ if (r) notifyContainer(u, t);
1068
+ }
1069
+ }
1070
+
1071
+ /** Accessor-node wake sentinel: readers re-run and re-invoke the getter. */ const FORCE = Symbol();
1072
+
1073
+ /** Two raws that wrap to the same target are the same logical value. */ function targetsEqual(e, t) {
1074
+ if (e === null || typeof e !== "object" || t === null || typeof t !== "object") return false;
1075
+ const n = lookupTarget(e, null);
1076
+ return n !== undefined && n === lookupTarget(t, null);
1077
+ }
1078
+
1079
+ /** Positional identity IS content for arrays (R15). */ function arrayStructureChanged(e, t) {
1080
+ if (e.length !== t.length) return true;
1081
+ for (let n = 0; n < t.length; n++) {
1082
+ const r = e[n];
1083
+ const o = t[n];
1084
+ if (!isEqual(r, o) && !targetsEqual(r, o)) return true;
1085
+ }
1086
+ return false;
1087
+ }
1088
+
1089
+ function membershipChanged(e, t) {
1090
+ const n = Reflect.ownKeys(t);
1091
+ if (Reflect.ownKeys(e).length - +isOwned(e) !== n.length - +isOwned(t)) return true;
1092
+ for (const t of n) if (t !== $OWNER && !(t in e)) return true;
1093
+ return false;
1094
+ }
1095
+
1096
+ /** One leaf's notification for an adoption diff (descriptor-aware). */ function notifyKeyDiff(e, t, n, r, o = true) {
1097
+ if (e.acc === true || o && hasOwn.call(r, t) && lookupGetter.call(r, t) !== undefined) {
1098
+ e.acc = isOwnAccessor(r, t);
1099
+ const o = Object.getOwnPropertyDescriptor(n, t);
1100
+ const i = Object.getOwnPropertyDescriptor(r, t);
1101
+ // Data descriptors fall through, as in notifyWrites (#3949).
1102
+ if (!i || i.get || i.set) {
1103
+ if (o?.get !== i?.get || o?.set !== i?.set || o?.value !== i?.value) setSignal(e, () => FORCE);
1104
+ return;
1105
+ }
1106
+ const l = o?.value;
1107
+ const u = i?.value;
1108
+ if (o?.get || o?.set || !isEqual(l, u) && !targetsEqual(l, u)) setSignal(e, typeof u === "function" ? () => u : u);
1109
+ } else {
1110
+ const o = n[t];
1111
+ const i = r[t];
1112
+ // A live chained `old` (a store proxy, §7b) already reads the inner
1113
+ // store's next frame, not what the node was last told: written, the
1114
+ // node's own equality judging it (#4361).
1115
+ if (n[$TARGET] || !isEqual(o, i) && !targetsEqual(o, i)) setSignal(e, typeof i === "function" ? () => i : i);
1116
+ }
1117
+ }
1118
+
1119
+ function hasAccessorFlag(e) {
1120
+ return e.acc === true;
1121
+ }
1122
+
1123
+ /** One leaf's notification when the caller already has both values. */ function notifyKeyValue(e, t, n, r, o, i) {
1124
+ if (e.acc === true) {
1125
+ notifyKeyDiff(e, t, o, i, false);
1126
+ return;
1127
+ }
1128
+ if (!isEqual(n, r) && !targetsEqual(n, r)) setSignal(e, typeof r === "function" ? () => r : r);
1129
+ }
1130
+
1131
+ /** The structural half of an adoption's notifications (reconcile does the
1132
+ * leaves itself): presence by `in`, the container by membership. Presence
1133
+ * is DIFFED `old` → `neu` like the leaves (#3743): a presence node is
1134
+ * written only where `in` changed between the view it was last told
1135
+ * (#3296) and the adoption — `setSignal` joins a held node's transaction
1136
+ * before its equality gate (A34 (1)), so repeating an unchanged absence to
1137
+ * a node an action holds would make the whole tick the action's (an
1138
+ * unrelated `a.value` stayed stale until the action settled). A real
1139
+ * change still writes, and on a held node still proposes. A live chained
1140
+ * `old` (a store proxy, §7b) reflects the inner store, not what the node
1141
+ * was last told — written unconditionally, as before; its container too
1142
+ * (#4361).
1143
+ *
1144
+ * The container is told of an arrangement change when it has structural
1145
+ * subscribers OR carries an arrangement guess (S4): for a guessed
1146
+ * container the write is the landing that judges the guess (Q-D, plan
1147
+ * sec. 39 — confirm, supersede, or hold beneath), not a subscriber
1148
+ * notification, and a landing whose truth changed no guessed key's value
1149
+ * or presence (a newer question's rows beneath an optimistic push) still
1150
+ * answers the arrangement — before, only the presence write reached the
1151
+ * lane, by accident of being unconditional. */ function notifyFoldTail(e, t, n) {
1152
+ const r = e.h;
1153
+ const o = t[$TARGET];
1154
+ if (r !== null) {
1155
+ for (const e of Reflect.ownKeys(r)) if (o || e in t !== e in n) setSignal(r[e], e in n);
1156
+ }
1157
+ const i = e.k;
1158
+ if (i !== null && (i.o !== null || i.l & CONFIG_OVERRIDE)) {
1159
+ const e = o || (Array.isArray(n) && Array.isArray(t) ? arrayStructureChanged(t, n) : membershipChanged(t, n));
1160
+ if (e) notifyContainer(i, n);
1161
+ }
1162
+ }
1163
+
1164
+ /** An adoption's notifications: every node of the target, diffed `old` →
1165
+ * `neu` (value nodes by key, presence by `in`, the container by
1166
+ * membership, the deep witness by identity). */ function notifyFold(e, t, n) {
1167
+ if (e.dk !== null && t !== n) bumpDeep(e);
1168
+ const r = e.n;
1169
+ if (r !== null) {
1170
+ for (const e of Reflect.ownKeys(r)) notifyKeyDiff(r[e], e, t, n);
1171
+ }
1172
+ notifyFoldTail(e, t, n);
1173
+ }
1174
+
1175
+ // ---------------------------------------------------------------------------
1176
+ // visibility
1177
+ let writing = 0;
1178
+
1179
+ /** Roots of the stores with an open setter: reads of THEIR drafts are the
1180
+ * writer's (no tracking, pending backing served); every other store reads
1181
+ * normally — a derive's external reads are its dependencies (#3037). */ let writeScopes = null;
1182
+
1183
+ const pendingNotify = new Set;
1184
+
1185
+ function scopeKey(e) {
1186
+ if (e.fam !== null) return e.fam;
1187
+ let t = e;
1188
+ while (t.u !== null) t = t.u;
1189
+ return t;
1190
+ }
1191
+
1192
+ function inDraft(e) {
1193
+ return writeScopes !== null && writeScopes.has(scopeKey(e));
1194
+ }
1195
+
1196
+ /** Shallow store: values served verbatim (a nested store proxy still wraps
1197
+ * into the family so writes stay isolated). */ function serveShallow(e, t, n) {
1198
+ if (n !== null && typeof n === "object" && n[$TARGET] !== undefined) return draftServe(e, wrap(n, e, t));
1199
+ return n;
1200
+ }
1201
+
1202
+ /** A child proxy served to a draft joins the draft's scope. */ function draftServe(e, t) {
1203
+ if (writeScopes !== null && inDraft(e)) {
1204
+ const e = t?.[$TARGET];
1205
+ if (e?.v !== undefined) writeScopes.add(scopeKey(e));
1206
+ }
1207
+ return t;
1208
+ }
1209
+
1210
+ /** The committed backing: the container node's committed value while a
1211
+ * batch is staged on it (an adoption swaps `t.v` eagerly — INTERNALS §3,
1212
+ * "adoption is eager by contract" — and the node keeps the frame committed
1213
+ * readers under a hold still see), `t.v` otherwise. */ function committed(e) {
1214
+ const t = e.k;
1215
+ if (t !== null) return t.ue !== NOT_PENDING ? asBacking(t.le) : e.v;
1216
+ // A node-less staging (`stageOn`): the fold queue remembers the frame.
1217
+ return e.pb !== null ? preBatch(e) : e.v;
1218
+ }
1219
+
1220
+ /** The pass that sees this flush's staging (signal parity — `read()`'s
1221
+ * staged arm): the computation running (`context`, never the owner — a
1222
+ * handler, an effect callback or `onSettled` has an owner but no pass),
1223
+ * unless it is a frame reader (children-forbidden, A32). Null = the
1224
+ * committed frame. */ function stagingReader() {
1225
+ let e = context;
1226
+ if (e === null) return null;
1227
+ if (e.Pt) e = e.Ht ?? null;
1228
+ return e === null || e.l & CONFIG_CHILDREN_FORBIDDEN ? null : e;
1229
+ }
1230
+
1231
+ /** The backing a read serves (RUL-1, signal parity): the draft and the write
1232
+ * override read the pending backing; a tracked reader reads it too — only
1233
+ * for presence and descriptors, its leaf (born from the two frames) decides
1234
+ * the value through core; a context-free or frame reader the committed
1235
+ * backing; a pass reading UNTRACKED asks the container node — `read()`
1236
+ * without a link: the node's value is the backing, chosen by the hold rules
1237
+ * (a render effect outside the parking flush sees committed and re-derives
1238
+ * at the landing; a memo joins the future; a staging is marked read). */
1239
+ /** The backing a read of `target` is served from — the truth (committed,
1240
+ * or the staging a pass may see). An optimistic family's lane values are
1241
+ * NOT composed here: a guessed key always has its node, and the node is
1242
+ * what serves its value (`nodeValue`); presence and enumeration compose
1243
+ * per key / per walk (`optimisticHas`, `enumerationSource`). */ function readSource(e, t, n = false) {
1244
+ // An optimistic setter's read before its first write sees what the writer
1245
+ // sees — the lanes' values and the tick's own guesses over the truth
1246
+ // (#3665), never a staging awaiting its fold: the draft is born on the
1247
+ // read (and notified at the setter's exit like any).
1248
+ if (userWrite && e.fam?.opt === true && !pendingNotify.has(e) && inDraft(e)) {
1249
+ const t = ensurePB(e);
1250
+ pendingNotify.add(e);
1251
+ return t;
1252
+ }
1253
+ const r = e.pb;
1254
+ if (r === null) return e.v;
1255
+ if (inDraft(e) || writeOverride) {
1256
+ // The derive reads its own store through its draft: a key (or the
1257
+ // container) a transaction holds — a user's write inside an action,
1258
+ // held with it — is the future, and the pass that reads it derives
1259
+ // from the future (L2: a read of something already held joins it): the
1260
+ // derive's whole result reveals with the action (core R31 / #3733:
1261
+ // "under the transaction and revealed with it"), not key by key. A
1262
+ // user's setter reading its draft is no pass and joins nothing here.
1263
+ if (writeOverride && !userWrite) {
1264
+ const n = t !== undefined ? e.n?.[t] : undefined;
1265
+ if (n !== undefined) {
1266
+ if ((n.l & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD) joinPassTx(txOf(n));
1267
+ } else {
1268
+ const n = e.k;
1269
+ if (n !== null && (n.l & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD && (t === undefined || heldKeyChanged(e, t))) joinPassTx(txOf(n));
1270
+ }
1271
+ }
1272
+ return r;
1273
+ }
1274
+ const o = e.k;
1275
+ // A key whose leaf carries a lane's value (a guess, S4): the leaf serves
1276
+ // it — the container's frame is not consulted (a reader is not made a
1277
+ // stale reader of the container's hold for a key it does not see through
1278
+ // it).
1279
+ if (t !== undefined) {
1280
+ const n = e.n?.[t];
1281
+ if (n?.l & CONFIG_OVERRIDE) return committed(e);
1282
+ }
1283
+ const i = o !== null && (o.l & CONFIG_HELD) !== 0;
1284
+ // A held container: the hold's unit is the key (#3706). A key the HELD
1285
+ // staging left unchanged reads the ambient view — a mainline layer's
1286
+ // write above the hold publishes mainline (#3688, #3612); a key it
1287
+ // changed reads the frame core serves this reader.
1288
+ if (i && t !== undefined && !heldKeyChanged(e, t, n)) return r;
1289
+ const l = stagingReader();
1290
+ if (l === null) {
1291
+ if (getObserver() !== null) return r;
1292
+ // a frame reader's leaf decides the value
1293
+ // A projection's writes are its truth as soon as made (the derive is
1294
+ // the authority, nothing it writes is a proposal): a context-free reader
1295
+ // sees them before the flush — unless a hold keeps them staged.
1296
+ if (familyAhead(e)) return r;
1297
+ // A verdict window (`isPending`/`latest`) with no reader (a top-level
1298
+ // probe) judges the container like any node — for a key the batch
1299
+ // changed; an unchanged key is committed and final (A22: pending is per
1300
+ // key). A held container's committed frame is the node's.
1301
+ if (verdict === null || !i && t !== undefined && !keyChanged(e, t, n)) return i ? asBacking(o.le) : e.v;
1302
+ return o === null ? committed(e) : asBacking(read(o));
1303
+ }
1304
+ // A pass reading a key the batch left unchanged: the same value in both
1305
+ // frames — committed, holding no one. Otherwise the container's frame by
1306
+ // core's rules — `read()` of the node WITHOUT a link (a value reader never
1307
+ // subscribes to the container): a render effect outside a hold's flush
1308
+ // sees committed and re-derives at the landing, a memo joins the future,
1309
+ // a staging read is marked. The key's own leaf, when it has one, decides
1310
+ // the value the same way.
1311
+ if (!i) {
1312
+ // A plain pass this flush already served a staging as the screen
1313
+ // (REACTIVE_STAGED_READ — what `read()` would set again here) reads an
1314
+ // unheld staging directly: an unchanged key is the same value in both
1315
+ // frames, a changed one is what the mark already stands for. The
1316
+ // per-key question is asked once per pass, not once per key (a
1317
+ // `mapArray` over 1k shifted rows asked it 1k times).
1318
+ if (l.R & REACTIVE_STAGED_READ && !(l.l & (CONFIG_VERDICT | CONFIG_CHILDREN_FORBIDDEN))) return r;
1319
+ if (t !== undefined && !keyChanged(e, t, n)) return committed(e);
1320
+ // A node-less staging (`stageOn`): what `read()` of its node would do
1321
+ // for this pass — a verdict reader sees the screen, any other derives
1322
+ // from the staging and is marked as having read it.
1323
+ if (o === null) {
1324
+ if (l.l & CONFIG_VERDICT && stagedScreen(l)) return committed(e);
1325
+ stagedRead(l);
1326
+ return r;
1327
+ }
1328
+ }
1329
+ return asBacking(untrack(() => read(o)));
1330
+ }
1331
+
1332
+ /** Did the HELD staging (the container node's, not a mainline layer above
1333
+ * it) change `key`? */ function heldKeyChanged(e, t, n = true) {
1334
+ return changedBetween(e, e.k.le, e.k.ue, t, n) && !adoptChild(e, t);
1335
+ }
1336
+
1337
+ /** The values of a held frame, built once per frame: neither the committed
1338
+ * frame nor the held staging changes while the hold stands (a draft writes a
1339
+ * clone), so the set stays true for every first read of the hold. */ const frameRaws = new WeakMap;
1340
+
1341
+ function rawsOf(e) {
1342
+ let t = frameRaws.get(e);
1343
+ if (t === undefined) frameRaws.set(e, t = new Set(Object.values(e)));
1344
+ return t;
1345
+ }
1346
+
1347
+ /** The same row at a slot, as the adoption pairs rows (reconcile.ts): two
1348
+ * objects whose keys agree — positionally for an unkeyed family. */ function sameRow(e, t, n) {
1349
+ return e !== null && typeof e === "object" && t !== null && typeof t === "object" && (n === null || sameKey(n(e), n(t)));
1350
+ }
1351
+
1352
+ /** A key of a held adoption whose old and new values are the same row that
1353
+ * nothing had proxied when the adoption ran, so `descend` never paired
1354
+ * them: paired now, as it would have been (#3712) — one target, the old
1355
+ * raw its committed frame, the new raw its staging, held by the same
1356
+ * transaction. Both raws resolve to it, so the slot reads as unchanged and
1357
+ * the row's own leaves decide: `id` the same in both frames holds no one,
1358
+ * `version` holds. A record's keys pair by name; an array's by the family's
1359
+ * key — without a family the pairing is not known here and the hold stands. */ function adoptChild(e, t) {
1360
+ const n = e.k;
1361
+ const r = asBacking(n.le);
1362
+ const o = r[t];
1363
+ const i = n.ue[t];
1364
+ const l = Array.isArray(r) ? e.fam?.key : null;
1365
+ if (e.s || l === undefined || !sameRow(o, i, l) || !isWrappable(o) || !isWrappable(i) || Array.isArray(o) !== Array.isArray(i) || lookupTarget(o, e.fam) !== undefined || lookupTarget(i, e.fam) !== undefined ||
1366
+ // A raw in both frames (the same objects swapped or reordered) is its
1367
+ // own row: pairing it with another would alias the two after the commit.
1368
+ rawsOf(r).has(i) || rawsOf(n.ue).has(o)) return false;
1369
+ // A raw-marked value wraps to itself, a proxy to its own target: neither
1370
+ // is a fresh target around `ov`.
1371
+ const u = wrap(o, e, t)[$TARGET];
1372
+ if (u?.v !== o) return false;
1373
+ adoptPB(u, i, false);
1374
+ holdNode(getContainerNode(u), txOf(n));
1375
+ return true;
1376
+ }
1377
+
1378
+ /** A family target's unheld staging, read with no pass (a handler). */ function familyAhead(e) {
1379
+ return e.fam !== null && context === null && !(e.k?.l & CONFIG_HELD);
1380
+ }
1381
+
1382
+ /** Did this batch change `key` — value (slot equality), presence, and (for
1383
+ * a `shape` reader — a descriptor read, a node's hold) enumerability or
1384
+ * accessor-ness? A swapped or non-plain prototype, or a chained backing,
1385
+ * changes every key (#3706: those hold the whole container).
1386
+ *
1387
+ * A VALUE read asks without `shape`: a key whose value is the same in both
1388
+ * frames serves the same value from either, whatever its descriptor did —
1389
+ * and the descriptor probes (`__lookupGetter__`, `propertyIsEnumerable`,
1390
+ * twice each) are the cost of the question on a wide container read under
1391
+ * a staging (a keyed reconcile's 1k rows, read by `mapArray` in the flush
1392
+ * that staged them: ~3× the tick). A container with accessors seen (`a`)
1393
+ * keeps the full test — a getter's answer is not a slot's. */ function keyChanged(e, t, n = true) {
1394
+ return changedBetween(e, committed(e), e.pb, t, n);
1395
+ }
1396
+
1397
+ function changedBetween(e, t, n, r, o) {
1398
+ if (e.ch || n[$TARGET] !== undefined || Object.getPrototypeOf(n) !== Object.getPrototypeOf(t) || !plainProto(t)) return true;
1399
+ if (e.del?.has(r)) return r in t;
1400
+ const i = hasOwn.call(n, r) || e.ovl && hasOwn.call(e.v, r);
1401
+ if (i !== hasOwn.call(t, r)) return true;
1402
+ if (!i) return false;
1403
+ if ((o || e.a) && (isOwnAccessor(t, r) || isOwnAccessor(n, r) || propertyIsEnumerable.call(t, r) !== propertyIsEnumerable.call(n, r))) return true;
1404
+ const l = n[r];
1405
+ const u = t[r];
1406
+ return !isEqual(u, l) && !sameLogicalSlot(e, u, l);
1407
+ }
1408
+
1409
+ /** Value of a key with a leaf: core's — `read()` links a tracked reader and,
1410
+ * tracked or not, applies the hold and A28 rules a signal's read does. A
1411
+ * projection's unheld staging is ahead for a handler (`familyAhead`), and is
1412
+ * the only value while its derive has never committed: the committed side
1413
+ * is the seed, never observable (A25) — a reader core serves the screen (a
1414
+ * verdict reader, #4355) is served the staging, as for a memo born staged
1415
+ * (core `serve`). */ function nodeValue(e, t, n) {
1416
+ let r;
1417
+ if (e.ue !== NOT_PENDING && !(e.l & CONFIG_HELD) && n.fam !== null && (context === null || n.fam.node?.m & STATUS_UNINITIALIZED)) {
1418
+ if (context !== null && tracking) link(e, context);
1419
+ r = e.ue;
1420
+ } else r = read(e);
1421
+ return r === FORCE ? t : r;
1422
+ }
1423
+
1424
+ /** Serve a present data key: link the leaf for a tracked reader (creating it
1425
+ * on first read), wrap a wrappable child through the per-node wrap cache. */ function serveDataKey(e, t, n, r, o, i = -1) {
1426
+ // Read-through (§7b): the inner store's node is the truth; the outer's is
1427
+ // a subscription point only — unless it carries a guess (an optimistic
1428
+ // store over a store: the guess is the outer's, shown over the inner).
1429
+ const l = e.ch && r === e.v;
1430
+ let u = n;
1431
+ // A write-override read (a derive's draft) serves the backing: the derive
1432
+ // composes on the truth, never on a lane's or a hold's frame.
1433
+ if (!inDraft(e) && !writeOverride) {
1434
+ if (o === undefined && getObserver() !== null) o = getNode(e, t, i);
1435
+ if (o !== undefined) {
1436
+ const t = nodeValue(o, n, e);
1437
+ if (l) {
1438
+ // The link's committed value follows the inner store's (what a lane
1439
+ // over it covers — its reveal and revert compare against it, S4).
1440
+ o.le = n;
1441
+ if (o.l & CONFIG_OVERRIDE) u = t;
1442
+ } else u = t;
1443
+ }
1444
+ }
1445
+ if (e.s) return serveShallow(e, t, u);
1446
+ // A derive's draft wrapper answers `$TARGET` without being a proxy: the
1447
+ // inner store's alias (#3767), resolved like a raw (#3859).
1448
+ if (e.ch && !l && u !== null && typeof u === "object" && u[$TARGET]?.px !== u) u = resolveChainedRaw(e, t, u);
1449
+ if (o !== undefined) {
1450
+ if (o.pxv === u && u !== undefined) return draftServe(e, o.px);
1451
+ if (!isWrappable(u)) return u;
1452
+ const n = wrap(u, e, t);
1453
+ o.px = n;
1454
+ o.pxv = u;
1455
+ return draftServe(e, n);
1456
+ }
1457
+ if (!isWrappable(u)) return u;
1458
+ return draftServe(e, wrap(u, e, t));
1459
+ }
1460
+
1461
+ /** Is `n` among `c`'s dependencies (any pass's — a kept tail included)? */ function linkedTo(e, t) {
1462
+ for (let n = e.C; n !== null; n = n.G) if (n.S === t) return true;
1463
+ return false;
1464
+ }
1465
+
1466
+ /** The projection's obligation (plan sec. 31.7 Q-B): before any value is served
1467
+ * through a family target, the derive — the firewall — is brought up to
1468
+ * date WITHOUT the reader subscribing to it. Core's `read()` of the
1469
+ * firewall with no link does all of it: the pull, the status gate
1470
+ * (uninitialized / pending → NotReady, errored → the error; a verdict
1471
+ * window judges it; a pending source registers the reader for the landing)
1472
+ * and the reader's height. The derive's own draft ops are exempt (the write
1473
+ * override): the derive is the author. */ function pullFamily(e) {
1474
+ const t = e.fam.node;
1475
+ if (t === null || writeOverride || inDraft(e)) return;
1476
+ // A settled derive: the pull alone, no link (the barrier). A derive with a
1477
+ // flight up (or errored): the reader observes the flight exactly as a
1478
+ // reader of a memo would — linked, so it re-runs at the landing (the
1479
+ // next settled pass pulls without linking and the stale link trims), a
1480
+ // verdict reader registered, a tracked pass suspended.
1481
+ if (t.m & (STATUS_PENDING | STATUS_ERROR)) {
1482
+ // A render effect outside the flight's own flush is the frame, not a
1483
+ // derivation: it keeps what it shows and learns of the landing from the
1484
+ // leaves the landing changes (unchanged leaves say nothing — it is not a
1485
+ // stale reader of the derive). Inside the flush it suspends like any.
1486
+ // One that already observes the flight (linked by the pass that saw it
1487
+ // go up — the frame's hold, `blocked`) keeps observing it: a re-run for
1488
+ // another reason (a guess it read, the seam) that dropped the link would
1489
+ // release the hold with the flight still up. An errored derive has no
1490
+ // landing to learn of: the error is what the frame shows.
1491
+ const n = context;
1492
+ if (n !== null && n.i === EFFECT_RENDER && !(t.m & STATUS_ERROR) && t.l & CONFIG_HELD && flushTransaction !== txOf(t) && !linkedTo(n, t)) return;
1493
+ // An optimistic family carrying guesses that stand in for the flight
1494
+ // (A17): the guesses are the display — a reader is served the leaves
1495
+ // (committed plus the guesses) and does not suspend on the derive; a
1496
+ // verdict window still asks it (`isPending` is true while the flight is
1497
+ // up). The family's, not the target's: the edit stands in for the one
1498
+ // flight the family has — not over a new question they do not answer
1499
+ // (optimistic.ts `stands`, #4336).
1500
+ if (verdict === null && t.m & STATUS_PENDING && optHooks?.stands(e)) return;
1501
+ read(t);
1502
+ } else untrack(() => read(t));
1503
+ }
1504
+
1505
+ /** A projection's writes are its derive's: a staging made while the derive
1506
+ * is held (a flight up — its continuation writes after an `await`, a
1507
+ * callback's late write) belongs to the derive's transaction and reveals
1508
+ * with it, never drained early — and answers the derive's question: a quiet
1509
+ * re-ask's landing is quiet through its reveal (A19 exc. 2; the leaf's own
1510
+ * classification, as core reads it — `heldNotFinal`). */ function holdWithDerive(e, t) {
1511
+ const n = e.fam?.node;
1512
+ if (n !== undefined && n !== null && !userWrite && // the derive's own write (its draft or landing), not a user setter's
1513
+ n.l & CONFIG_HELD && t.ue !== NOT_PENDING &&
1514
+ // A node carrying a guess is the lane's: its staging is the truth beneath
1515
+ // the guess (`setSignal` → `laneWrite`), never re-homed by the store.
1516
+ !(t.l & (CONFIG_HELD | CONFIG_OVERRIDE))) {
1517
+ holdNode(t, txOf(n));
1518
+ t.T.ut = n.T.ut;
1519
+ }
1520
+ }
1521
+
1522
+ /** A chained backing's child (§7b): a raw the inner family owns, or holds
1523
+ * at this key, is served as the inner store's wrapper — never a fresh
1524
+ * raw-keyed one — so the chained targets are the same objects across
1525
+ * settled and pending views. */ function resolveChainedRaw(e, t, n) {
1526
+ const r = e.v[$TARGET];
1527
+ if (r.ch) {
1528
+ const e = resolveChainedRaw(r, t, n);
1529
+ return e === n ? n : wrap(e, r, t);
1530
+ }
1531
+ const o = lookupTarget(n, r.fam);
1532
+ if (o !== undefined) return o.px;
1533
+ if ((r.v[t] === n || r.pb?.[t] === n) && isWrappable(n)) return wrap(n, r, t);
1534
+ return n;
1535
+ }
1536
+
1537
+ /** The backing a structural read serves: a tracked reader subscribes to the
1538
+ * container and reads the frame core serves it — committed or staging by
1539
+ * the hold rules (`read()` on the node: the node's value IS the backing);
1540
+ * anyone else reads by `readSource`. */ function enumerationSource(e) {
1541
+ if (!inDraft(e) && !writeOverride && getObserver() !== null) {
1542
+ // A container's arrangement guess is not a backing: the view composes
1543
+ // over the base it is shown over.
1544
+ const t = asBacking(read(getContainerNode(e)));
1545
+ return optHooks && e.fam?.opt === true ? optHooks.view(e, t) : t;
1546
+ }
1547
+ const t = readSource(e);
1548
+ return optHooks && optRead(e) ? optHooks.view(e, t) : t;
1549
+ }
1550
+
1551
+ /** A read on an optimistic family that composes the lanes' values: not the
1552
+ * draft's (it composed already), not a derive's write-override read (the
1553
+ * derive composes on the truth). */ function optRead(e) {
1554
+ return e.fam?.opt === true && !inDraft(e) && !writeOverride;
1555
+ }
1556
+
1557
+ /** Pollution keys are never served from the prototype (core R30). */ const UNSAFE_KEYS = new Set([ "__proto__", "prototype", "constructor" ]);
1558
+
1559
+ // ---------------------------------------------------------------------------
1560
+ // traps
1561
+ /** A trap write: the target is told at the setter's exit, the key recorded
1562
+ * in this setter's `wk`. A setter's first write to a target still staged by
1563
+ * an earlier setter of the batch opens a set of its own (the notify visits
1564
+ * it, D-8) and chains the earlier one behind for the fold's union. */ function touch(e, t) {
1565
+ let n = e.wk;
1566
+ if (!pendingNotify.has(e)) {
1567
+ pendingNotify.add(e);
1568
+ if (n !== null && n !== WK_ALL) {
1569
+ n = new Set;
1570
+ n.u = e.wk;
1571
+ e.wk = n;
1572
+ }
1573
+ }
1574
+ if (n !== WK_ALL) (e.wk ??= new Set).add(t);
1575
+ }
1576
+
1577
+ const traps = {
1578
+ get(e, t, n) {
1579
+ // One typeof gates every brand-symbol compare off the hot string path.
1580
+ if (typeof t !== "string") {
1581
+ if (t === $TARGET) return e;
1582
+ if (t === $PROXY) return n;
1583
+ if (t === $OWNER) return undefined;
1584
+ // ownership stamp: never a user key
1585
+ if (t === $RECORD) return undefined;
1586
+ // a store is no view (see `viewOf`)
1587
+ // refresh()/isPending resolve the projection computed through $REFRESH.
1588
+ if (t === $REFRESH) return e.fam?.node ?? undefined;
1589
+ if (t === $TRACK) {
1590
+ if (e.fam !== null) pullFamily(e);
1591
+ if (!inDraft(e) && getObserver() !== null) {
1592
+ read(getContainerNode(e));
1593
+ // Structural chaining (§7b): a chained backing's $TRACK reads
1594
+ // through to the INNER store's container — structural
1595
+ // notifications land on the source's own node.
1596
+ const t = readSource(e);
1597
+ if (t[$TARGET] !== undefined) t[$TRACK];
1598
+ }
1599
+ return undefined;
1600
+ }
1601
+ // user symbols fall through to the generic path
1602
+ }
1603
+ // (The witness before the pull: a mark on an uninitialized derived
1604
+ // store is witnessed, then the pull throws — loading, declared pending.)
1605
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.io(e, t);
1606
+ if (e.fam !== null) pullFamily(e);
1607
+ // Hot inline case: existing PLAIN node (non-accessor) read outside any
1608
+ // draft — the dbmon/uibench effect re-read shape. Core's `read()` serves
1609
+ // it (linking a tracked reader; the hold rules either way). Skips
1610
+ // serveDataKey's frame, the FORCE compare (only accessor keys ever hold
1611
+ // the sentinel), and isWrappable for primitives. ONE node-map lookup
1612
+ // serves this block and the accessor probe below. Before the backing is
1613
+ // consulted: a deleted key's node serves `undefined` (notifyWrites), and
1614
+ // a chained target's node carrying a GUESS is the value (A17 — the base
1615
+ // beneath it, and any hold there, is not this reader's concern; a link
1616
+ // without one reads through below).
1617
+ const r = e.n?.[t];
1618
+ if (writeScopes === null && !writeOverride) {
1619
+ const n = r;
1620
+ if (n !== undefined && n.acc !== true && (!e.ch || n.l & CONFIG_OVERRIDE)) {
1621
+ const r = nodeValue(n, undefined, e);
1622
+ if (r === null || typeof r !== "object") return r;
1623
+ if (e.s) return serveShallow(e, t, r);
1624
+ if (n.pxv === r) return n.px;
1625
+ if (isWrappable(r)) {
1626
+ const o = wrap(r, e, t);
1627
+ n.px = o;
1628
+ n.pxv = r;
1629
+ return o;
1630
+ }
1631
+ return r;
1632
+ }
1633
+ }
1634
+ const o = readSource(e, t);
1635
+ // Overlay delete (#3044): a prototype overlay cannot shadow a delete, so
1636
+ // deleted keys are tracked aside and read as absent in the pending view.
1637
+ if (o === e.pb && e.del?.has(t)) {
1638
+ if (!inDraft(e) && getObserver() !== null) read(getNode(e, t));
1639
+ return undefined;
1640
+ }
1641
+ // Accessor keys serve through Reflect.get with the PROXY receiver
1642
+ // (R20/R29: internal reads track; the node is linked for shape-change
1643
+ // notification but its value is never served). Accessor-ness comes from
1644
+ // the node's cached flag; the first TRACKED read (which creates the
1645
+ // node) probes once — untracked node-less reads take the plain path,
1646
+ // where a raw-receiver getter still returns correct committed values.
1647
+ // Tracking suppression is PER-TARGET (inDraft), never global (#3037).
1648
+ let i = -1;
1649
+ {
1650
+ let l;
1651
+ if (r !== undefined) l = r.acc === true; else if (!inDraft(e) && getObserver() !== null) {
1652
+ l = isOwnAccessor(o, t);
1653
+ if (o === (e.pb ?? e.v)) i = l ? 1 : 0;
1654
+ } else l = false;
1655
+ if (l) {
1656
+ if (!inDraft(e) && getObserver() !== null) read(r ?? getNode(e, t, i));
1657
+ const l = Reflect.get(o, t, n);
1658
+ if (e.s) return serveShallow(e, t, l);
1659
+ return isWrappable(l) ? draftServe(e, wrap(l, e, t)) : l;
1660
+ }
1661
+ }
1662
+ // Plain-data fast path: no descriptor allocation per read. Inherited
1663
+ // pollution keys are never served (core R30) — checked before the
1664
+ // proto-function branch can leak `constructor`. Overlay pending backings
1665
+ // chain to the committed backing, so "own in the view" means own on
1666
+ // either layer — a genuine prototype method is one that is own on
1667
+ // NEITHER.
1668
+ const l = e.ovl && o === e.pb;
1669
+ if ((t === "constructor" || t === "__proto__" || t === "prototype") && !hasOwn.call(o, t) && !(l && hasOwn.call(e.v, t))) return undefined;
1670
+ let u = o[t];
1671
+ if (u === undefined ? !hasOwn.call(o, t) && !(l && hasOwn.call(e.v, t)) : false) {
1672
+ // Inherited: prototype getters/methods run with the proxy receiver.
1673
+ u = Reflect.get(o, t, n);
1674
+ if (typeof u === "function") return u;
1675
+ // proto methods untracked
1676
+ // Read-through (§7b): the inner store answers an absent key too — the
1677
+ // outer's node is a subscription point unless it carries a guess.
1678
+ if (e.ch && o === e.v) return serveDataKey(e, t, u, o, r, i);
1679
+ // Reading a currently-absent own key subscribes to it (R12) — for any
1680
+ // target OUTSIDE its own draft scope, even mid-setter (#3037, above).
1681
+ if (u === undefined && !inDraft(e)) {
1682
+ // (`nodeValue` reads — and links — the node it creates.)
1683
+ if (getObserver() !== null) getNode(e, t, i);
1684
+ const n = e.n?.[t];
1685
+ if (n) {
1686
+ const r = nodeValue(n, undefined, e);
1687
+ if (e.s) return serveShallow(e, t, r);
1688
+ return isWrappable(r) ? draftServe(e, wrap(r, e, t)) : r;
1689
+ }
1690
+ }
1691
+ if (e.s) return serveShallow(e, t, u);
1692
+ return isWrappable(u) ? draftServe(e, wrap(u, e, t)) : u;
1693
+ }
1694
+ if (typeof u === "function" && !hasOwn.call(o, t) && !(l && hasOwn.call(e.v, t))) return u;
1695
+ // proto method
1696
+ return serveDataKey(e, t, u, o, r, i);
1697
+ },
1698
+ has(e, t) {
1699
+ if (t === $TARGET || t === $PROXY || t === $TRACK) return true;
1700
+ if (t === $OWNER || t === $RECORD) return false;
1701
+ // (The witness before the pull: a mark on an uninitialized derived
1702
+ // store is witnessed, then the pull throws — loading, declared pending.)
1703
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.io(e, t);
1704
+ if (e.fam !== null) pullFamily(e);
1705
+ const n = readSource(e, t);
1706
+ // A tracked reader's presence node answers (born from the two frames;
1707
+ // core decides which it sees); everyone else reads the backing. A
1708
+ // chained target's node is a subscription point (§7b): the inner store
1709
+ // answers through the backing — unless the outer node carries a guess.
1710
+ if (!inDraft(e) && !writeOverride) {
1711
+ if (getObserver() !== null) {
1712
+ const r = getHasNode(e, t);
1713
+ const o = read(r);
1714
+ if (!e.ch || n !== e.v) return !!o;
1715
+ const i = t in n;
1716
+ r.le = i;
1717
+ // the link follows the inner store (S4)
1718
+ if (r.l & CONFIG_OVERRIDE) return !!o;
1719
+ return i;
1720
+ } else if (optHooks && optRead(e)) {
1721
+ const n = optHooks.has(e, t);
1722
+ if (n !== undefined) return n;
1723
+ }
1724
+ }
1725
+ return t in n && !(n === e.pb && e.del?.has(t));
1726
+ },
1727
+ ownKeys(e) {
1728
+ if (verdict !== null && affectsHooks !== null && getObserver() === null) affectsHooks.io(e, undefined);
1729
+ if (e.fam !== null) pullFamily(e);
1730
+ return visibleKeys(e, enumerationSource(e));
1731
+ },
1732
+ getOwnPropertyDescriptor(e, t) {
1733
+ if (t === $OWNER || t === $RECORD) return undefined;
1734
+ const n = getObserver();
1735
+ if (verdict !== null && affectsHooks !== null && n === null) affectsHooks.io(e, t);
1736
+ if (e.fam !== null) pullFamily(e);
1737
+ // An enumerator (spread, Object.entries) already holds the container
1738
+ // node and reads its frame; a descriptor read on its own tracks
1739
+ // presence (R13).
1740
+ const r = n !== null && !inDraft(e) && !writeOverride;
1741
+ const o = r && observerHoldsContainer(e, n);
1742
+ let i;
1743
+ if (o) i = enumerationSource(e); else if (r) {
1744
+ // The presence node's frame (core decides which) is the one described
1745
+ // (a chained target's inner store answers through the backing, §7b);
1746
+ // the descriptor itself comes from the backing the container's frame
1747
+ // serves this reader (`readSource`: a held container's changed key —
1748
+ // enumerability, accessor-ness — reads committed for a stale reader,
1749
+ // the staging for a member; #3706 contrast).
1750
+ const n = getHasNode(e, t);
1751
+ const r = read(n);
1752
+ i = readSource(e, t, true);
1753
+ if (!r && (!e.ch || n.l & CONFIG_OVERRIDE || !(t in i))) return undefined;
1754
+ } else i = readSource(e, t, true);
1755
+ let l;
1756
+ if (optHooks && !o && optRead(e)) {
1757
+ const n = optHooks.descriptor(e, t);
1758
+ if (n === null) return undefined;
1759
+ l = n;
1760
+ }
1761
+ l ??= visibleDescriptor(e, i, t);
1762
+ if (l === undefined) return undefined;
1763
+ if (!(t === "length" && Array.isArray(e))) l.configurable = true;
1764
+ return l;
1765
+ },
1766
+ set(e, t, n) {
1767
+ const r = inDraft(e);
1768
+ if (!r && !writeOverride) {
1769
+ return true;
1770
+ }
1771
+ if (t === "__proto__") return true;
1772
+ // pollution guard (core R30)
1773
+ const o = e.s ? n : unwrapValue(n);
1774
+ const i = ensurePB(e);
1775
+ // #4146 (R1: node existence is unobservable; #3706: the hold's unit is
1776
+ // the key): a user setter writing a key a HELD container's staging left
1777
+ // unchanged would write it into the held staging — read as the hold's
1778
+ // until its landing, while the same key with a leaf (a live reader)
1779
+ // publishes at the flush. The key gets its leaf first, born from the two
1780
+ // frames (`bornStaged`: a key the hold left unchanged — or added — is
1781
+ // unstaged and held by no one; a key the hold changed is born held, A34
1782
+ // (1)), and the write stages on it (A28; a write by the holder joins it,
1783
+ // A34) — as it would with a reader's leaf. Kept while the container is
1784
+ // held (`deferredReleases`).
1785
+ if (userWrite && e.k?.l & CONFIG_HELD) deferredReleases.add(getNode(e, t));
1786
+ if (Array.isArray(i)) {
1787
+ touch(e, t);
1788
+ if (t === "length") e.wk = WK_ALL; else touch(e, "length");
1789
+ } else {
1790
+ touch(e, t);
1791
+ if (!(t in i)) e.kc++;
1792
+ }
1793
+ const l = UNSAFE_KEYS.has(t);
1794
+ if (l || e.ovl && e.sc !== 2 && !hasOwn.call(i, t)) {
1795
+ Object.defineProperty(i, t, {
1796
+ value: o,
1797
+ writable: true,
1798
+ enumerable: true,
1799
+ configurable: true
1800
+ });
1801
+ } else i[t] = o;
1802
+ e.del?.delete(t);
1803
+ if (l) return true;
1804
+ if (e.s && o !== null && typeof o === "object") markRawOne(o);
1805
+ if (!r) notifyWrites(e);
1806
+ // a write-override write
1807
+ return true;
1808
+ },
1809
+ defineProperty(e, t, n) {
1810
+ const r = inDraft(e);
1811
+ if (!r && !writeOverride) {
1812
+ return true;
1813
+ }
1814
+ if (t === "__proto__") return true;
1815
+ if (n.get || n.set) e.a = true;
1816
+ if ("value" in n) n = {
1817
+ ...n,
1818
+ value: unwrapValue(n.value)
1819
+ };
1820
+ const o = ensurePB(e);
1821
+ if (e.a || !(n.enumerable && n.writable && n.configurable)) e.sc = 1;
1822
+ touch(e, t);
1823
+ Object.defineProperty(o, t, n);
1824
+ e.del?.delete(t);
1825
+ if (!r) notifyWrites(e);
1826
+ // a write-override write
1827
+ return true;
1828
+ },
1829
+ deleteProperty(e, t) {
1830
+ const n = inDraft(e);
1831
+ if (!n && !writeOverride) {
1832
+ return true;
1833
+ }
1834
+ const r = ensurePB(e);
1835
+ touch(e, t);
1836
+ // The key count (`kc`, the overlay gate and the rebuild cap) follows a
1837
+ // delete the way it follows an add: an own key of the draft leaving — a
1838
+ // clone's key, or an overlay's own add — is one fewer now. An overlay's
1839
+ // override of a committed key is the exception: the committed key's
1840
+ // deletion is recorded in `del` and counted at the rebuild, as before.
1841
+ // Without the decrement the natural "delete the old key, set the new
1842
+ // one" shape drifted the count up by one per write until a three-key
1843
+ // object crossed the overlay threshold and paid a rebuild on every
1844
+ // commit after.
1845
+ const o = e.ovl && hasOwn.call(e.v, t);
1846
+ if (hasOwn.call(r, t)) {
1847
+ delete r[t];
1848
+ o || e.kc--;
1849
+ }
1850
+ if (o) (e.del ??= new Set).add(t);
1851
+ if (!n) notifyWrites(e);
1852
+ // a write-override write
1853
+ return true;
1854
+ }
1855
+ };
1856
+
1857
+ /** Is `obs` subscribed to the container BY THIS PASS (its enumerator holds
1858
+ * it)? A link left from a previous pass is stale — the reader must subscribe
1859
+ * to the key's presence node or a later add never re-runs it. */ function observerHoldsContainer(e, t) {
1860
+ const n = e.k;
1861
+ if (n === null) return false;
1862
+ const r = n.Ot;
1863
+ return r !== null && r.u === t && (!(t.R & REACTIVE_RECOMPUTING_DEPS) || r.de === t.Ne);
1864
+ }
1865
+
1866
+ function storeSetter(e, t, n = true) {
1867
+ if (writeHook !== null) writeHook(e);
1868
+ const r = e[$TARGET];
1869
+ const o = writeScopes;
1870
+ writeScopes = new Set;
1871
+ writeScopes.add(scopeKey(r));
1872
+ const i = userWrite;
1873
+ userWrite = n;
1874
+ writing++;
1875
+ let l;
1876
+ try {
1877
+ // No untrack: the writing flag already disables store-node linking
1878
+ // (draft reads never self-track, proj R2), while EXTERNAL reads (signals
1879
+ // inside a projection derive) must keep tracking — they are the derive's
1880
+ // dependencies.
1881
+ l = t(e);
1882
+ } finally {
1883
+ writing--;
1884
+ writeScopes = o;
1885
+ // Outermost setter exit: emit write-time notifications (one node write
1886
+ // per changed observed key) so holds and lanes engage now.
1887
+ if (writing === 0 && pendingNotify.size) {
1888
+ const e = [ ...pendingNotify ];
1889
+ pendingNotify.clear();
1890
+ for (const t of e) notifyWrites(t);
1891
+ // A direct commit (a projection's creation run) folds at the setter's
1892
+ // exit, unqueued: the backing is the pre-batch one (nothing swapped).
1893
+ if (directCommit) for (const t of e) if (t.pb !== null) foldTarget(t, t.v);
1894
+ }
1895
+ userWrite = i;
1896
+ }
1897
+ if (l !== undefined && l !== e && isWrappable(l)) {
1898
+ // A returned replacement: on an optimistic family a user's is itself an
1899
+ // optimistic edit — diffed against the view as guesses (`next` parity);
1900
+ // otherwise it is adopted as the incoming truth.
1901
+ if (optHooks && n && r.fam?.opt === true && !writeOverride) optHooks.writes(r, unwrapValue(l)); else adoptPB(r, unwrapValue(l));
1902
+ }
1903
+ // A34 (3), #3612 — the derived store's twin of `setMemo`: a user write to
1904
+ // a derivation another transaction holds is nobody's proposal; it becomes
1905
+ // the draft's prior state and the hold re-derives over it (the writes
1906
+ // joined the hold through `setSignal`; the derive re-runs under it).
1907
+ if (n && heldDerivationHit) {
1908
+ heldDerivationHit = false;
1909
+ const e = r.fam.node;
1910
+ if (!(e.R & REACTIVE_DIRTY)) {
1911
+ e.R = e.R & ~REACTIVE_CHECK | REACTIVE_DIRTY;
1912
+ insertIntoHeap(e, dirtyQueue);
1913
+ schedule();
1914
+ }
1915
+ }
1916
+ }
1917
+
1918
+ function createStore(e, t) {
1919
+ const n = t?.shallow === true;
1920
+ const r = wrap(e);
1921
+ // Dependency edges (store/edges.ts): the root is named like a plain signal.
1922
+ if (signalHook !== null) signalHook(r[$TARGET]);
1923
+ if (n) {
1924
+ r[$TARGET].s = true;
1925
+ markRawIngest(e);
1926
+ }
1927
+ {
1928
+ const e = getOwner();
1929
+ registerGraph(r, e);
1930
+ // Only once the engine is installed, like the node stamping it feeds: a
1931
+ // WeakMap.set per fresh store is a growing ephemeron table and,
1932
+ // disabled, buys nothing.
1933
+ if (attrHooks !== null) {
1934
+ storeOwners.set(r[$TARGET], e);
1935
+ nameStore(r, t?.name, t?.loc);
1936
+ }
1937
+ }
1938
+ const setter = e => storeSetter(r, e);
1939
+ return [ r, setter ];
1940
+ }
1941
+
1942
+ /** The list driver's slot view of a shallow store (types.ts `shallowView`):
1943
+ * one `readSource` for the container instead of a trap per slot. A shallow
1944
+ * store's slots hold raw records, so the backing the container read serves
1945
+ * is what each slot read would — except where a slot's own leaf can answer
1946
+ * differently: an optimistic family's guesses, a chained backing, accessor
1947
+ * keys, or a read inside a draft or write scope. Those keep the trap. */ installShallowView(e => {
1948
+ // Any open write scope keeps the trap (it covers `inDraft`).
1949
+ if (e?.s && !(e.ch || e.a || writeScopes || writeOverride || e.fam?.opt)) return e.fam && pullFamily(e),
1950
+ readSource(e);
1951
+ });
1952
+
1953
+ /** True when `proxy` is a SHALLOW store (children served verbatim, slots
1954
+ * replaced by reference — #2932). The list driver uses this to choose the
1955
+ * slot-patch channel (collected row bodies) over per-record registration. */ function storeIsShallow(e) {
1956
+ const t = e?.[$TARGET];
1957
+ return t?.s === true;
1958
+ }
1959
+
1960
+ /** True when `proxy` belongs to a projection family (S3). */ function storeHasFamily(e) {
1961
+ const t = e?.[$TARGET];
1962
+ return t?.fam != null;
1963
+ }
1964
+
1965
+ /** True when `proxy` belongs to an OPTIMISTIC family (S4). */ function storeHasOptimisticFamily(e) {
1966
+ const t = e?.[$TARGET];
1967
+ return t?.fam?.opt === true;
1968
+ }
1969
+
1970
+ // ---------------------------------------------------------------------------
1971
+ // enumeration views (one visibility rule for the traps AND the deep walk)
1972
+ function visibleKeys(e, t) {
1973
+ let n;
1974
+ if (e.ovl && t === e.pb) {
1975
+ n = Reflect.ownKeys(e.v);
1976
+ const r = e.del;
1977
+ if (r?.size) n = n.filter(e => !r.has(e));
1978
+ for (const r of Reflect.ownKeys(t)) {
1979
+ if (!hasOwn.call(e.v, r)) n.push(r);
1980
+ }
1981
+ } else n = Reflect.ownKeys(t);
1982
+ // The ownership stamp is the last symbol on an owned backing: hide it.
1983
+ for (let e = n.length - 1; e >= 0 && typeof n[e] === "symbol"; e--) {
1984
+ if (n[e] === $OWNER) {
1985
+ n.splice(e, 1);
1986
+ break;
1987
+ }
1988
+ }
1989
+ return n;
1990
+ }
1991
+
1992
+ function visibleDescriptor(e, t, n) {
1993
+ let r = Object.getOwnPropertyDescriptor(t, n);
1994
+ if (e.ovl && t === e.pb) {
1995
+ if (e.del?.has(n)) return undefined;
1996
+ if (r === undefined) r = Object.getOwnPropertyDescriptor(e.v, n);
1997
+ }
1998
+ return r;
1999
+ }
2000
+
2001
+ // ---------------------------------------------------------------------------
2002
+ // deep / snapshot
2003
+ /** Tracking snapshot: subscribes every reachable record's container and deep
2004
+ * witness (one node per record, not one per path), then snapshots. */ function deep(e) {
2005
+ const t = e?.[$TARGET];
2006
+ if (t?.px !== e) return e;
2007
+ const n = new Set;
2008
+ const childTarget = (e, t, n) => {
2009
+ let r = lookupTarget(t, e.fam);
2010
+ if (r === undefined) {
2011
+ if (!isWrappable(t)) return undefined;
2012
+ wrap(t, e, n);
2013
+ r = lookupTarget(t, e.fam) ?? t[$TARGET];
2014
+ }
2015
+ return r;
2016
+ };
2017
+ const walkT = e => {
2018
+ if (e.fam !== null) pullFamily(e);
2019
+ const t = enumerationSource(e);
2020
+ if (n.has(t)) return;
2021
+ n.add(t);
2022
+ read(getDeepNode(e));
2023
+ // Through a chain (#3323): every inner record's container AND deep
2024
+ // witness — base writes bump the inner witnesses.
2025
+ for (let t = e; t.ch; ) {
2026
+ t = t.v[$TARGET];
2027
+ read(getContainerNode(t));
2028
+ read(getDeepNode(t));
2029
+ }
2030
+ for (const n of visibleKeys(e, t)) {
2031
+ const r = visibleDescriptor(e, t, n);
2032
+ if (r === undefined) continue;
2033
+ if (r.get || r.set) {
2034
+ e.a = true;
2035
+ continue;
2036
+ // accessors track through their own reads when invoked
2037
+ }
2038
+ let o = r.value;
2039
+ if (o === null || typeof o !== "object") continue;
2040
+ if (e.ch && o[$TARGET] === undefined) o = resolveChainedRaw(e, n, o);
2041
+ const i = childTarget(e, o, n);
2042
+ if (i === undefined) continue;
2043
+ // raw-marked: leaf by contract
2044
+ walkT(i);
2045
+ }
2046
+ };
2047
+ walkT(t);
2048
+ return snapshot(e);
2049
+ }
2050
+
2051
+ /** Non-tracking snapshot: source identity for unowned subtrees (zero copy),
2052
+ * a copy for owned ones (they are by definition modified relative to the
2053
+ * source). Sees the pending backing (R27). */ function snapshot(e) {
2054
+ const t = e?.[$TARGET];
2055
+ return snapshotWalk(e, new Map, t?.fam ?? null);
2056
+ }
2057
+
2058
+ function snapshotWalk(e, t, n) {
2059
+ if (e === null || typeof e !== "object") return e;
2060
+ let r = e;
2061
+ for (let e = true; ;e = false) {
2062
+ const t = r?.[$TARGET]?.v !== undefined;
2063
+ let o = t ? r[$TARGET] : undefined;
2064
+ if (o === undefined && n !== null) o = lookupTarget(r, n);
2065
+ if (o === undefined) o = lookupTarget(r, null);
2066
+ if (o === undefined) break;
2067
+ if (e && !t && n !== null && o.fam !== n) {
2068
+ const e = n.map.get(o.px);
2069
+ if (e !== undefined) o = e;
2070
+ }
2071
+ if (o.fam !== null) n = o.fam;
2072
+ // R27: a snapshot sees the batch's pending backing — a held one (another
2073
+ // transaction's future) only from its own draft.
2074
+ let i = o.v;
2075
+ if (o.pb !== null && (inDraft(o) || writeOverride || o.k === null || !(o.k.l & CONFIG_HELD))) {
2076
+ if (o.ovl) materializePB(o);
2077
+ i = o.pb;
2078
+ }
2079
+ if (optHooks && optRead(o)) i = optHooks.view(o, i);
2080
+ if (i === r) break;
2081
+ r = i;
2082
+ }
2083
+ if (!isWrappable(r)) return r;
2084
+ const o = t.get(r);
2085
+ if (o !== undefined) return o;
2086
+ if (isOwned(r)) {
2087
+ const e = Array.isArray(r);
2088
+ const o = e ? [] : Object.create(Object.getPrototypeOf(r));
2089
+ t.set(r, o);
2090
+ for (const i of Reflect.ownKeys(r)) {
2091
+ if (e && i === "length" || i === $OWNER) continue;
2092
+ const l = Object.getOwnPropertyDescriptor(r, i);
2093
+ if (typeof i === "symbol" && !l.enumerable) continue;
2094
+ if (l.get || l.set) {
2095
+ Object.defineProperty(o, i, l);
2096
+ continue;
2097
+ }
2098
+ const u = l.value;
2099
+ const f = u !== null && typeof u === "object" ? snapshotWalk(u, t, n) : u;
2100
+ if (l.enumerable && l.writable && l.configurable) o[i] = f; else Object.defineProperty(o, i, {
2101
+ ...l,
2102
+ value: f
2103
+ });
2104
+ }
2105
+ if (e && o.length !== r.length) o.length = r.length;
2106
+ return o;
2107
+ }
2108
+ t.set(r, r);
2109
+ let i = null;
2110
+ for (const e of Reflect.ownKeys(r)) {
2111
+ const o = Object.getOwnPropertyDescriptor(r, e);
2112
+ if (!o || o.get || o.set) continue;
2113
+ const l = o.value;
2114
+ if (l === null || typeof l !== "object") continue;
2115
+ const u = snapshotWalk(l, t, n);
2116
+ if (u !== l) {
2117
+ if (i === null) {
2118
+ i = Array.isArray(r) ? [ ...r ] : Object.create(Object.getPrototypeOf(r), Object.getOwnPropertyDescriptors(r));
2119
+ t.set(r, i);
2120
+ }
2121
+ i[e] = u;
2122
+ }
2123
+ }
2124
+ return i ?? r;
2125
+ }
2126
+
2127
+ export { $PROXY, $TARGET, $TRACK, LaneView, adoptPB, arrayStructureChanged, asBacking, bumpDeep, cloneRaw, committed, containerEquals, createStore, deep, getContainerNode, getHasNode, getNode, hasAccessorFlag, materializePB, membershipChanged, nameStore, notifyFold, notifyFoldTail, notifyKeyDiff, notifyKeyValue, resolveChainedRaw, runDirect, sameKey, sameRow, snapshot, storeHasFamily, storeHasOptimisticFamily, storeIsShallow, storeSetter, targetsEqual, unwrapValue, userWriting, wrap };