Skip to content

Commit 39051bf

Browse files
fix(signals): tracked store reads keep a data value that replaced a getter (#3951)
* fix(signals): tracked store reads keep a data value that replaced a getter (#3949) Co-authored-by: Cursor <[email protected]> * fix(signals): notify an accessor replaced by data on the existing write (#3949) The dedicated data-value write was copied onto both notify paths and grew the store scenarios past the minified size allowance. Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
1 parent c189013 commit 39051bf

3 files changed

Lines changed: 154 additions & 5 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+
Tracked store reads keep a data value that replaced an own getter

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,16 +1176,19 @@ function notifyWrites(t: StoreTarget): void {
11761176
) {
11771177
// Accessor keys: the node is linked for shape-change notification,
11781178
// its value is never served (the getter runs with the proxy
1179-
// receiver on read) — FORCE wakes the readers.
1179+
// receiver on read) — FORCE wakes the readers. A data descriptor
1180+
// falls through so the cleared-acc hot read serves it (#3949);
1181+
// an own getter's descriptor has no `value`, so the compare below
1182+
// would otherwise keep the getter's result (or FORCE) for `undefined`.
11801183
(node as any).acc = isOwnAccessor(pb, key);
11811184
const od = Object.getOwnPropertyDescriptor(old, key);
11821185
const nd = Object.getOwnPropertyDescriptor(pb, key);
1183-
if ((od && (od.get || od.set)) || (nd && (nd.get || nd.set))) {
1186+
if (!nd || nd.get || nd.set) {
11841187
if (od?.get !== nd?.get || od?.set !== nd?.set || od?.value !== nd?.value)
11851188
setSignal(node, () => FORCE as any);
11861189
continue;
11871190
}
1188-
if (!isEqual(od?.value, nd?.value)) setSignal(node, () => nd?.value);
1191+
if (od?.get || od?.set || !isEqual(od?.value, nd.value)) setSignal(node, () => nd.value);
11891192
continue;
11901193
}
11911194
const nv = t.del !== null && t.del.has(key) ? undefined : pb[key as any];
@@ -1297,14 +1300,15 @@ export function notifyKeyDiff(
12971300
(node as any).acc = isOwnAccessor(neu, key);
12981301
const od = Object.getOwnPropertyDescriptor(old, key);
12991302
const nd = Object.getOwnPropertyDescriptor(neu, key);
1300-
if ((od && (od.get || od.set)) || (nd && (nd.get || nd.set))) {
1303+
// Data descriptors fall through, as in notifyWrites (#3949).
1304+
if (!nd || nd.get || nd.set) {
13011305
if (od?.get !== nd?.get || od?.set !== nd?.set || od?.value !== nd?.value)
13021306
setSignal(node, () => FORCE as any);
13031307
return;
13041308
}
13051309
const ov = od?.value;
13061310
const nv = nd?.value;
1307-
if (!isEqual(ov, nv) && !targetsEqual(ov, nv))
1311+
if (od?.get || od?.set || (!isEqual(ov, nv) && !targetsEqual(ov, nv)))
13081312
setSignal(node, typeof nv === "function" ? () => nv : (nv as any));
13091313
} else {
13101314
const ov = old[key as any];
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/**
2+
* #3949 — replacing an own getter with a plain data value wrote the FORCE
3+
* sentinel onto a node whose accessor flag had just been cleared. The hot
4+
* read then served that sentinel's backing argument, `undefined`, to tracked
5+
* readers. snapshot() and a node-less read already returned the data value.
6+
*/
7+
import {
8+
createRenderEffect,
9+
createRoot,
10+
createStore,
11+
flush,
12+
reconcile,
13+
snapshot
14+
} from "../../src/index.js";
15+
16+
function withGetter(): { x: number } {
17+
return {
18+
get x() {
19+
return 1;
20+
}
21+
};
22+
}
23+
24+
function trackX(store: { x?: number }): number[] {
25+
const seen: number[] = [];
26+
createRoot(() => {
27+
createRenderEffect(
28+
() => store.x,
29+
v => {
30+
seen.push(v as number);
31+
}
32+
);
33+
});
34+
flush();
35+
return seen;
36+
}
37+
38+
describe("#3949 accessor key replaced by data", () => {
39+
test("reconcile of an own getter notifies the data value", () => {
40+
const [store, setStore] = createStore(withGetter());
41+
const seen = trackX(store);
42+
43+
setStore(reconcile({ x: 2 }, null));
44+
flush();
45+
46+
expect(seen).toEqual([1, 2]);
47+
expect(store.x).toBe(2);
48+
expect(snapshot(store).x).toBe(2);
49+
});
50+
51+
test("defineProperty inside a setter draft replaces a getter with data", () => {
52+
const [store, setStore] = createStore(withGetter());
53+
const seen = trackX(store);
54+
55+
setStore(s => {
56+
Object.defineProperty(s, "x", {
57+
configurable: true,
58+
enumerable: true,
59+
writable: true,
60+
value: 2
61+
});
62+
});
63+
flush();
64+
65+
expect(seen).toEqual([1, 2]);
66+
expect(store.x).toBe(2);
67+
expect(snapshot(store).x).toBe(2);
68+
});
69+
70+
test("a setter that returns a replacement object replaces a getter with data", () => {
71+
const [store, setStore] = createStore(withGetter());
72+
const seen = trackX(store);
73+
74+
setStore(() => ({ x: 2 }));
75+
flush();
76+
77+
expect(seen).toEqual([1, 2]);
78+
expect(store.x).toBe(2);
79+
expect(snapshot(store).x).toBe(2);
80+
});
81+
82+
test("a plain data key still notifies", () => {
83+
const [store, setStore] = createStore({ x: 1 });
84+
const seen = trackX(store);
85+
86+
setStore(s => {
87+
s.x = 2;
88+
});
89+
flush();
90+
91+
expect(seen).toEqual([1, 2]);
92+
expect(store.x).toBe(2);
93+
expect(snapshot(store).x).toBe(2);
94+
});
95+
96+
test("a getter replaced by the same data value keeps that value", () => {
97+
const [store, setStore] = createStore(withGetter());
98+
const seen = trackX(store);
99+
100+
setStore(reconcile({ x: 1 }, null));
101+
flush();
102+
103+
expect(seen.at(-1)).toBe(1);
104+
expect(store.x).toBe(1);
105+
expect(snapshot(store).x).toBe(1);
106+
});
107+
108+
test("a getter replaced by an undefined data value reads undefined", () => {
109+
const [store, setStore] = createStore<{ x?: number }>(withGetter());
110+
const seen = trackX(store);
111+
112+
setStore(s => {
113+
Object.defineProperty(s, "x", {
114+
configurable: true,
115+
enumerable: true,
116+
writable: true,
117+
value: undefined
118+
});
119+
});
120+
flush();
121+
122+
expect(seen).toEqual([1, undefined]);
123+
expect(store.x).toBeUndefined();
124+
expect(snapshot(store).x).toBeUndefined();
125+
});
126+
127+
test("getter to deleted still reads undefined for a tracked reader", () => {
128+
const [store, setStore] = createStore<{ x?: number }>(withGetter());
129+
const seen = trackX(store);
130+
131+
setStore(s => {
132+
delete s.x;
133+
});
134+
flush();
135+
136+
expect(seen).toEqual([1, undefined]);
137+
expect(store.x).toBeUndefined();
138+
expect(snapshot(store).x).toBeUndefined();
139+
});
140+
});

0 commit comments

Comments
 (0)