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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. package/dist/dev-shared.js +6357 -0
  2. package/dist/dev.attribution.js +2485 -0
  3. package/dist/dev.js +2851 -7027
  4. package/dist/observe/affects.js +129 -0
  5. package/dist/observe/attribution.js +7 -0
  6. package/dist/observe/boundaries.js +641 -0
  7. package/dist/observe/core/action.js +177 -0
  8. package/dist/observe/core/async.js +846 -0
  9. package/dist/observe/core/attribution-costs.js +66 -0
  10. package/dist/observe/core/attribution-feedback.js +282 -0
  11. package/dist/observe/core/attribution-hooks.js +82 -0
  12. package/dist/observe/core/attribution-queries.js +28 -0
  13. package/dist/observe/core/attribution.js +2095 -0
  14. package/dist/observe/core/constants.js +248 -0
  15. package/dist/{types-cjs/core/context.d.cts → observe/core/context.js} +37 -12
  16. package/dist/observe/core/core.js +1789 -0
  17. package/dist/observe/core/dev.js +293 -0
  18. package/dist/observe/core/effect.js +195 -0
  19. package/dist/observe/core/error-hooks.js +71 -0
  20. package/dist/observe/core/error.js +80 -0
  21. package/dist/observe/core/external.js +98 -0
  22. package/dist/observe/core/graph.js +155 -0
  23. package/dist/observe/core/heap.js +147 -0
  24. package/dist/observe/core/invariants.js +45 -0
  25. package/dist/observe/core/lanes.js +205 -0
  26. package/dist/observe/core/optimistic.js +585 -0
  27. package/dist/observe/core/owner.js +322 -0
  28. package/dist/observe/core/scheduler.js +1428 -0
  29. package/dist/observe/core/verdict.js +669 -0
  30. package/dist/observe/index.js +59 -0
  31. package/dist/observe/map.js +484 -0
  32. package/dist/{types-cjs/signals.d.cts → observe/signals.js} +311 -317
  33. package/dist/observe/store/index.js +40 -0
  34. package/dist/observe/store/next/optimistic.js +660 -0
  35. package/dist/observe/store/next/projection.js +255 -0
  36. package/dist/observe/store/next/reconcile.js +375 -0
  37. package/dist/observe/store/next/store.js +2327 -0
  38. package/dist/observe/store/next/target.js +51 -0
  39. package/dist/observe/store/store.js +325 -0
  40. package/dist/observe/store/storePath.js +103 -0
  41. package/dist/observe/store/utils.js +1023 -0
  42. package/dist/prod/attribution.js +41 -0
  43. package/dist/prod/boundaries.js +128 -76
  44. package/dist/prod/core/action.js +65 -37
  45. package/dist/prod/core/async.js +324 -172
  46. package/dist/prod/core/constants.js +57 -1
  47. package/dist/prod/core/context.js +3 -3
  48. package/dist/prod/core/core.js +1003 -364
  49. package/dist/prod/core/dev.js +17 -1
  50. package/dist/prod/core/effect.js +76 -42
  51. package/dist/prod/core/error-hooks.js +71 -0
  52. package/dist/prod/core/external.js +4 -4
  53. package/dist/prod/core/graph.js +37 -37
  54. package/dist/prod/core/heap.js +61 -56
  55. package/dist/prod/core/lanes.js +107 -63
  56. package/dist/prod/core/optimistic.js +374 -93
  57. package/dist/prod/core/owner.js +62 -46
  58. package/dist/prod/core/scheduler.js +653 -286
  59. package/dist/prod/core/verdict.js +245 -127
  60. package/dist/prod/index.js +15 -5
  61. package/dist/prod/map.js +292 -141
  62. package/dist/prod/signals.js +29 -16
  63. package/dist/prod/store/next/optimistic.js +77 -55
  64. package/dist/prod/store/next/projection.js +31 -20
  65. package/dist/prod/store/next/reconcile.js +87 -84
  66. package/dist/prod/store/next/store.js +718 -401
  67. package/dist/prod/store/next/target.js +32 -10
  68. package/dist/prod/store/store.js +12 -14
  69. package/dist/prod/store/utils.js +950 -130
  70. package/dist/types/attribution.d.ts +20 -0
  71. package/dist/types/attribution.prod.d.ts +22 -0
  72. package/dist/types/boundaries.d.ts +10 -1
  73. package/dist/types/core/action.d.ts +12 -5
  74. package/dist/types/core/async.d.ts +1 -1
  75. package/dist/types/core/attribution-costs.d.ts +35 -0
  76. package/dist/types/core/attribution-feedback.d.ts +133 -0
  77. package/dist/types/core/attribution-hooks.d.ts +147 -9
  78. package/dist/types/core/attribution-queries.d.ts +10 -0
  79. package/dist/types/core/attribution.d.ts +216 -182
  80. package/dist/types/core/constants.d.ts +56 -0
  81. package/dist/types/core/core.d.ts +190 -2
  82. package/dist/types/core/dev.d.ts +246 -28
  83. package/dist/types/core/effect.d.ts +8 -1
  84. package/dist/types/core/error-hooks.d.ts +71 -0
  85. package/dist/types/core/index.d.ts +4 -1
  86. package/dist/types/core/invariants.d.ts +4 -0
  87. package/dist/types/core/lanes.d.ts +39 -5
  88. package/dist/types/core/scheduler.d.ts +153 -0
  89. package/dist/types/core/types.d.ts +32 -9
  90. package/dist/types/index.d.ts +10 -3
  91. package/dist/types/map.d.ts +2 -0
  92. package/dist/types/signals.d.ts +8 -0
  93. package/dist/types/store/index.d.ts +2 -0
  94. package/dist/types/store/next/optimistic.d.ts +1 -1
  95. package/dist/types/store/next/store.d.ts +11 -5
  96. package/dist/types/store/next/target.d.ts +37 -10
  97. package/dist/types/store/utils.d.ts +178 -0
  98. package/package.json +21 -17
  99. package/dist/node.cjs +0 -10541
  100. package/dist/node.dev.cjs +0 -13724
  101. package/dist/types-cjs/affects.d.cts +0 -47
  102. package/dist/types-cjs/boundaries.d.cts +0 -175
  103. package/dist/types-cjs/core/action.d.cts +0 -64
  104. package/dist/types-cjs/core/async.d.cts +0 -23
  105. package/dist/types-cjs/core/attribution-hooks.d.cts +0 -127
  106. package/dist/types-cjs/core/attribution.d.cts +0 -495
  107. package/dist/types-cjs/core/constants.d.cts +0 -159
  108. package/dist/types-cjs/core/core.d.cts +0 -185
  109. package/dist/types-cjs/core/dev.d.cts +0 -136
  110. package/dist/types-cjs/core/effect.d.cts +0 -30
  111. package/dist/types-cjs/core/error.d.cts +0 -56
  112. package/dist/types-cjs/core/external.d.cts +0 -15
  113. package/dist/types-cjs/core/graph.d.cts +0 -28
  114. package/dist/types-cjs/core/heap.d.cts +0 -24
  115. package/dist/types-cjs/core/index.d.cts +0 -13
  116. package/dist/types-cjs/core/invariants.d.cts +0 -59
  117. package/dist/types-cjs/core/lanes.d.cts +0 -54
  118. package/dist/types-cjs/core/optimistic.d.cts +0 -6
  119. package/dist/types-cjs/core/owner.d.cts +0 -124
  120. package/dist/types-cjs/core/scheduler.d.cts +0 -236
  121. package/dist/types-cjs/core/types.d.cts +0 -204
  122. package/dist/types-cjs/core/verdict.d.cts +0 -2
  123. package/dist/types-cjs/index.d.cts +0 -10
  124. package/dist/types-cjs/map.d.cts +0 -69
  125. package/dist/types-cjs/package.json +0 -3
  126. package/dist/types-cjs/store/index.d.cts +0 -18
  127. package/dist/types-cjs/store/next/optimistic.d.cts +0 -25
  128. package/dist/types-cjs/store/next/projection.d.cts +0 -8
  129. package/dist/types-cjs/store/next/reconcile.d.cts +0 -10
  130. package/dist/types-cjs/store/next/store.d.cts +0 -121
  131. package/dist/types-cjs/store/next/target.d.cts +0 -166
  132. package/dist/types-cjs/store/store.d.cts +0 -143
  133. package/dist/types-cjs/store/storePath.d.cts +0 -58
  134. package/dist/types-cjs/store/utils.d.cts +0 -74
@@ -0,0 +1,255 @@
1
+ import { computed, suppressComputedRecompute } from "../../core/core.js";
2
+
3
+ import { getOwner } from "../../core/owner.js";
4
+
5
+ import { setProjectionWriteActive, projectionWriteActive } from "../../core/scheduler.js";
6
+
7
+ import { handleAsync } from "../../core/async.js";
8
+
9
+ import "../../core/verdict.js";
10
+
11
+ import "../../core/effect.js";
12
+
13
+ import { CONFIG_AUTO_DISPOSE } from "../../core/constants.js";
14
+
15
+ import "../../core/dev.js";
16
+
17
+ import { $TARGET, STORE_VALUE, markRawIngest, setWriteOverride } from "../store.js";
18
+
19
+ import { reconcileNextState } from "./reconcile.js";
20
+
21
+ import { storeSetterNext, wrapNext } from "./store.js";
22
+
23
+ /**
24
+ * Store rewrite — projections (§7/§7b): a projection is a computed store.
25
+ * The derive runs inside a computed whose recompute merges its output into
26
+ * the projection's backing through the adoption channel (replace-mode root:
27
+ * entity changes merge in place, the root proxy is stable for life). Children
28
+ * wrap into the projection's own FAMILY (writes land here, never in a source
29
+ * family), and every family node carries the projection computed as its
30
+ * firewall — reads link the derive's status and lifecycle natively. The §6c
31
+ * status gate in the traps makes an uninitialized async derive's seed
32
+ * unobservable through every read surface.
33
+ *
34
+ * Mirrors the legacy runProjectionComputed shape (shadow runs for open
35
+ * loading windows, handleAsync landings, commit-through-setter) on next
36
+ * primitives; the generic draft write-traps are reused from the legacy
37
+ * module unchanged.
38
+ */
39
+ /**
40
+ * Wrap a store proxy as a projection DRAFT: every operation carries the write
41
+ * override (the derive is the author — its ops must not hit the §6c firewall
42
+ * gate, even in a continuation after an `await`/`yield` where the sync write
43
+ * scope has closed).
44
+ *
45
+ * FAKE TARGET, not the store proxy itself (#3060): after a proxy trap
46
+ * returns, the engine runs spec invariant validation against the proxy's
47
+ * TARGET — [[OwnPropertyKeys]] after ownKeys, [[GetOwnProperty]] after
48
+ * set/getOwnPropertyDescriptor/defineProperty. With the store proxy as
49
+ * target those checks re-enter the store's traps OUTSIDE the override
50
+ * bracket (the trap's finally has already run), so `Object.keys(state)` in
51
+ * a derive continuation fired the firewall gate and re-threw the
52
+ * projection's own pending NotReadyError into the derive. A dummy of
53
+ * matching kind (array/object, same trick as the store's own TargetShape)
54
+ * keeps invariant validation away from the store entirely; the traps
55
+ * forward to the closed-over inner proxy inside the bracket.
56
+ *
57
+ * Save/restore projectionWriteActive, never hard-reset: the draft can be
58
+ * driven from inside an enclosing authoritative-write scope (next-store
59
+ * optimistic derives), and a hard `false` would clobber it mid-derive.
60
+ */ function wrapDraft(e, t, r, i) {
61
+ const write = e => r ? r(e) : e();
62
+ const o = {
63
+ get(o, n) {
64
+ let c;
65
+ const s = projectionWriteActive;
66
+ setWriteOverride(true);
67
+ setProjectionWriteActive(true);
68
+ try {
69
+ c = e[n];
70
+ } finally {
71
+ setWriteOverride(false);
72
+ setProjectionWriteActive(s);
73
+ }
74
+ // A shallow store's leaves are raw by contract (#3498): no draft proxy
75
+ // over them, so identity holds and a frozen leaf is never trapped.
76
+ return !i && typeof c === "object" && c !== null && n !== $TARGET ? wrapDraft(c, t, r) : c;
77
+ },
78
+ has(t, r) {
79
+ let i;
80
+ const o = projectionWriteActive;
81
+ setWriteOverride(true);
82
+ setProjectionWriteActive(true);
83
+ try {
84
+ i = r in e;
85
+ } finally {
86
+ setWriteOverride(false);
87
+ setProjectionWriteActive(o);
88
+ }
89
+ return i;
90
+ },
91
+ set(r, i, o) {
92
+ if (t && !t()) return true;
93
+ const n = projectionWriteActive;
94
+ setWriteOverride(true);
95
+ setProjectionWriteActive(true);
96
+ try {
97
+ write(() => {
98
+ e[i] = o;
99
+ });
100
+ } finally {
101
+ setWriteOverride(false);
102
+ setProjectionWriteActive(n);
103
+ }
104
+ return true;
105
+ },
106
+ deleteProperty(r, i) {
107
+ if (t && !t()) return true;
108
+ const o = projectionWriteActive;
109
+ setWriteOverride(true);
110
+ setProjectionWriteActive(true);
111
+ try {
112
+ write(() => {
113
+ delete e[i];
114
+ });
115
+ } finally {
116
+ setWriteOverride(false);
117
+ setProjectionWriteActive(o);
118
+ }
119
+ return true;
120
+ },
121
+ ownKeys() {
122
+ const t = projectionWriteActive;
123
+ setWriteOverride(true);
124
+ setProjectionWriteActive(true);
125
+ try {
126
+ return Reflect.ownKeys(e);
127
+ } finally {
128
+ setWriteOverride(false);
129
+ setProjectionWriteActive(t);
130
+ }
131
+ },
132
+ getOwnPropertyDescriptor(t, r) {
133
+ let i;
134
+ const o = projectionWriteActive;
135
+ setWriteOverride(true);
136
+ setProjectionWriteActive(true);
137
+ try {
138
+ i = Reflect.getOwnPropertyDescriptor(e, r);
139
+ } finally {
140
+ setWriteOverride(false);
141
+ setProjectionWriteActive(o);
142
+ }
143
+ // The dummy target doesn't hold the key, so a non-configurable report
144
+ // would violate the proxy invariant. Store descriptors are already
145
+ // normalized configurable; enforce it for raw leaves too.
146
+ if (i) i.configurable = true;
147
+ return i;
148
+ },
149
+ defineProperty(r, i, o) {
150
+ if (t && !t()) return true;
151
+ const n = projectionWriteActive;
152
+ setWriteOverride(true);
153
+ setProjectionWriteActive(true);
154
+ try {
155
+ write(() => {
156
+ Reflect.defineProperty(e, i, o);
157
+ });
158
+ } finally {
159
+ setWriteOverride(false);
160
+ setProjectionWriteActive(n);
161
+ }
162
+ return true;
163
+ }
164
+ };
165
+ // Matching-kind dummy so Array.isArray(draft) answers like the store.
166
+ return new Proxy(Array.isArray(e) ? [] : {}, o);
167
+ }
168
+
169
+ function createProjectionNextInternal(e, t, r) {
170
+ const i = {
171
+ map: new WeakMap,
172
+ node: null,
173
+ shallow: !!r?.shallow
174
+ };
175
+ const o = wrapNext(t, null, null, i);
176
+ if (i.shallow) {
177
+ // Shallow projection: the root is the only wrapped level — slot values
178
+ // serve raw, ingests sticky raw-mark (same t.s machinery as plain).
179
+ o[$TARGET].s = true;
180
+ markRawIngest(t);
181
+ }
182
+ let n;
183
+ if (r?.seedLoadingValue) n = {
184
+ loadingValue: undefined
185
+ };
186
+ if (r?.name) n = {
187
+ ...n,
188
+ name: r.name
189
+ };
190
+ const c = computed(() => {
191
+ if (!i.node) i.node = getOwner();
192
+ runProjectionComputedNext(o, e, r?.key === undefined ? "id" : r.key);
193
+ }, n);
194
+ c.C &= ~CONFIG_AUTO_DISPOSE;
195
+ i.node = c;
196
+ return {
197
+ store: o,
198
+ node: c
199
+ };
200
+ }
201
+
202
+ function createProjectionNext(e, t, r) {
203
+ return createProjectionNextInternal(e, t, r).store;
204
+ }
205
+
206
+ /** Derived writable store (legacy parity): a projection whose public setter
207
+ * masks the recompute for the tick (core R31 — the manual write wins over a
208
+ * same-flush dependency change). */ function createStoreDerivedNext(e, t, r) {
209
+ const {store: i, node: o} = createProjectionNextInternal(e, t, r);
210
+ return [ i, e => {
211
+ // Mark the projection as manually written before notifying nodes.
212
+ suppressComputedRecompute(o);
213
+ storeSetterNext(i, e);
214
+ } ];
215
+ }
216
+
217
+ // A detached copy of projection state: the root container alone for a shallow
218
+ // store — its leaves are raw references by contract and stay so (#3498) — the
219
+ // whole tree otherwise.
220
+ function cloneState(e, t) {
221
+ return t ? Array.isArray(e) ? e.slice() : {
222
+ ...e
223
+ } : JSON.parse(JSON.stringify(e));
224
+ }
225
+
226
+ function runProjectionComputedNext(e, t, r, i, o) {
227
+ const n = getOwner();
228
+ const c = e[$TARGET];
229
+ let s = false;
230
+ let u;
231
+ // Open loading window (seedLoadingValue): the observable store IS commit #0
232
+ // for the whole first flight — the derive works a detached shadow of the
233
+ // seed so draft writes cannot tear through to readers (#2988). Every commit
234
+ // point reconciles the shadow through the normal commit path.
235
+ const l = n.Se ? cloneState(c[STORE_VALUE], c.s) : null;
236
+ const a = wrapDraft(e, () => !s || n.o?.ce === u, o, c.s);
237
+ storeSetterNext(a, o => {
238
+ u = t(l ?? o);
239
+ s = true;
240
+ const commit = t => {
241
+ // Shadow run: commit a detached snapshot, never the shadow itself
242
+ // (adoption takes the value by identity — handing it the live shadow
243
+ // would fuse the draft to the observable store).
244
+ if (l && (t === undefined || t === l)) t = cloneState(l, c.s);
245
+ if (t === o || t === undefined) return;
246
+ const write = () => storeSetterNext(e, e => reconcileNextState(t, e, r, true), false);
247
+ i ? i(write, t) : write();
248
+ };
249
+ const a = handleAsync(n, u, commit);
250
+ if (!n.Se) commit(a);
251
+ }, false);
252
+ return n;
253
+ }
254
+
255
+ export { createProjectionNext, createStoreDerivedNext, runProjectionComputedNext };
@@ -0,0 +1,375 @@
1
+ import { isEqual } from "../../core/core.js";
2
+
3
+ import "../../core/dev.js";
4
+
5
+ import { projectionWriteActive } from "../../core/scheduler.js";
6
+
7
+ import "../../core/invariants.js";
8
+
9
+ import "../../core/verdict.js";
10
+
11
+ import "../../core/effect.js";
12
+
13
+ import { $TARGET, markRawIngest, isWrappable, rawValuesUsed, isRawValue, getWriteOverride } from "../store.js";
14
+
15
+ import { materializePB, adoptPB, unwrapValue, notifyFold, targetsEqual, bumpDeep, notifyKeyValue, notifyKeyDiff, notifyFoldTail, hasAccessorFlag } from "./store.js";
16
+
17
+ import { isOwned, $OWNER, storeNextLookup, optHooks, lookupTarget } from "./target.js";
18
+
19
+ /**
20
+ * Store rewrite — reconcile, the adoption channel (INTERNALS-STORE-STATE.md
21
+ * §3, decision 2026-08-16c). Reconcile never merge-writes: it adopts `next`
22
+ * as the authoritative pending backing at every proxied level (pointer swap
23
+ * folded at flush commit), notification riding the fold's descriptor diff.
24
+ *
25
+ * Structural optimizations (all kept, per 2026-08-17 morning ruling):
26
+ * - Identity skip with completed proof: `incoming === backing && !owned` —
27
+ * sound because input is immutable by convention (R2a) and ownership marks
28
+ * the only writer the convention doesn't cover (us). Fixes FINDING-1.
29
+ * - Reachability pruning: descent happens only where a child TARGET exists
30
+ * (proxies exist only where read) — never-subscribed subtrees are never
31
+ * walked (recon-snap R17), while a subscriber deep below an untracked path
32
+ * keeps its chain walkable because wrapping created the intermediate
33
+ * targets (recon-snap R16).
34
+ * - Keyed matching ported semantics: key-matched rows keep proxy identity;
35
+ * key mismatch detaches (fresh proxy on next read, recon-snap R18);
36
+ * keyless items fall back positional; null/primitive slots are legal
37
+ * members (R11). Kind changes replace wholesale (R10).
38
+ */ function reconcileNextState(e, t, n, o = false) {
39
+ if (t == null) throw new Error("");
40
+ const i = t?.[$TARGET];
41
+ if (i === undefined || i.px !== t) throw new Error("");
42
+ // Reconcile's diff walks need a REAL pending container — a prototype
43
+ // overlay (#3044) materializes to the clone path first (edge: reconcile
44
+ // inside a setter that already wrote this target).
45
+ if (i.ovl) materializePB(i);
46
+ let f = n === null ? null : typeof n === "string" ? e => e?.[n] : n;
47
+ // §7b chained backing: a projection derive returning a LIVE store proxy
48
+ // adopts the proxy itself as the backing — reads flow through the inner
49
+ // store's traps, so consumers subscribe to the inner graph and updates
50
+ // flow with no re-derive (#2941). The adoption diff still notifies THIS
51
+ // store's existing subscribers of the swap.
52
+ if (o && e !== t && e?.[$TARGET] !== undefined) {
53
+ const t = i.pb ?? i.v;
54
+ if (t === e) return;
55
+ // already chained to this store
56
+ adoptPB(i, e);
57
+ return;
58
+ }
59
+ const l = unwrapValue(e);
60
+ if (f) {
61
+ // Root identity precondition — checked before ANY mutation, so a throwing
62
+ // reconcile is atomic by construction (RUL-12 ruling). Projections
63
+ // (replace=true) relax it: a root entity change merges in place — the
64
+ // root proxy is stable for life (proj R5/R11) — and children are NOT
65
+ // key-matched across the entity change (proj R7: keyFn drops to
66
+ // positional so old-entity subtrees never merge into the new entity's).
67
+ const e = i.pb ?? i.v;
68
+ const t = f(e);
69
+ if (t !== undefined && !sameKey(f(l), t)) {
70
+ if (!o) throw new Error("");
71
+ // Entity change: wholesale swap. The root proxy is stable for life
72
+ // (proj R5) but NOTHING below survives — children are never matched
73
+ // across an entity change even when their own keys align (proj R7).
74
+ // Displaced-raw unregistration (proj R10): the outgoing raw stops
75
+ // resolving to this proxy; re-handed later it wraps fresh.
76
+ const e = i.pb ?? i.v;
77
+ if (isOwned(e)) delete e[$OWNER]; // disown: wraps fresh if re-handed
78
+ else (i.fam?.map ?? storeNextLookup).delete(e);
79
+ adoptPB(i, l);
80
+ return;
81
+ }
82
+ }
83
+ // Tentative channel (§6b, RUL-5): a user-context reconcile on an optimistic
84
+ // family parks as engine overrides — values, membership, and length ride
85
+ // armed nodes (reverting with their transaction); committed raw is never
86
+ // touched. Key-matched rows keep proxy identity by descending into the
87
+ // existing child targets instead of overriding their parent slots.
88
+ if (i.fam?.opt === true && !projectionWriteActive && !getWriteOverride()) {
89
+ optHooks.applyTentative(i, l, f);
90
+ return;
91
+ }
92
+ applyAdopt(i, l, f, o);
93
+ }
94
+
95
+ function applyAdopt(e, t, n, o = false) {
96
+ const i = e.pb ?? e.v;
97
+ // The sound identity skip (O7): same reference AND we never diverged it.
98
+ if (t === i && !isOwned(i)) return;
99
+ const f = e.fam;
100
+ // §6b (R28): the diff's previous-arrangement baseline is the LANE VIEW —
101
+ // optimistic rows must be visible to key matching so a landing carrying the
102
+ // same key recycles their proxies. Raw `prev` keeps the identity/ownership
103
+ // roles above; only matching reads the view.
104
+ const l = f?.opt === true ? optHooks.optimisticView(e, i) : i;
105
+ const r = Array.isArray(t);
106
+ // Plain stores notify inline AFTER the descent (child registrations feed
107
+ // the fold diff's identity-preservation check); projections keep deferred
108
+ // folds (downstream holds can form later in the flush).
109
+ const s = f === null;
110
+ const u = e.s === true;
111
+ // Node-notify base is the view the nodes were last told (#3296): a draft
112
+ // preceding this reconcile already moved them to its pending backing at
113
+ // setter exit (prev, materialized above), so diffing incoming against the
114
+ // committed backing would skip a key the draft changed and the reconcile
115
+ // restores — the node would commit the superseded draft value.
116
+ const a = i;
117
+ adoptPB(e, t, s);
118
+ // Shallow adoption: records are slot values — sticky raw-mark the incoming
119
+ // set (R41) and never descend; slot notification is the positional diff.
120
+ if (u) markRawIngest(t);
121
+ if (Array.isArray(l) !== r) {
122
+ if (s) notifyFold(e, a, t);
123
+ return;
124
+ }
125
+ if (r) {
126
+ const i = l;
127
+ const r = t;
128
+ // Fused array walk (eager mode): per-index notification rides the same
129
+ // loop as the descent (descend first — targetsEqual needs the child's
130
+ // re-registration, R9). Length, trailing removed indexes, and any other
131
+ // unvisited node keys land in the counted sweep below.
132
+ const c = s ? e.n : null;
133
+ let d = 0;
134
+ if (n && !u) {
135
+ // Positional-prefix fast path (legacy keyedMatch-walk parity): while
136
+ // rows key-match in place — the steady-state polling shape — descend
137
+ // directly with zero staging. The prevByKey map is built only for the
138
+ // misaligned remainder, and never at all on aligned ticks.
139
+ const l = i.length;
140
+ const s = r.length;
141
+ let u = false;
142
+ let p = 0;
143
+ for (const y = Math.min(l, s); p < y; p++) {
144
+ const l = r[p];
145
+ const s = i[p];
146
+ // Routing heuristic only (aligned vs keyed remainder) — both routes
147
+ // notify identically and descend() is the one authoritative
148
+ // validator, so bare typeof gates suffice here; full isWrappable
149
+ // per row was the walk's dominant residual cost.
150
+ if (s !== l && !(s !== null && typeof s === "object" && l !== null && typeof l === "object" && sameKey(n(s), n(l)))) break;
151
+ // misaligned: fall to the keyed remainder below
152
+ // Identity skip inline (FINDING-1 guard), then descend the pair.
153
+ if ((s !== l || l !== null && typeof l === "object" && isOwned(l)) && l !== null && typeof l === "object") descend(unwrapValue(s), l, n, f, o);
154
+ if (e.dk !== null && !u && !(l !== null && typeof l === "object" ? targetsEqual(s, l) : isEqual(s, l))) {
155
+ bumpDeep(e);
156
+ u = true;
157
+ }
158
+ if (c !== null) {
159
+ const e = c[p];
160
+ if (e !== undefined) {
161
+ d++;
162
+ notifyKeyValue(e, p, a[p], l, a, t);
163
+ }
164
+ }
165
+ }
166
+ if (e.dk !== null && !u && p < r.length) bumpDeep(e);
167
+ const y = p;
168
+ // misalignment point (== nlen on aligned ticks)
169
+ let w = null;
170
+ for (;p < r.length; p++) {
171
+ const e = r[p];
172
+ // typeof gates route; descend validates (same contract as the prefix).
173
+ if (e !== null && typeof e === "object") {
174
+ const t = n(e);
175
+ let l;
176
+ if (t !== undefined) {
177
+ if (w === null) {
178
+ // Occurrence-aware (re-audit 2, P1-5): duplicate keys queue
179
+ // their prev INDICES (rows can themselves be arrays, so index
180
+ // queues are the unambiguous encoding — same as buildRowOps)
181
+ // and each is consumed ONCE. First-wins would adopt two next
182
+ // rows into the SAME prev target while row ops retain two
183
+ // separate DOM rows (the second one stale).
184
+ w = new Map;
185
+ // From structStart, not 0 (re-audit 3, P1-2): prefix-aligned
186
+ // rows already adopted their incoming counterparts — re-offering
187
+ // them here let a duplicate key adopt a prefix row AGAIN while
188
+ // row ops (which correctly window from structStart) retained
189
+ // the later occurrence's DOM row against a never-adopted target.
190
+ for (let e = y; e < i.length; e++) {
191
+ const t = unwrapValue(i[e]);
192
+ if (t !== null && typeof t === "object") {
193
+ const o = n(t);
194
+ if (o === undefined) continue;
195
+ const i = w.get(o);
196
+ if (i === undefined) w.set(o, e); else if (Array.isArray(i)) i.push(e); else w.set(o, [ i, e ]);
197
+ }
198
+ }
199
+ }
200
+ const e = w.get(t);
201
+ if (e === undefined) l = undefined; else if (Array.isArray(e)) {
202
+ l = unwrapValue(i[e.shift()]);
203
+ if (e.length === 1) w.set(t, e[0]);
204
+ } else {
205
+ l = unwrapValue(i[e]);
206
+ w.delete(t);
207
+ }
208
+ } else {
209
+ l = unwrapValue(i[p]);
210
+ // keyless item: positional fallback
211
+ }
212
+ descend(l, e, n, f, o);
213
+ }
214
+ if (c !== null) {
215
+ const e = c[p];
216
+ if (e !== undefined) {
217
+ d++;
218
+ notifyKeyDiff(e, p, a, t, false);
219
+ }
220
+ }
221
+ }
222
+ } else {
223
+ const l = Math.min(i.length, r.length);
224
+ const s = r.length;
225
+ let p = false;
226
+ for (let y = 0; y < s; y++) {
227
+ const s = r[y];
228
+ if (!u && y < l && s !== null && typeof s === "object") descend(unwrapValue(i[y]), s, n, f, o);
229
+ if (e.dk !== null && !p && !(s !== null && typeof s === "object" ? targetsEqual(i[y], s) : isEqual(i[y], s))) {
230
+ bumpDeep(e);
231
+ p = true;
232
+ }
233
+ if (c !== null) {
234
+ const e = c[y];
235
+ if (e !== undefined) {
236
+ d++;
237
+ notifyKeyDiff(e, y, a, t, false);
238
+ }
239
+ }
240
+ }
241
+ }
242
+ if (s) {
243
+ if (c !== null && d < e.nc) {
244
+ for (const e of Reflect.ownKeys(c)) {
245
+ // visited indexes are < nextRows.length; everything else sweeps
246
+ const n = typeof e === "string" ? +e : NaN;
247
+ if (!(n >= 0 && n < r.length)) notifyKeyDiff(c[e], e, a, t, false);
248
+ }
249
+ }
250
+ notifyFoldTail(e, a, t);
251
+ }
252
+ return;
253
+ } else {
254
+ // FUSED adoption walk (eager mode): one pass fetches each key's pair,
255
+ // descends, then notifies its node inline — descend runs FIRST so the
256
+ // child's re-registration is visible to targetsEqual (identity-preserved
257
+ // slots must not notify, R9). This replaces the notifyFold re-walk that
258
+ // doubled dbmon's diff cost. for-in covers own enumerable string keys
259
+ // with no key-array allocation; symbols get a pass only when present.
260
+ const i = s ? e.n : null;
261
+ let r = 0;
262
+ let c = false;
263
+ // The per-key body is inlined on purpose (legacy applyStateFast parity:
264
+ // an extracted helper costs a call per key on the hottest object-diff
265
+ // site). Reference-identical values early-continue BEFORE any other
266
+ // work — sound only with the ownership guard (FINDING-1: an owned
267
+ // backing is setter-diverged and must still diff).
268
+ for (const s in t) {
269
+ const d = t[s];
270
+ const p = a[s];
271
+ const y = d !== null && typeof d === "object";
272
+ if (p === d && (!y || !isOwned(d)) && (i === null || i[s] === undefined || !hasAccessorFlag(i[s]))) {
273
+ if (i !== null && i[s] !== undefined) r++;
274
+ continue;
275
+ }
276
+ if (y && !u) descend(unwrapValue(l[s]), d, n, f, o);
277
+ // Deep-witness (dk): value changes must notify even with NO per-key
278
+ // node — deep() subscribes one node per record. Checked after descend
279
+ // so in-place adoptions (same logical slot) don't bump; child records
280
+ // carry their own witness. One flag + null check when unused.
281
+ if (e.dk !== null && !c && !(y ? targetsEqual(p, d) : isEqual(p, d))) {
282
+ bumpDeep(e);
283
+ c = true;
284
+ }
285
+ if (i !== null) {
286
+ const e = i[s];
287
+ if (e !== undefined) {
288
+ r++;
289
+ notifyKeyValue(e, s, p, d, a, t);
290
+ }
291
+ }
292
+ }
293
+ const d = Object.getOwnPropertySymbols(t);
294
+ for (let e = 0; e < d.length; e++) {
295
+ const s = d[e];
296
+ if (s === $OWNER) continue;
297
+ const c = t[s];
298
+ if (!u && c !== null && typeof c === "object") descend(unwrapValue(l[s]), c, n, f, o);
299
+ if (i !== null) {
300
+ const e = i[s];
301
+ if (e !== undefined) {
302
+ r++;
303
+ notifyKeyValue(e, s, a[s], c, a, t);
304
+ }
305
+ }
306
+ }
307
+ if (s) {
308
+ // Deleted-key nodes (in the map but absent from incoming) — counted
309
+ // fast-out: when every node was visited, skip the sweep entirely.
310
+ if (i !== null && r < e.nc) {
311
+ for (const e of Reflect.ownKeys(i)) {
312
+ if (!hasOwnP.call(t, e)) notifyKeyDiff(i[e], e, a, t, false);
313
+ }
314
+ }
315
+ notifyFoldTail(e, a, t);
316
+ }
317
+ return;
318
+ }
319
+ }
320
+
321
+ const hasOwnP = Object.prototype.hasOwnProperty;
322
+
323
+ /** Setter-channel row ops (the fold site calls this for array targets with
324
+ * ops consumers): structural mutation through the setter — push/splice/index
325
+ /** Key equality for EVERY key comparison in this module (re-audit 2, P1-5):
326
+ * SameValueZero, matching the adoption window's Map-based matcher — NaN keys
327
+ * are equal to themselves, so aligned NaN rows stay aligned in the prefix
328
+ * walk instead of forever misaligning. */ function sameKey(e, t) {
329
+ return e === t || e !== e && t !== t;
330
+ }
331
+
332
+ function descend(e, t, n, o, i = false) {
333
+ if (e === null || typeof e !== "object" || t === null || typeof t !== "object") return;
334
+ // Lookup FIRST: a hit implies pv was wrappable and never raw-marked (only
335
+ // wrappables acquire targets; rawValues never wrap) — one WeakMap get
336
+ // replaces isWrappable(pv) + isRawValue(pv), and a miss prunes untracked
337
+ // subtrees before any further checks.
338
+ const f = lookupTarget(e, o);
339
+ if (f === undefined) return;
340
+ // nothing proxied below this pair
341
+ // The NEW side still validates fully: a frozen/platform/markRaw'd incoming
342
+ // value is a leaf for reconcile — replaced by reference, never recursed
343
+ // into (R42); the parent's slot notification covers the change.
344
+ if (!isWrappable(t)) return;
345
+ if (rawValuesUsed && isRawValue(t)) return;
346
+ t = unwrapValue(t);
347
+ // Kind change replaces wholesale, never merges (R10): a target's carrier
348
+ // class (array vs object) is fixed at creation, so the slot detaches and a
349
+ // fresh proxy of the right kind wraps the incoming value on next read.
350
+ if (Array.isArray(e) !== Array.isArray(t)) return;
351
+ if (n) {
352
+ const o = n(e);
353
+ const i = n(t);
354
+ // Key mismatch detaches: the slot takes the new entity; the old proxy
355
+ // keeps its (old) backing and a fresh proxy wraps the new value on read.
356
+ // SameValueZero (re-audit 2, P1-5): NaN keys are self-equal — strict
357
+ // inequality detached every NaN-keyed slot on every tick while the
358
+ // Map-based row-ops matcher retained its DOM row (stale forever).
359
+ if (o !== undefined && i !== undefined && !sameKey(o, i)) return;
360
+ }
361
+ // Reachability pruning (§6d) is MODE-dependent, both pinned:
362
+ // - keyed matching descends only where subscriptions exist at/below (`d`) —
363
+ // captured-but-unobserved proxies deliberately detach and go stale
364
+ // (recon-snap R18; subscribing is what buys liveness);
365
+ // - positional (key: null) pairing preserves slot identity unconditionally
366
+ // (recon-snap R8 — the fixed-shape dashboard pattern).
367
+ // Projection merges (replace mode) preserve key-matched identity
368
+ // UNCONDITIONALLY (proj R6: the slot keeps its proxy without needing a
369
+ // subscriber below); plain keyed reconcile detaches unobserved captures
370
+ // (recon-snap R18 — staleness is the pinned pruning contract).
371
+ if (!i && n !== null && !f.d) return;
372
+ applyAdopt(f, t, n, i);
373
+ }
374
+
375
+ export { reconcileNextState, sameKey };