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

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 (70) hide show
  1. package/README.md +1 -1
  2. package/dist/dev.js +4287 -2803
  3. package/dist/node.cjs +4563 -3756
  4. package/dist/prod/affects.js +16 -16
  5. package/dist/prod/boundaries.js +90 -90
  6. package/dist/prod/core/action.js +3 -3
  7. package/dist/prod/core/async.js +234 -178
  8. package/dist/prod/core/constants.js +39 -1
  9. package/dist/prod/core/core.js +362 -243
  10. package/dist/prod/core/effect.js +56 -56
  11. package/dist/prod/core/external.js +4 -4
  12. package/dist/prod/core/graph.js +65 -54
  13. package/dist/prod/core/heap.js +47 -39
  14. package/dist/prod/core/invariants.js +3 -2
  15. package/dist/prod/core/lanes.js +41 -34
  16. package/dist/prod/core/optimistic.js +78 -58
  17. package/dist/prod/core/owner.js +101 -100
  18. package/dist/prod/core/scheduler.js +249 -192
  19. package/dist/prod/core/verdict.js +185 -78
  20. package/dist/prod/index.js +7 -7
  21. package/dist/prod/map.js +108 -106
  22. package/dist/prod/signals.js +20 -1
  23. package/dist/prod/store/index.js +36 -0
  24. package/dist/prod/store/next/optimistic.js +385 -0
  25. package/dist/prod/store/next/projection.js +172 -0
  26. package/dist/prod/store/next/reconcile.js +331 -0
  27. package/dist/prod/store/next/store.js +1499 -0
  28. package/dist/prod/store/next/target.js +20 -0
  29. package/dist/prod/store/store.js +126 -882
  30. package/dist/prod/store/utils.js +59 -186
  31. package/dist/types/core/attribution-hooks.d.ts +52 -0
  32. package/dist/types/core/attribution.d.ts +186 -0
  33. package/dist/types/core/constants.d.ts +26 -0
  34. package/dist/types/core/core.d.ts +16 -1
  35. package/dist/types/core/dev.d.ts +20 -3
  36. package/dist/types/core/graph.d.ts +1 -0
  37. package/dist/types/core/lanes.d.ts +4 -16
  38. package/dist/types/core/scheduler.d.ts +12 -2
  39. package/dist/types/core/types.d.ts +85 -41
  40. package/dist/types/signals.d.ts +19 -0
  41. package/dist/types/store/index.d.ts +15 -5
  42. package/dist/types/store/next/optimistic.d.ts +20 -0
  43. package/dist/types/store/next/projection.d.ts +8 -0
  44. package/dist/types/store/next/reconcile.d.ts +3 -0
  45. package/dist/types/store/next/store.d.ts +77 -0
  46. package/dist/types/store/next/target.d.ts +117 -0
  47. package/dist/types/store/store.d.ts +26 -101
  48. package/dist/types/store/utils.d.ts +0 -40
  49. package/dist/types-cjs/core/attribution-hooks.d.cts +52 -0
  50. package/dist/types-cjs/core/attribution.d.cts +186 -0
  51. package/dist/types-cjs/core/constants.d.cts +26 -0
  52. package/dist/types-cjs/core/core.d.cts +16 -1
  53. package/dist/types-cjs/core/dev.d.cts +20 -3
  54. package/dist/types-cjs/core/graph.d.cts +1 -0
  55. package/dist/types-cjs/core/lanes.d.cts +4 -16
  56. package/dist/types-cjs/core/scheduler.d.cts +12 -2
  57. package/dist/types-cjs/core/types.d.cts +85 -41
  58. package/dist/types-cjs/signals.d.cts +19 -0
  59. package/dist/types-cjs/store/index.d.cts +15 -5
  60. package/dist/types-cjs/store/next/optimistic.d.cts +20 -0
  61. package/dist/types-cjs/store/next/projection.d.cts +8 -0
  62. package/dist/types-cjs/store/next/reconcile.d.cts +3 -0
  63. package/dist/types-cjs/store/next/store.d.cts +77 -0
  64. package/dist/types-cjs/store/next/target.d.cts +117 -0
  65. package/dist/types-cjs/store/store.d.cts +26 -101
  66. package/dist/types-cjs/store/utils.d.cts +0 -40
  67. package/package.json +2 -1
  68. package/dist/prod/store/optimistic.js +0 -217
  69. package/dist/prod/store/projection.js +0 -231
  70. package/dist/prod/store/reconcile.js +0 -707
@@ -0,0 +1,331 @@
1
+ import { isEqual } from "../../core/core.js";
2
+
3
+ import { projectionWriteActive } from "../../core/scheduler.js";
4
+
5
+ import "../../core/invariants.js";
6
+
7
+ import "../../core/verdict.js";
8
+
9
+ import "../../core/effect.js";
10
+
11
+ import { $TARGET, markRawIngest, isWrappable, rawValuesUsed, isRawValue, getWriteOverride } from "../store.js";
12
+
13
+ import { materializePB, adoptPB, unwrapValue, notifyFold, targetsEqual, bumpDeep, notifyKeyValue, notifyKeyDiff, notifyFoldTail, hasAccessorFlag } from "./store.js";
14
+
15
+ import { storeNextLookup, optHooks, ownedRaw } from "./target.js";
16
+
17
+ /**
18
+ * Store rewrite — reconcile, the adoption channel (INTERNALS-STORE-STATE.md
19
+ * §3, decision 2026-08-16c). Reconcile never merge-writes: it adopts `next`
20
+ * as the authoritative pending backing at every proxied level (pointer swap
21
+ * folded at flush commit), notification riding the fold's descriptor diff.
22
+ *
23
+ * Structural optimizations (all kept, per 2026-08-17 morning ruling):
24
+ * - Identity skip with completed proof: `incoming === backing && !owned` —
25
+ * sound because input is immutable by convention (R2a) and ownership marks
26
+ * the only writer the convention doesn't cover (us). Fixes FINDING-1.
27
+ * - Reachability pruning: descent happens only where a child TARGET exists
28
+ * (proxies exist only where read) — never-subscribed subtrees are never
29
+ * walked (recon-snap R17), while a subscriber deep below an untracked path
30
+ * keeps its chain walkable because wrapping created the intermediate
31
+ * targets (recon-snap R16).
32
+ * - Keyed matching ported semantics: key-matched rows keep proxy identity;
33
+ * key mismatch detaches (fresh proxy on next read, recon-snap R18);
34
+ * keyless items fall back positional; null/primitive slots are legal
35
+ * members (R11). Kind changes replace wholesale (R10).
36
+ */ function reconcileNextState(e, t, n, o = false) {
37
+ if (t == null) throw new Error("");
38
+ const l = t?.[$TARGET];
39
+ if (l === undefined || l.px !== t) throw new Error("");
40
+ // Reconcile's diff walks need a REAL pending container — a prototype
41
+ // overlay (#3044) materializes to the clone path first (edge: reconcile
42
+ // inside a setter that already wrote this target).
43
+ if (l.ovl) materializePB(l);
44
+ let f = n === null ? null : typeof n === "string" ? e => e?.[n] : n;
45
+ // §7b chained backing: a projection derive returning a LIVE store proxy
46
+ // adopts the proxy itself as the backing — reads flow through the inner
47
+ // store's traps, so consumers subscribe to the inner graph and updates
48
+ // flow with no re-derive (#2941). The adoption diff still notifies THIS
49
+ // store's existing subscribers of the swap.
50
+ if (o && e !== t && e?.[$TARGET] !== undefined) {
51
+ const t = l.pb ?? l.v;
52
+ if (t === e) return;
53
+ // already chained to this store
54
+ adoptPB(l, e);
55
+ return;
56
+ }
57
+ const i = unwrapValue(e);
58
+ if (f) {
59
+ // Root identity precondition — checked before ANY mutation, so a throwing
60
+ // reconcile is atomic by construction (RUL-12 ruling). Projections
61
+ // (replace=true) relax it: a root entity change merges in place — the
62
+ // root proxy is stable for life (proj R5/R11) — and children are NOT
63
+ // key-matched across the entity change (proj R7: keyFn drops to
64
+ // positional so old-entity subtrees never merge into the new entity's).
65
+ const e = l.pb ?? l.v;
66
+ const t = f(e);
67
+ if (t !== undefined && f(i) !== t) {
68
+ if (!o) throw new Error("");
69
+ // Entity change: wholesale swap. The root proxy is stable for life
70
+ // (proj R5) but NOTHING below survives — children are never matched
71
+ // across an entity change even when their own keys align (proj R7).
72
+ // Displaced-raw unregistration (proj R10): the outgoing raw stops
73
+ // resolving to this proxy; re-handed later it wraps fresh.
74
+ (l.fam?.map ?? storeNextLookup).delete(l.pb ?? l.v);
75
+ adoptPB(l, i);
76
+ return;
77
+ }
78
+ }
79
+ // Tentative channel (§6b, RUL-5): a user-context reconcile on an optimistic
80
+ // family parks as engine overrides — values, membership, and length ride
81
+ // armed nodes (reverting with their transaction); committed raw is never
82
+ // touched. Key-matched rows keep proxy identity by descending into the
83
+ // existing child targets instead of overriding their parent slots.
84
+ if (l.fam?.opt === true && !projectionWriteActive && !getWriteOverride()) {
85
+ optHooks.applyTentative(l, i, f);
86
+ return;
87
+ }
88
+ applyAdopt(l, i, f, o);
89
+ }
90
+
91
+ function applyAdopt(e, t, n, o = false) {
92
+ const l = e.pb ?? e.v;
93
+ // The sound identity skip (O7): same reference AND we never diverged it.
94
+ if (t === l && !ownedRaw.has(l)) return;
95
+ const f = e.fam;
96
+ // §6b (R28): the diff's previous-arrangement baseline is the LANE VIEW —
97
+ // optimistic rows must be visible to key matching so a landing carrying the
98
+ // same key recycles their proxies. Raw `prev` keeps the identity/ownership
99
+ // roles above; only matching reads the view.
100
+ const i = f?.opt === true ? optHooks.optimisticView(e, l) : l;
101
+ const r = Array.isArray(t);
102
+ // Plain stores notify inline AFTER the descent (child registrations feed
103
+ // the fold diff's identity-preservation check); projections keep deferred
104
+ // folds (downstream holds can form later in the flush).
105
+ const u = f === null;
106
+ const s = e.s === true;
107
+ const a = e.v;
108
+ adoptPB(e, t, u);
109
+ // Shallow adoption: records are slot values — sticky raw-mark the incoming
110
+ // set (R41) and never descend; slot notification is the positional diff.
111
+ if (s) markRawIngest(t);
112
+ if (Array.isArray(i) !== r) {
113
+ if (u) notifyFold(e, a, t);
114
+ return;
115
+ }
116
+ if (r) {
117
+ const l = i;
118
+ const r = t;
119
+ // Fused array walk (eager mode): per-index notification rides the same
120
+ // loop as the descent (descend first — targetsEqual needs the child's
121
+ // re-registration, R9). Length, trailing removed indexes, and any other
122
+ // unvisited node keys land in the counted sweep below.
123
+ const c = u ? e.n : null;
124
+ let p = 0;
125
+ if (n && !s) {
126
+ // Positional-prefix fast path (legacy keyedMatch-walk parity): while
127
+ // rows key-match in place — the steady-state polling shape — descend
128
+ // directly with zero staging. The prevByKey map is built only for the
129
+ // misaligned remainder, and never at all on aligned ticks.
130
+ const i = l.length;
131
+ const u = r.length;
132
+ let s = false;
133
+ let d = 0;
134
+ for (const y = Math.min(i, u); d < y; d++) {
135
+ const i = r[d];
136
+ const u = l[d];
137
+ // Routing heuristic only (aligned vs keyed remainder) — both routes
138
+ // notify identically and descend() is the one authoritative
139
+ // validator, so bare typeof gates suffice here; full isWrappable
140
+ // per row was the walk's dominant residual cost.
141
+ if (u !== i && !(u !== null && typeof u === "object" && i !== null && typeof i === "object" && n(u) === n(i))) break;
142
+ // misaligned: fall to the keyed remainder below
143
+ // Identity skip inline (FINDING-1 guard), then descend the pair.
144
+ if ((u !== i || i !== null && typeof i === "object" && ownedRaw.has(i)) && i !== null && typeof i === "object") descend(unwrapValue(u), i, n, f, o);
145
+ if (e.dk !== null && !s && !(i !== null && typeof i === "object" ? targetsEqual(u, i) : isEqual(u, i))) {
146
+ bumpDeep(e);
147
+ s = true;
148
+ }
149
+ if (c !== null) {
150
+ const e = c[d];
151
+ if (e !== undefined) {
152
+ p++;
153
+ notifyKeyValue(e, d, a[d], i, a, t);
154
+ }
155
+ }
156
+ }
157
+ if (e.dk !== null && !s && d < r.length) bumpDeep(e);
158
+ let y = null;
159
+ for (;d < r.length; d++) {
160
+ const e = r[d];
161
+ // typeof gates route; descend validates (same contract as the prefix).
162
+ if (e !== null && typeof e === "object") {
163
+ const t = n(e);
164
+ let i;
165
+ if (t !== undefined) {
166
+ if (y === null) {
167
+ y = new Map;
168
+ for (let e = 0; e < l.length; e++) {
169
+ const t = unwrapValue(l[e]);
170
+ if (t !== null && typeof t === "object") {
171
+ const e = n(t);
172
+ if (e !== undefined && !y.has(e)) y.set(e, t);
173
+ }
174
+ }
175
+ }
176
+ i = y.get(t);
177
+ } else {
178
+ i = unwrapValue(l[d]);
179
+ // keyless item: positional fallback
180
+ }
181
+ descend(i, e, n, f, o);
182
+ }
183
+ if (c !== null) {
184
+ const e = c[d];
185
+ if (e !== undefined) {
186
+ p++;
187
+ notifyKeyDiff(e, d, a, t, false);
188
+ }
189
+ }
190
+ }
191
+ } else {
192
+ const i = Math.min(l.length, r.length);
193
+ const u = r.length;
194
+ let d = false;
195
+ for (let y = 0; y < u; y++) {
196
+ const u = r[y];
197
+ if (!s && y < i && u !== null && typeof u === "object") descend(unwrapValue(l[y]), u, n, f, o);
198
+ if (e.dk !== null && !d && !(u !== null && typeof u === "object" ? targetsEqual(l[y], u) : isEqual(l[y], u))) {
199
+ bumpDeep(e);
200
+ d = true;
201
+ }
202
+ if (c !== null) {
203
+ const e = c[y];
204
+ if (e !== undefined) {
205
+ p++;
206
+ notifyKeyDiff(e, y, a, t, false);
207
+ }
208
+ }
209
+ }
210
+ }
211
+ if (u) {
212
+ if (c !== null && p < e.nc) {
213
+ for (const e of Reflect.ownKeys(c)) {
214
+ // visited indexes are < nextRows.length; everything else sweeps
215
+ const n = typeof e === "string" ? +e : NaN;
216
+ if (!(n >= 0 && n < r.length)) notifyKeyDiff(c[e], e, a, t, false);
217
+ }
218
+ }
219
+ notifyFoldTail(e, a, t);
220
+ }
221
+ return;
222
+ } else {
223
+ // FUSED adoption walk (eager mode): one pass fetches each key's pair,
224
+ // descends, then notifies its node inline — descend runs FIRST so the
225
+ // child's re-registration is visible to targetsEqual (identity-preserved
226
+ // slots must not notify, R9). This replaces the notifyFold re-walk that
227
+ // doubled dbmon's diff cost. for-in covers own enumerable string keys
228
+ // with no key-array allocation; symbols get a pass only when present.
229
+ const l = u ? e.n : null;
230
+ let r = 0;
231
+ let c = false;
232
+ // The per-key body is inlined on purpose (legacy applyStateFast parity:
233
+ // an extracted helper costs a call per key on the hottest object-diff
234
+ // site). Reference-identical values early-continue BEFORE any other
235
+ // work — sound only with the ownership guard (FINDING-1: an owned
236
+ // backing is setter-diverged and must still diff).
237
+ for (const u in t) {
238
+ const p = t[u];
239
+ const d = a[u];
240
+ const y = p !== null && typeof p === "object";
241
+ if (d === p && (!y || !ownedRaw.has(p)) && (l === null || l[u] === undefined || !hasAccessorFlag(l[u]))) {
242
+ if (l !== null && l[u] !== undefined) r++;
243
+ continue;
244
+ }
245
+ if (y && !s) descend(unwrapValue(i[u]), p, n, f, o);
246
+ // Deep-witness (dk): value changes must notify even with NO per-key
247
+ // node — deep() subscribes one node per record. Checked after descend
248
+ // so in-place adoptions (same logical slot) don't bump; child records
249
+ // carry their own witness. One flag + null check when unused.
250
+ if (e.dk !== null && !c && !(y ? targetsEqual(d, p) : isEqual(d, p))) {
251
+ bumpDeep(e);
252
+ c = true;
253
+ }
254
+ if (l !== null) {
255
+ const e = l[u];
256
+ if (e !== undefined) {
257
+ r++;
258
+ notifyKeyValue(e, u, d, p, a, t);
259
+ }
260
+ }
261
+ }
262
+ const p = Object.getOwnPropertySymbols(t);
263
+ for (let e = 0; e < p.length; e++) {
264
+ const u = p[e];
265
+ const c = t[u];
266
+ if (!s && c !== null && typeof c === "object") descend(unwrapValue(i[u]), c, n, f, o);
267
+ if (l !== null) {
268
+ const e = l[u];
269
+ if (e !== undefined) {
270
+ r++;
271
+ notifyKeyValue(e, u, a[u], c, a, t);
272
+ }
273
+ }
274
+ }
275
+ if (u) {
276
+ // Deleted-key nodes (in the map but absent from incoming) — counted
277
+ // fast-out: when every node was visited, skip the sweep entirely.
278
+ if (l !== null && r < e.nc) {
279
+ for (const e of Reflect.ownKeys(l)) {
280
+ if (!hasOwnP.call(t, e)) notifyKeyDiff(l[e], e, a, t, false);
281
+ }
282
+ }
283
+ notifyFoldTail(e, a, t);
284
+ }
285
+ return;
286
+ }
287
+ }
288
+
289
+ const hasOwnP = Object.prototype.hasOwnProperty;
290
+
291
+ function descend(e, t, n, o, l = false) {
292
+ if (e === null || typeof e !== "object" || t === null || typeof t !== "object") return;
293
+ // Lookup FIRST: a hit implies pv was wrappable and never raw-marked (only
294
+ // wrappables acquire targets; rawValues never wrap) — one WeakMap get
295
+ // replaces isWrappable(pv) + isRawValue(pv), and a miss prunes untracked
296
+ // subtrees before any further checks.
297
+ const f = (o?.map ?? storeNextLookup).get(e);
298
+ if (f === undefined) return;
299
+ // nothing proxied below this pair
300
+ // The NEW side still validates fully: a frozen/platform/markRaw'd incoming
301
+ // value is a leaf for reconcile — replaced by reference, never recursed
302
+ // into (R42); the parent's slot notification covers the change.
303
+ if (!isWrappable(t)) return;
304
+ if (rawValuesUsed && isRawValue(t)) return;
305
+ t = unwrapValue(t);
306
+ // Kind change replaces wholesale, never merges (R10): a target's carrier
307
+ // class (array vs object) is fixed at creation, so the slot detaches and a
308
+ // fresh proxy of the right kind wraps the incoming value on next read.
309
+ if (Array.isArray(e) !== Array.isArray(t)) return;
310
+ if (n) {
311
+ const o = n(e);
312
+ const l = n(t);
313
+ // Key mismatch detaches: the slot takes the new entity; the old proxy
314
+ // keeps its (old) backing and a fresh proxy wraps the new value on read.
315
+ if (o !== undefined && l !== undefined && o !== l) return;
316
+ }
317
+ // Reachability pruning (§6d) is MODE-dependent, both pinned:
318
+ // - keyed matching descends only where subscriptions exist at/below (`d`) —
319
+ // captured-but-unobserved proxies deliberately detach and go stale
320
+ // (recon-snap R18; subscribing is what buys liveness);
321
+ // - positional (key: null) pairing preserves slot identity unconditionally
322
+ // (recon-snap R8 — the fixed-shape dashboard pattern).
323
+ // Projection merges (replace mode) preserve key-matched identity
324
+ // UNCONDITIONALLY (proj R6: the slot keeps its proxy without needing a
325
+ // subscriber below); plain keyed reconcile detaches unobserved captures
326
+ // (recon-snap R18 — staleness is the pinned pruning contract).
327
+ if (!l && n !== null && !f.d) return;
328
+ applyAdopt(f, t, n, l);
329
+ }
330
+
331
+ export { reconcileNextState };