Skip to content

Commit f637481

Browse files
devagrawal09claude
andcommitted
fix(signals): deep()/snapshot() see an action's store writes in the pass it lands in (#4552)
A per-key read of a key under a held container asks core for the container's frame (`readSource` -> `read()` of the container node without a link), and so does `deep()`'s enumeration walk (`enumerationSource`): the hold's own pass (the landing flush, or a memo or effect that joined the hold) is served the staging, and a frame reader outside the hold's flush the committed frame. `snapshotWalk` tested the container's `CONFIG_HELD` flag instead, which stands until the seam, so in the flush an action lands in it composed the committed backing while the keys it walked and the per-key reads beside it already served the landing. Nothing re-runs after the commit, so an effect or memo over `deep(store)` kept the pre-action store. The composition now makes the same call core makes for a per-key read. Regressed in 1b9ceb6 (#3774), where the rebuilt store replaced `pendingBackingVisible` (#3147, aa03f76) with the flag test. Pins the issue's four cases next to #3147's. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 50dec1b commit f637481

3 files changed

Lines changed: 199 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
`deep()` and `snapshot()` see an action's store writes in the pass the action lands in, as the per-key reads beside them do (#4552). A per-key read of a key under a held container asks core for the container's frame — the hold's own pass (the landing flush, a memo or effect that joined the hold) is served the staging, a frame reader outside the hold's flush the committed one (#3147) — and so does `deep()`'s enumeration walk; `snapshot()`'s composition tested the container's held flag instead, which stands until the seam, so an effect or memo over `deep(store)` re-run at the landing composed the pre-action store beside per-key reads that already showed the writes, and kept it: nothing re-runs after the commit. The composition now makes the same call core makes for a per-key read.

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,15 +2737,29 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreFamily | nul
27372737
if (outer !== undefined) t = outer;
27382738
}
27392739
if (t._fam !== null) fam = t._fam;
2740-
// R27: a snapshot sees the batch's pending backing — a held one (another
2741-
// transaction's future) only from its own draft.
2740+
// R27: a snapshot sees the batch's pending backing. A HELD one is a
2741+
// transaction's future, and whether this reader sees it is core's call
2742+
// on the container node — the same `read()` without a link a per-key
2743+
// read of a held key makes (`readSource`) and `deep()`'s own walk makes
2744+
// (`enumerationSource`): the hold's own pass (its landing flush, a memo
2745+
// or effect that joined it) composes the staging, a frame reader outside
2746+
// the hold's flush the committed frame and is re-derived at the landing
2747+
// (#3147). A flag test instead read the committed backing in the pass
2748+
// the hold lands in — the container is held until the seam — while the
2749+
// per-key reads beside it already served the landing (#4552).
27422750
let backing = t.v;
2743-
if (
2744-
t.pb !== null &&
2745-
(inDraft(t) || writeOverride || t.k === null || !(t.k._config & CONFIG_HELD))
2746-
) {
2747-
if (t.ovl) materializePB(t);
2748-
backing = t.pb!;
2751+
if (t.pb !== null) {
2752+
const k = t.k;
2753+
let usePB = true;
2754+
if (!inDraft(t) && !writeOverride && k !== null && k._config & CONFIG_HELD) {
2755+
const frame = untrack(() => readNode(k));
2756+
usePB = frame === k._pendingValue;
2757+
if (!usePB) backing = asBacking(frame);
2758+
}
2759+
if (usePB) {
2760+
if (t.ovl) materializePB(t);
2761+
backing = t.pb;
2762+
}
27492763
}
27502764
if (optHooks && optRead(t)) backing = optHooks.view(t, backing);
27512765
if (backing === src) break;
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/**
2+
* #4552 — deep()/snapshot() see an action's writes in the pass the action
3+
* lands in, as the per-key reads beside them do.
4+
*
5+
* A per-key read of a key a held container changed asks core for the
6+
* container's frame (`readSource` → `read()` of the container node without
7+
* a link), and so does `deep()`'s enumeration walk (`enumerationSource`):
8+
* the hold's own pass — the landing flush, a memo or effect that joined the
9+
* hold — is served the staging. `snapshot()`'s composition tested the
10+
* container's HELD flag instead, which stands until the seam, so in the
11+
* landing flush it composed the committed backing while the keys it walked
12+
* and the per-key reads beside it already served the landing. Nothing
13+
* re-runs after the commit, so the stale value stuck.
14+
*
15+
* Ruling pinned here (#3147's, applied): the held-landing visibility
16+
* decision is ONE decision, core's on the container node, shared by per-key
17+
* reads, `deep()`'s walk and `snapshot()`'s composition.
18+
*/
19+
import { describe, expect, it } from "vitest";
20+
import {
21+
action,
22+
createEffect,
23+
createMemo,
24+
createRoot,
25+
createStore,
26+
deep,
27+
flush,
28+
snapshot
29+
} from "../../src/index.js";
30+
31+
const settle = async (n = 4) => {
32+
for (let i = 0; i < n; i++) {
33+
await new Promise(r => setTimeout(r, 0));
34+
flush();
35+
}
36+
};
37+
38+
type State = { a: number; b: number };
39+
40+
/** An action writing `a` before its yield and `b` after it; the gate between
41+
* them is released by the caller. */
42+
function gatedAction(setState: (fn: (s: State) => void) => void, afterYield: boolean) {
43+
let release!: () => void;
44+
const gate = new Promise<void>(r => (release = r));
45+
const done = action(function* () {
46+
setState(d => {
47+
d.a = 1;
48+
});
49+
yield gate;
50+
if (afterYield)
51+
setState(d => {
52+
d.b = 1;
53+
});
54+
})();
55+
return { release, done };
56+
}
57+
58+
describe("#4552: deep()/snapshot() see an action's writes when it lands", () => {
59+
it("an effect over deep(), snapshot() and per-key reads sees one frame at the landing", async () => {
60+
const [state, setState] = createStore<State>({ a: 0, b: 0 });
61+
const seen: any[] = [];
62+
let dispose!: () => void;
63+
createRoot(d => {
64+
dispose = d;
65+
createEffect(
66+
() => ({ deep: deep(state), snap: snapshot(state), a: state.a, b: state.b }),
67+
v => void seen.push(v)
68+
);
69+
});
70+
await settle();
71+
expect(seen).toEqual([{ deep: { a: 0, b: 0 }, snap: { a: 0, b: 0 }, a: 0, b: 0 }]);
72+
73+
const { release, done } = gatedAction(setState, true);
74+
await settle();
75+
// Held: nothing reaches the effect's callback while the action runs.
76+
expect(seen).toHaveLength(1);
77+
78+
release();
79+
await done;
80+
await settle();
81+
expect(seen).toHaveLength(2);
82+
expect(seen.at(-1)).toEqual({ deep: { a: 1, b: 1 }, snap: { a: 1, b: 1 }, a: 1, b: 1 });
83+
dispose();
84+
flush();
85+
});
86+
87+
it("a memo over deep() ends equal to the store", async () => {
88+
const [state, setState] = createStore<State>({ a: 0, b: 0 });
89+
let m!: () => State;
90+
let dispose!: () => void;
91+
createRoot(d => {
92+
dispose = d;
93+
m = createMemo(() => deep(state));
94+
createEffect(
95+
() => m(),
96+
() => {}
97+
);
98+
});
99+
await settle();
100+
expect(m()).toEqual({ a: 0, b: 0 });
101+
102+
const { release, done } = gatedAction(setState, true);
103+
await settle();
104+
release();
105+
await done;
106+
await settle();
107+
expect(snapshot(state)).toEqual({ a: 1, b: 1 });
108+
expect(m()).toEqual({ a: 1, b: 1 });
109+
dispose();
110+
flush();
111+
});
112+
113+
it("a write before the yield only lands in deep() too (already worked)", async () => {
114+
const [state, setState] = createStore<State>({ a: 0, b: 0 });
115+
const seen: any[] = [];
116+
let dispose!: () => void;
117+
createRoot(d => {
118+
dispose = d;
119+
createEffect(
120+
() => ({ deep: deep(state), snap: snapshot(state), a: state.a, b: state.b }),
121+
v => void seen.push(v)
122+
);
123+
});
124+
await settle();
125+
126+
const { release, done } = gatedAction(setState, false);
127+
await settle();
128+
release();
129+
await done;
130+
await settle();
131+
expect(seen.at(-1)).toEqual({ deep: { a: 1, b: 0 }, snap: { a: 1, b: 0 }, a: 1, b: 0 });
132+
dispose();
133+
flush();
134+
});
135+
136+
it("the same writes without an action, stepped by hand, land in deep() (already worked)", async () => {
137+
const [state, setState] = createStore<State>({ a: 0, b: 0 });
138+
const seen: any[] = [];
139+
let dispose!: () => void;
140+
createRoot(d => {
141+
dispose = d;
142+
createEffect(
143+
() => ({ deep: deep(state), snap: snapshot(state), a: state.a, b: state.b }),
144+
v => void seen.push(v)
145+
);
146+
});
147+
await settle();
148+
149+
let release!: () => void;
150+
const gate = new Promise<void>(r => (release = r));
151+
const body = function* () {
152+
setState(d => {
153+
d.a = 1;
154+
});
155+
yield gate;
156+
setState(d => {
157+
d.b = 1;
158+
});
159+
};
160+
const it = body();
161+
const first = it.next();
162+
await settle();
163+
expect(seen.at(-1)).toEqual({ deep: { a: 1, b: 0 }, snap: { a: 1, b: 0 }, a: 1, b: 0 });
164+
release();
165+
await first.value;
166+
it.next();
167+
await settle();
168+
expect(seen.at(-1)).toEqual({ deep: { a: 1, b: 1 }, snap: { a: 1, b: 1 }, a: 1, b: 1 });
169+
dispose();
170+
flush();
171+
});
172+
});

0 commit comments

Comments
 (0)