Skip to content

Commit 2322d64

Browse files
brenelzryansolid
authored andcommitted
fix: address review findings
A store record with more than 32 keys written during snapshot capture committed its overlay by flattening it into the backing the container's snapshot pointed at, so a scoped reader created after the flush enumerated the live key set. The fold now materializes a copy while the container's snapshot refers to that backing, and a signals test pins a 40-key record with a key added from onSettled. The hydration spec disposes the root, removes the container and restores the console spies in a finally block, so a failed variant no longer leaks into the next one. The captureWriteSnapshot comment names the store container's structural notify as its second caller.
1 parent a2dff39 commit 2322d64

5 files changed

Lines changed: 73 additions & 22 deletions

File tree

‎.changeset/store-container-write-snapshot.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
A structural store write during hydration's snapshot capture (a row removed from an array from `onSettled`, by `splice` or `reconcile`) records the container's pre-write backing as its snapshot, the way a leaf write already does. Readers inside the snapshot scope, such as a hydrated `<For>`, are held on the server's list until the scope is released instead of re-running mid-claim against a snapshot `length` and live items, which rendered a row for `undefined`, threw, and halted reactivity (#3950).
5+
A structural store write during hydration's snapshot capture (a row removed from an array from `onSettled`, by `splice` or `reconcile`) records the container's pre-write backing as its snapshot, the way a leaf write already does. Readers inside the snapshot scope, such as a hydrated `<For>`, are held on the server's list until the scope is released instead of re-running mid-claim against a snapshot `length` and live items, which rendered a row for `undefined`, threw, and halted reactivity (#3950). A record with more than 32 keys commits such a write to a copy of its backing, so the recorded snapshot keeps the pre-write key set for a scoped reader created after the flush.

‎packages/signals/src/core/core.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,9 +1930,10 @@ export function notePromotedWrite(el: Signal<any> | Computed<any>): void {
19301930
promotedWrites.push(el);
19311931
}
19321932

1933-
/** Cold half of setSignal's snapshot arm (see there): first write during
1934-
* capture to a plain signal without a live snapshot records the pre-write
1935-
* value. Only plain user signals qualify. Computeds reach setSignal from an
1933+
/** Cold half of setSignal's snapshot arm (see there), shared with a store
1934+
* container's structural notify: first write during capture to a plain
1935+
* signal without a live snapshot records the pre-write value. Only plain
1936+
* user signals qualify. Computeds reach setSignal from an
19361937
* async landing (asyncWrite), and a value arriving from async during the pass
19371938
* REVEALS — the creation-time arm skips pending computeds for the same
19381939
* reason. Firewall leaves belong to a projection whose compute is the tree's

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,10 @@ function foldTarget(t: StoreTarget, old: Record<PropertyKey, any>): void {
957957
// the adopted object (shared ownership — never cloned).
958958
t.pb = null;
959959
t.wk = null;
960-
} else if (t.ovl && (t.v !== old || !overlayRebuilds(t, pb))) {
960+
} else if (
961+
t.ovl &&
962+
(t.v !== old || (!overlayRebuilds(t, pb) && k?._x?._snapshotValue !== old))
963+
) {
961964
// Overlay flatten (#3044): the backing keeps its identity, so the
962965
// `t.v === old` gate below skips path copying (the parent slot
963966
// already points here) and the adopted-notify (setter

‎packages/signals/tests/snapshot.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,51 @@ describe("store snapshot support", () => {
797797
expect(runs).toBe(2);
798798
clearSnapshots();
799799
});
800+
801+
it("a key added to a wide owned record during capture is held for scoped readers", () => {
802+
const initial: Record<string, number> = {};
803+
for (let i = 0; i < 40; i++) initial["k" + i] = i;
804+
let owner!: any;
805+
let state!: Record<string, number>;
806+
let setState!: (fn: (s: Record<string, number>) => void) => void;
807+
createRoot(() => {
808+
owner = getOwner()!;
809+
[state, setState] = createStore(initial);
810+
});
811+
setState(s => {
812+
s.k0 = 100;
813+
});
814+
flush();
815+
816+
let keys!: () => string[];
817+
let late!: () => string[];
818+
runWithOwner(owner, () => {
819+
setSnapshotCapture(true);
820+
markSnapshotScope(owner);
821+
keys = createMemo(() => Object.keys(state));
822+
expect(keys()).toHaveLength(40);
823+
onSettled(() => {
824+
setState(s => {
825+
s.extra = 1;
826+
});
827+
});
828+
});
829+
flush();
830+
runWithOwner(owner, () => {
831+
late = createMemo(() => Object.keys(state));
832+
});
833+
834+
expect(keys()).toHaveLength(40);
835+
expect(late()).toHaveLength(40);
836+
expect(state.extra).toBe(1);
837+
838+
releaseSnapshotScope(owner);
839+
flush();
840+
841+
expect(keys()).toContain("extra");
842+
expect(late()).toContain("extra");
843+
clearSnapshots();
844+
});
800845
});
801846

802847
describe("pending projection skips snapshot capture", () => {

‎packages/web/test/hydration/onsettled-store-write-3950.spec.tsx‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,26 @@ describe("store write from onSettled during hydration of a <For> (#3950)", () =>
4949
document.body.appendChild(container);
5050
const rows = () => [...container.querySelectorAll("li")].map(li => li.textContent);
5151

52-
applyChunk(container, shell, true);
53-
for (const c of chunks) applyChunk(container, c, false);
54-
expect(rows()).toEqual(["remove", "keep"]);
55-
const kept = container.querySelectorAll("li")[1];
52+
let dispose: (() => void) | undefined;
53+
try {
54+
applyChunk(container, shell, true);
55+
for (const c of chunks) applyChunk(container, c, false);
56+
expect(rows()).toEqual(["remove", "keep"]);
57+
const kept = container.querySelectorAll("li")[1];
5658

57-
const dispose = hydrate(() => <variant.App />, container);
58-
await vi.waitFor(() => {
59-
flush();
60-
expect(rows()).toEqual(["keep"]);
61-
});
62-
expect(container.querySelector("li")).toBe(kept);
63-
64-
const diagnostics = [...warn.mock.calls, ...error.mock.calls].map(c => String(c[0]));
65-
dispose();
66-
container.remove();
67-
warn.mockRestore();
68-
error.mockRestore();
69-
expect(diagnostics).toEqual([]);
59+
dispose = hydrate(() => <variant.App />, container);
60+
await vi.waitFor(() => {
61+
flush();
62+
expect(rows()).toEqual(["keep"]);
63+
});
64+
expect(container.querySelector("li")).toBe(kept);
65+
expect([...warn.mock.calls, ...error.mock.calls].map(c => String(c[0]))).toEqual([]);
66+
} finally {
67+
dispose?.();
68+
container.remove();
69+
warn.mockRestore();
70+
error.mockRestore();
71+
}
7072
});
7173
}
7274
});

0 commit comments

Comments
 (0)