Skip to content

Commit ae0ec3f

Browse files
fix(store): deep() hears base and optimistic writes through chained views; stable row identity under overlays (#3323)
deep() over a derived view (createOptimisticStore(base), projections whose backing is another store) never re-ran on base writes: the view's targets chain to the inner store's proxies, base writes bump the INNER record's witnesses, and the walk subscribed only the outer ones — while resolving children to fresh non-chained wrappers of the base raw. The walk now reads through the whole chain and resolves children to the targets the get trap serves. Investigating that surfaced two more holes in the same composition: - deep() over any optimistic store was deaf to every write on a row added under a held action: the row lives in presence/value overrides, and the walk enumerated the raw backing. The ownKeys/getOwnPropertyDescriptor trap bodies are now shared helpers (visibleKeys/visibleDescriptor) the walk uses, so parity with readers is structural. - A derived view's untouched rows came back as fresh proxies for the life of an optimistic action and snapped back at settle. notifyOptimisticWrites compared the inner store's child PROXIES to the draft's raws and marked every row changed; it now compares unwrapped values. Chained targets serving from a pending backing resolve inner-owned raws to the inner proxy before wrapping (resolveChainedRaw), and snapshotWalk starts from the family's wrapper when entering a level from a raw so outer overrides below the root compose. The dist-artifacts top-level-await scanner now skips comments; an apostrophe in a module-scope docblock opened a phantom string and derailed its depth. Closes #3323 Co-authored-by: Cursor <[email protected]>
1 parent 1c9e9e7 commit ae0ec3f

7 files changed

Lines changed: 571 additions & 94 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Fix `deep()` over optimistic and derived stores missing writes it should hear, and derived views churning row identities under an optimistic overlay (#3323).
6+
7+
- `deep(view)` / `deep(view[i])` over a derived view — `createOptimisticStore(base)` or a projection whose backing is another store — never re-ran when the base store was written, while per-key reads on the same view did. A view's targets chain to the inner store's proxies and base writes bump the inner record's witness nodes; the walk never subscribed them, and it resolved children to fresh non-chained wrappers of the base raw instead of the chained row targets the view serves. The walk now reads through the whole chain and resolves children to the targets the get trap would serve.
8+
- `deep()` over any optimistic store was deaf to every write on a row added under a held action: the row lives in presence/value overrides, not the committed backing, and the walk enumerated the raw backing. The `ownKeys` / `getOwnPropertyDescriptor` trap bodies are now shared helpers the walk uses, so the walk sees exactly what readers see.
9+
- A derived view's untouched rows came back as fresh proxies for the life of an optimistic action (`view.map(r => r)` was O(n) new identities per action) and snapped back at settle. The optimistic diff compared the inner store's child proxies to the draft's raws and marked every row changed; it now compares unwrapped values. Chained targets serving from a pending backing resolve inner-owned raws to the inner proxy before wrapping, and `snapshot()` composes outer overrides below the root.
10+
11+
Only `deep()`, the chained-view read path, and the optimistic diff changed; plain-store reads and writes are unaffected.

‎packages/signals/docs/INTERNALS-STORE-STATE.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,35 @@ raw object. Mechanics:
344344
- **Structural chaining** (#2864, core R21): outer `ownKeys`/`$TRACK` reads
345345
likewise read through to the inner key-set node — chaining is the same
346346
read-through rule, not a special case.
347+
- **`deep()` through a chain** (#3323): the walk subscribes every inner
348+
record's key-set AND deep-witness node along the chain (from `t.v`, not
349+
`readSource` — a pending backing is a raw clone and would hide the chain),
350+
and resolves children via `resolveChainedRaw` to the chained wrapper the
351+
get trap serves, never a fresh raw-keyed one. Base writes bump the inner
352+
witnesses, so a `deep(view)` subscriber hears them exactly as per-key
353+
readers do (the reader-families-agree rule).
354+
- **One visibility rule** (#3323): `visibleKeys` / `visibleDescriptor` are
355+
the bodies of the `ownKeys` / `getOwnPropertyDescriptor` traps AND what
356+
the `deep()` walk enumerates. The walk used to re-derive the trap rules
357+
over raw backings and missed each new one (the #3044 overlay merge, the
358+
chain, optimistic presence/value overrides — a row added under a held
359+
action lives in `h`/`n`, so its record was never walked and `deep()` was
360+
deaf to every write on it until settle). Parity is now structural.
361+
- **Chained pending backings keep identity** (#3323): `ensurePB` on a
362+
chained target clones the inner *proxy*, whose descriptors yield the
363+
inner store's raws. Two rules keep the overlay serving the same chained
364+
targets as the settled state: (1) `serveDataKey` (and the walk) pass a
365+
raw the inner family owns — already served, or held by the inner backing
366+
at that key — through `resolveChainedRaw` to the inner proxy before
367+
wrapping; a raw the inner does not own is a draft's replacement object and
368+
stays view-owned/non-chained. (2) `notifyOptimisticWrites` diffs
369+
`unwrapValue`d values on both sides — `old = t.v` is the inner proxy whose
370+
reads return inner child *proxies*, and comparing those to the clone's
371+
raws marked every untouched row changed, so the overlay served each from
372+
an override as a fresh non-chained target (row identities churned for the
373+
life of the action and snapped back at settle). `snapshotWalk`, entering a
374+
level from a raw below a chained family, starts from that family's wrapper
375+
(keyed by the inner proxy) so outer overrides below the root compose.
347376
- **Lane masking = shadow + dynamic dependencies** (core R36 zero-churn): a
348377
lane value on an outer property node shadows read-through. Subscribers
349378
re-run once on the hold write, rebuild dependencies against the lane value

‎packages/signals/src/store/next/optimistic.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,19 @@ export function notifyOptimisticWrites(t: StoreNextTarget, pb: Record<PropertyKe
548548
if (ft !== null) globalQueue.initTransition(ft);
549549
}
550550
const old = t.v;
551+
// Compare RAWS on both sides (`nv` below is unwrapped already). A chained
552+
// target's `old` is the inner store's proxy, whose reads hand back inner
553+
// child PROXIES; the draft's clone holds the inner raws. Comparing the two
554+
// as-is marked every untouched row changed, and the overlay then served
555+
// each from an override as a fresh non-chained target — row identities
556+
// churned for the life of the action and snapped back at settle (#3323).
551557
const visible = (key: PropertyKey, fallback: any): any => {
552558
const node = t.n?.[key as any];
553-
return node !== undefined && hasActiveOverride(node)
554-
? unwrapOverride(node._x?._overrideValue)
555-
: fallback;
559+
return unwrapValue(
560+
node !== undefined && hasActiveOverride(node)
561+
? unwrapOverride(node._x?._overrideValue)
562+
: fallback
563+
);
556564
};
557565
const visiblePresent = (key: PropertyKey): boolean => {
558566
const node = t.h?.[key as any];

0 commit comments

Comments
 (0)