Skip to content

Commit f17cd09

Browse files
authored
fix(signals): serve the landed frame to deep() and snapshot() in a pass (#4551)
1 parent 3c47d06 commit f17cd09

3 files changed

Lines changed: 203 additions & 7 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()` read inside a computation now serve the frame the store's container node serves that computation. Before, a container held by a transaction (an action that wrote the store on both sides of a yield) always served its committed backing to `deep()`/`snapshot()`, even in the transaction's own landing flush, where the same computation's property reads already saw the landed values: a render effect reading `s.x` and `deep(s)` logged `3:{"x":1}` and was never re-run. The same applied to a derived `createStore(fn, seed)` whose user setter wrote across a yield. Reads outside a computation still see the committed backing of a held container.

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,14 +2741,19 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreFamily | nul
27412741
}
27422742
if (t._fam !== null) fam = t._fam;
27432743
// R27: a snapshot sees the batch's pending backing — a held one (another
2744-
// transaction's future) only from its own draft.
2744+
// transaction's future) only from its own draft, or from a pass the
2745+
// container node serves the future (its landing flush, a memo joining).
27452746
let backing = t.v;
2746-
if (
2747-
t.pb !== null &&
2748-
(inDraft(t) || writeOverride || t.k === null || !(t.k._config & CONFIG_HELD))
2749-
) {
2750-
if (t.ovl) materializePB(t);
2751-
backing = t.pb!;
2747+
const pb = t.pb;
2748+
if (pb !== null) {
2749+
const k = t.k;
2750+
backing =
2751+
inDraft(t) || writeOverride || k === null || !(k._config & CONFIG_HELD)
2752+
? pb
2753+
: stagingReader() !== null
2754+
? asBacking(untrack(() => readNode(k)))
2755+
: t.v;
2756+
if (backing === pb && t.ovl) backing = materializePB(t);
27522757
}
27532758
if (optHooks && optRead(t)) backing = optHooks.view(t, backing);
27542759
if (backing === src) break;
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import { describe, expect, test } from "vitest";
2+
import {
3+
action,
4+
createRenderEffect,
5+
createMemo,
6+
createRoot,
7+
createSignal,
8+
createStore,
9+
deep,
10+
flush,
11+
snapshot
12+
} from "../src/index.js";
13+
14+
const tick = () => new Promise(r => setTimeout(r, 0));
15+
16+
describe("F-1: deep()/snapshot() after an action that writes a store on both sides of a yield", () => {
17+
test("F-1: a render effect reading s.x and deep(s) sees one frame at the landing", async () => {
18+
const [s, set] = createStore<{ x: number }>({ x: 1 });
19+
const log: string[] = [];
20+
createRoot(() => {
21+
createRenderEffect(
22+
() => `${s.x}:${JSON.stringify(deep(s))}`,
23+
v => void log.push(v)
24+
);
25+
});
26+
flush();
27+
28+
let release!: () => void;
29+
const act = action(function* () {
30+
set(d => {
31+
d.x = 2;
32+
});
33+
yield new Promise<void>(r => (release = r));
34+
set(d => {
35+
d.x = 3;
36+
});
37+
});
38+
act();
39+
await tick();
40+
flush();
41+
release();
42+
for (let i = 0; i < 4; i++) {
43+
await tick();
44+
flush();
45+
}
46+
47+
expect(snapshot(s)).toEqual({ x: 3 });
48+
expect(log.at(-1)).toBe('3:{"x":3}');
49+
for (const entry of log) expect(entry).toBe(`${entry[0]}:{"x":${entry[0]}}`);
50+
});
51+
52+
test("F-1: snapshot() inside the render effect sees the landed frame", async () => {
53+
const [s, set] = createStore<{ x: number }>({ x: 1 });
54+
const log: string[] = [];
55+
createRoot(() => {
56+
createRenderEffect(
57+
() => `${s.x}:${JSON.stringify(snapshot(s))}`,
58+
v => void log.push(v)
59+
);
60+
});
61+
flush();
62+
let release!: () => void;
63+
action(function* () {
64+
set(d => {
65+
d.x = 2;
66+
});
67+
yield new Promise<void>(r => (release = r));
68+
set(d => {
69+
d.x = 3;
70+
});
71+
})();
72+
await tick();
73+
flush();
74+
release();
75+
for (let i = 0; i < 4; i++) {
76+
await tick();
77+
flush();
78+
}
79+
expect(log.at(-1)).toBe('3:{"x":3}');
80+
});
81+
82+
test("F-1: a derived createStore(fn, seed) serves the user setter's writes to deep()", async () => {
83+
const [n, setN] = createSignal(1);
84+
let s: any, set: any;
85+
createRoot(() => {
86+
[s, set] = createStore<any>(
87+
(d: any) => {
88+
d.n = n();
89+
},
90+
{ n: 0, mine: [] as number[] }
91+
);
92+
});
93+
const log: string[] = [];
94+
createRoot(() =>
95+
createRenderEffect(
96+
() => JSON.stringify(deep(s)),
97+
v => void log.push(v)
98+
)
99+
);
100+
flush();
101+
let release!: () => void;
102+
action(function* () {
103+
set((d: any) => void d.mine.push(1));
104+
yield new Promise<void>(r => (release = r));
105+
set((d: any) => void d.mine.push(2));
106+
})();
107+
await tick();
108+
flush();
109+
setN(5);
110+
flush();
111+
release();
112+
for (let i = 0; i < 4; i++) {
113+
await tick();
114+
flush();
115+
}
116+
expect(snapshot(s)).toEqual({ n: 5, mine: [1, 2] });
117+
expect(log.at(-1)).toBe(JSON.stringify({ n: 5, mine: [1, 2] }));
118+
});
119+
120+
async function runAcrossYield(first: () => void, second: () => void) {
121+
let release!: () => void;
122+
action(function* () {
123+
first();
124+
yield new Promise<void>(r => (release = r));
125+
second();
126+
})();
127+
await tick();
128+
flush();
129+
release();
130+
for (let i = 0; i < 4; i++) {
131+
await tick();
132+
flush();
133+
}
134+
}
135+
136+
test("F-1: a nested object written across the yield is whole in deep()", async () => {
137+
const [s, set] = createStore({ a: { b: { c: 1 } } });
138+
const log: string[] = [];
139+
createRoot(() =>
140+
createRenderEffect(
141+
() => `${s.a.b.c}:${JSON.stringify(deep(s))}`,
142+
v => void log.push(v)
143+
)
144+
);
145+
flush();
146+
await runAcrossYield(
147+
() => set(d => void (d.a.b.c = 2)),
148+
() => set(d => void (d.a.b.c = 3))
149+
);
150+
expect(snapshot(s)).toEqual({ a: { b: { c: 3 } } });
151+
expect(log.at(-1)).toBe('3:{"a":{"b":{"c":3}}}');
152+
});
153+
154+
test("F-1: an array pushed on both sides of the yield is whole in deep()", async () => {
155+
const [s, set] = createStore({ list: [1] });
156+
const log: string[] = [];
157+
createRoot(() =>
158+
createRenderEffect(
159+
() => `${s.list.length}:${JSON.stringify(deep(s.list))}`,
160+
v => void log.push(v)
161+
)
162+
);
163+
flush();
164+
await runAcrossYield(
165+
() => set(d => void d.list.push(2)),
166+
() => set(d => void d.list.push(3))
167+
);
168+
expect(snapshot(s)).toEqual({ list: [1, 2, 3] });
169+
expect(log.at(-1)).toBe("3:[1,2,3]");
170+
});
171+
172+
test("F-1: a memo reading s.x and snapshot(s) agrees after the action settles", async () => {
173+
const [s, set] = createStore({ x: 1 });
174+
const log: string[] = [];
175+
createRoot(() => {
176+
const m = createMemo(() => `${s.x}:${JSON.stringify(snapshot(s))}`);
177+
createRenderEffect(m, v => void log.push(v));
178+
});
179+
flush();
180+
await runAcrossYield(
181+
() => set(d => void (d.x = 2)),
182+
() => set(d => void (d.x = 3))
183+
);
184+
expect(log.at(-1)).toBe('3:{"x":3}');
185+
});
186+
});

0 commit comments

Comments
 (0)