Skip to content

Commit 9d1cf78

Browse files
ryansolidnickshirocursoragent
committed
fix(signals): a retained projection draft row resolves to its store proxy (#3767)
A derived store whose derive returns rows from its own draft (`draft => [...source(), ...draft.filter(r => r.failed)]`) puts the draft wrapper itself into the result. On the next source refresh reconcile's descend unwrapped the wrapper to itself and adopted it as the row's own backing, so every read of the row forwarded to itself until the stack overflowed. `wrapDraft` now registers the wrapper as its store target's alias in the family map (or the global lookup), so `wrap`, `unwrapValue` and reconcile's slot matching all resolve it to the store proxy: the row keeps its identity, writes through the setter land, and an equivalent refresh no longer re-runs readers of the retained row. Size-Exception: +20 B on `signals: + createStore` (14.56 -> 14.58 KB) and the frozen `page: live server components` cap (48.51 -> 48.53 KB), accepted by the maintainer for #3767. Co-authored-by: Nick <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 01e80a6 commit 9d1cf78

7 files changed

Lines changed: 205 additions & 6 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+
A derived store that returns rows from its own draft no longer recurses forever when its source refreshes: the projection draft wrapper resolves to its store proxy wherever the result carries it, so a retained row keeps its identity and stays readable, and an equivalent refresh doesn't re-notify its readers.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type { Computed, Signal } from "../core/types.js";
3333
import type { Refreshable } from "../core/index.js";
3434
import { reconcileState } from "./reconcile.js";
3535
import { nameStore, runDirect, storeSetter, wrap } from "./store.js";
36-
import type { StoreFamily, StoreTarget } from "./target.js";
36+
import { storeLookup, type StoreFamily, type StoreTarget } from "./target.js";
3737
import {
3838
$TARGET,
3939
markRawIngest,
@@ -116,7 +116,12 @@ function wrapDraft(
116116
Reflect.defineProperty(inner, prop, desc);
117117
})
118118
};
119-
return new Proxy(Array.isArray(inner) ? [] : {}, traps);
119+
const px = new Proxy(Array.isArray(inner) ? [] : {}, traps);
120+
// The wrapper stands for its store proxy wherever the derive's result
121+
// carries it (`wrap`, `unwrapValue`, reconcile's slot matching).
122+
const t: StoreTarget | undefined = inner[$TARGET];
123+
if (t) (t.fam ? t.fam.map : storeLookup).set(px, t);
124+
return px;
120125
}
121126

122127
function cloneState<T extends object>(v: T, shallow: boolean): T {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,14 @@ export function wrap<T extends Record<PropertyKey, any>>(
268268
return createTarget(value, parent, parentKey, fam).px;
269269
}
270270

271-
/** Unwrap our own proxies to their current backing; leave everything else. */
271+
/** Unwrap our own proxies to their current backing; leave everything else.
272+
* A projection draft wrapper is registered as its target's alias
273+
* (projection.ts `wrapDraft`) — adopted as a backing, it would forward to
274+
* itself (#3767). */
272275
export function unwrapValue(v: any): any {
273276
if (v == null || typeof v !== "object") return v;
274277
const t: StoreTarget | undefined = v[$TARGET];
275-
if (t !== undefined && t.px === v && t.v !== undefined) {
278+
if (t !== undefined && (t.px === v || lookupTarget(v, t.fam) === t) && t.v !== undefined) {
276279
// A draft escaping into other storage must be a REAL container that
277280
// becomes this target's committed backing at fold (the shared-raw
278281
// contract) — a prototype overlay is neither.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* #3767 — a writable derived store whose derive returns one of its own draft
3+
* rows (`draft.filter(...)`) overflowed the stack once its source refreshed.
4+
* The row read through the draft is a draft WRAPPER (a proxy over a fake
5+
* target forwarding to the store proxy, projection.ts `wrapDraft`), not the
6+
* store's own proxy; reconcile took it for a foreign value and adopted it as
7+
* the row's backing, so the row's backing forwarded to the row itself.
8+
*/
9+
import { describe, expect, it } from "vitest";
10+
import {
11+
createRenderEffect,
12+
createRoot,
13+
createSignal,
14+
createStore,
15+
flush
16+
} from "../../src/index.js";
17+
18+
type Row = { id: number; title: string; failed?: boolean };
19+
20+
describe("#3767 a derive retaining its own draft row", () => {
21+
it("keeps the row readable after the source refreshes", () => {
22+
const [source, setSource] = createSignal<Row[]>([{ id: 1, title: "server" }]);
23+
let rows!: Row[];
24+
let setRows!: (fn: (draft: Row[]) => Row[] | void) => void;
25+
const dispose = createRoot(d => {
26+
[rows, setRows] = createStore<Row[]>(
27+
draft => [...source(), ...draft.filter(row => row.failed)],
28+
[]
29+
);
30+
return d;
31+
});
32+
flush();
33+
expect(rows.map(r => r.id)).toEqual([1]);
34+
35+
setRows(draft => {
36+
draft.push({ id: 2, title: "local", failed: true });
37+
});
38+
flush();
39+
expect(rows.map(r => r.id)).toEqual([1, 2]);
40+
const local = rows[1];
41+
42+
setSource([{ id: 1, title: "server" }]);
43+
flush();
44+
expect(rows.map(r => r.id)).toEqual([1, 2]);
45+
expect(Object.keys(rows[1])).toEqual(["id", "title", "failed"]);
46+
expect({ ...rows[1] }).toEqual({ id: 2, title: "local", failed: true });
47+
expect(rows[1]).toBe(local);
48+
49+
setSource([{ id: 1, title: "server 2" }]);
50+
flush();
51+
expect(rows.map(r => r.title)).toEqual(["server 2", "local"]);
52+
expect(rows[1]).toBe(local);
53+
54+
// The retained row is still the store's row: a write through the setter
55+
// lands, and the next derive keeps it.
56+
setRows(draft => {
57+
draft[1].title = "edited";
58+
});
59+
flush();
60+
expect(rows[1].title).toBe("edited");
61+
setSource([{ id: 1, title: "server 3" }]);
62+
flush();
63+
expect(rows.map(r => r.title)).toEqual(["server 3", "edited"]);
64+
expect(rows[1]).toBe(local);
65+
dispose();
66+
});
67+
68+
it("re-renders a reader of the retained row", () => {
69+
const [source, setSource] = createSignal<Row[]>([{ id: 1, title: "server" }]);
70+
let rows!: Row[];
71+
let setRows!: (fn: (draft: Row[]) => Row[] | void) => void;
72+
const seen: string[][] = [];
73+
const dispose = createRoot(d => {
74+
[rows, setRows] = createStore<Row[]>(
75+
draft => [...source(), ...draft.filter(row => row.failed)],
76+
[]
77+
);
78+
createRenderEffect(
79+
() => rows.map(r => r.title),
80+
titles => {
81+
seen.push(titles);
82+
}
83+
);
84+
return d;
85+
});
86+
flush();
87+
setRows(draft => {
88+
draft.push({ id: 2, title: "local", failed: true });
89+
});
90+
flush();
91+
setSource([{ id: 1, title: "server" }]);
92+
flush();
93+
setRows(draft => {
94+
draft[1].title = "edited";
95+
});
96+
flush();
97+
expect(seen).toEqual([["server"], ["server", "local"], ["server", "edited"]]);
98+
dispose();
99+
});
100+
101+
it("retains a nested draft record of an object store", () => {
102+
type State = { server: string; local?: { note: string } };
103+
const [source, setSource] = createSignal("a");
104+
let state!: State;
105+
let setState!: (fn: (draft: State) => State | void) => void;
106+
const dispose = createRoot(d => {
107+
[state, setState] = createStore<State>(draft => ({ server: source(), local: draft.local }), {
108+
server: ""
109+
});
110+
return d;
111+
});
112+
flush();
113+
setState(draft => {
114+
draft.local = { note: "kept" };
115+
});
116+
flush();
117+
const local = state.local;
118+
setSource("b");
119+
flush();
120+
expect(state.server).toBe("b");
121+
expect({ ...state.local }).toEqual({ note: "kept" });
122+
expect(state.local).toBe(local);
123+
setSource("c");
124+
flush();
125+
expect({ ...state.local }).toEqual({ note: "kept" });
126+
dispose();
127+
});
128+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createRoot, createSignal, createStore, flush, snapshot } from "../../src/index.js";
2+
3+
type Row = { id: string; failed?: boolean };
4+
5+
it("retains a draft row after a derived store adopts an equivalent source", () => {
6+
const { rows, setRows, setSource, dispose } = createRoot(dispose => {
7+
const [source, setSource] = createSignal<Row[]>([{ id: "remote" }]);
8+
const [rows, setRows] = createStore<Row[]>(
9+
draft => [...source(), ...draft.filter(row => row.failed)],
10+
[]
11+
);
12+
return { rows, setRows, setSource, dispose };
13+
});
14+
15+
expect(rows.map(row => row.id)).toEqual(["remote"]);
16+
17+
setRows(draft => {
18+
draft.push({ id: "local", failed: true });
19+
});
20+
flush();
21+
expect(rows.map(row => row.id)).toEqual(["remote", "local"]);
22+
const local = rows[1];
23+
24+
setSource([{ id: "remote" }]);
25+
flush();
26+
27+
expect(rows.map(row => row.id)).toEqual(["remote", "local"]);
28+
expect(rows[1]).toBe(local);
29+
expect(Object.keys(rows[1])).toEqual(["id", "failed"]);
30+
expect(rows[1].failed).toBe(true);
31+
expect(snapshot(rows)).toEqual([{ id: "remote" }, { id: "local", failed: true }]);
32+
33+
setRows(draft => {
34+
draft[1].failed = false;
35+
});
36+
flush();
37+
expect(rows[1].failed).toBe(false);
38+
39+
setSource([{ id: "remote" }]);
40+
flush();
41+
expect(rows.map(row => row.id)).toEqual(["remote"]);
42+
43+
dispose();
44+
});

‎scripts/size/floor-caps.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"app: render + one signal (the simple-app floor)": "9.86 KB",
44
"app: hydrating (no stores) with Show/For/Loading/Errored/lazy": "17.71 KB",
55
"page: base server components (hydrating + dynamic + frames + sf reference)": "44.84 KB",
6-
"page: live server components (base + live/GET + action + isPending/latest)": "48.51 KB",
6+
"page: live server components (base + live/GET + action + isPending/latest)": "48.53 KB",
77
"server: floor (getRequestEvent + isServer)": "1.34 KB",
88
"server: renderToString (the server-render floor)": "20.42 KB"
99
}

‎scripts/size/scenarios.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,14 @@ module.exports = [
887887
// lane re-stage +13, verdict pull +1). Cap set at measured + 10 B rounded
888888
// up to 0.01 KB. Accepted by the maintainer (2026-10-05). The cap is
889889
// frozen again at 14.56 KB.
890-
limit: "14.56 KB",
890+
// Size-Exception (#3767, 2026-10-05): 14.56 -> 14.58 KB, measured at
891+
// 14,565 B against `next` @ 01e80a601's 14,546 (+19 B; 5 B over the cap;
892+
// +63 B minified) — a projection draft wrapper registers as its store
893+
// target's alias, and `unwrapValue` resolves the alias (a retained draft
894+
// row adopted as its own backing recursed forever). Cap set at measured
895+
// + 10 B rounded up to 0.01 KB. Accepted by the maintainer (2026-10-05).
896+
// The cap is frozen again at 14.58 KB.
897+
limit: "14.58 KB",
891898
alias
892899
},
893900
{
@@ -3408,6 +3415,13 @@ module.exports = [
34083415
// lane re-stage +13, verdict pull +1). Cap set at measured + 10 B rounded
34093416
// up to 0.01 KB. Accepted by the maintainer (2026-10-05). The cap is
34103417
// frozen again at 48.51 KB.
3418+
// Size-Exception (#3767, 2026-10-05): 48.51 -> 48.53 KB
3419+
// (floor-caps.json), measured at 48,514 B against `next` @ 01e80a601's
3420+
// 48,495 (+19 B; 4 B over the cap; +63 B minified) — the #3767 store
3421+
// alias (draft wrapper registration + `unwrapValue` alias check; see
3422+
// `signals: + createStore`). Cap set at measured + 10 B rounded up to
3423+
// 0.01 KB. Accepted by the maintainer (2026-10-05). The cap is frozen
3424+
// again at 48.53 KB.
34113425
limit: floorCaps["page: live server components (base + live/GET + action + isPending/latest)"],
34123426
alias: pageAlias
34133427
},

0 commit comments

Comments
 (0)