Skip to content

Commit fa371c3

Browse files
GabbeVryansolidClaude via Cursorcursoragent
authored
fix(signals): preserve optimistic overrides over derived stores (#3855)
* fix(signals): compare optimistic writes against committed chained frames * test(signals): the held backing write shows after the action settles Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> * chore(size): size exception for optimistic writes over derived stores (#3855) app: hydrating + every store primitive family 28.97 -> 29.02 KB, recorded minified 91,882 B, from Size run 37602728903. Accepted by the maintainer 2026-10-07. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Ryan Carniato <[email protected]> Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent fd9f381 commit fa371c3

4 files changed

Lines changed: 202 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+
Compare optimistic store writes against the committed backing frame of chained stores, so a writable derived store's held staging cannot suppress an optimistic override.

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,25 @@ const hasOwn = Object.prototype.hasOwnProperty;
8383
* restored when the setter's writes become guesses (`writes`). */
8484
const optStaged: Map<StoreTarget, Record<PropertyKey, any>> = new Map();
8585

86+
// A chained backing is a proxy: reading it as a handler can expose a
87+
// derived store's staging. Compose the writer's view on the committed
88+
// backing of each link, retaining any optimistic guesses on that link.
89+
function writerBacking(t: StoreTarget): Record<PropertyKey, any> {
90+
const base = committed(t);
91+
const inner: StoreTarget | undefined = (base as any)[$TARGET];
92+
return inner === undefined
93+
? base
94+
: optimisticView(inner, writerBacking(inner), laneValueOf, true);
95+
}
96+
8697
/** The user's draft on an optimistic family: a clone of the view the user
8798
* saw — the committed frame with the lanes' values and the tick's own
8899
* unflushed guesses over it (#3665). A staging already on the target (a
89100
* landing adopted this flush) is set aside for the setter's duration: the
90101
* guesses go over it, it stays the truth beneath. */
91102
function optimisticDraft(t: StoreTarget): Record<PropertyKey, any> {
92103
if (t.pb !== null) optStaged.set(t, t.pb);
93-
return cloneRaw(optimisticView(t, committed(t), laneValueOf, true), t);
104+
return cloneRaw(optimisticView(t, writerBacking(t), laneValueOf, true), t);
94105
}
95106

96107
export function notifyOptimisticWrites(
@@ -99,7 +110,7 @@ export function notifyOptimisticWrites(
99110
): Record<PropertyKey, any> | null {
100111
// The view the user saw: the committed frame (a staging adopted eagerly
101112
// is not it) with the lanes' values and the tick's own guesses over it.
102-
const base = committed(t);
113+
const base = writerBacking(t);
103114
const old = optimisticView(t, base, laneValueOf, true);
104115
const isArr = Array.isArray(pb);
105116
// A chained target's nodes are links (§7b): their committed value is
@@ -108,8 +119,7 @@ export function notifyOptimisticWrites(
108119
// inner store's truth for its key before guessing over it.
109120
const inner = t.ch ? (t.v as Record<PropertyKey, any>) : null;
110121
const guess = (node: Signal<any>, key: PropertyKey, presence: boolean, nv: unknown): void => {
111-
if (inner !== null)
112-
node._value = untrack(() => (presence ? key in inner : unwrapValue(inner[key as any])));
122+
if (inner !== null) node._value = presence ? key in base : unwrapValue(base[key as any]);
113123
// By value: a function leaf is a value, not an updater (#3017).
114124
optimisticWrite(node, () => nv);
115125
};
@@ -126,7 +136,8 @@ export function notifyOptimisticWrites(
126136
guess(getHasNode(t, key), key, true, true);
127137
structural = true;
128138
} else {
129-
const ov = unwrapValue(old[key as any]);
139+
const previous = old[key as any];
140+
const ov = unwrapValue(inner && previous ? resolveChainedRaw(t, key, previous) : previous);
130141
if (!isEqual(ov, nv) && !targetsEqual(ov, nv)) {
131142
guess(getNode(t, key), key, false, nv);
132143
if (isArr) structural = true;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
action,
4+
createMemo,
5+
createOptimisticStore,
6+
createRenderEffect,
7+
createRoot,
8+
createSignal,
9+
createStore,
10+
isPending
11+
} from "../../src/index.js";
12+
13+
const delay = () => new Promise<void>(resolve => setTimeout(resolve, 20));
14+
15+
// No source change: cover the manual write before any re-derivation occurs.
16+
describe("optimistic shadows over a writable derived store", () => {
17+
it.each([undefined, true])("covers a held flag reset from %s", async initial => {
18+
let dispose!: () => void;
19+
let release!: () => void;
20+
let move!: () => Promise<void>;
21+
let startDrag!: () => void;
22+
let drag!: () => boolean;
23+
let failed!: () => boolean | undefined;
24+
const shown: string[] = [];
25+
createRoot(d => {
26+
dispose = d;
27+
const [source] = createSignal<{ failed?: boolean }>(
28+
initial === undefined ? {} : { failed: initial }
29+
);
30+
const [local, setLocal] = createStore(() => ({ ...source() }), {});
31+
const [view, setView] = createOptimisticStore(local);
32+
failed = () => view.failed;
33+
const [dragging, setDragging] = createSignal(false);
34+
drag = dragging;
35+
startDrag = () => setDragging(true);
36+
const preview = createMemo(() => {
37+
if (!dragging()) return () => "hidden";
38+
return createMemo(() => (view.failed ? "failed" : "ready"));
39+
});
40+
createRenderEffect(
41+
() => preview()(),
42+
value => void shown.push(value)
43+
);
44+
move = action(function* () {
45+
setLocal(draft => {
46+
draft.failed = false;
47+
});
48+
setView(draft => {
49+
draft.failed = false;
50+
});
51+
yield new Promise<void>(resolve => {
52+
release = resolve;
53+
});
54+
});
55+
});
56+
await delay();
57+
const done = move();
58+
try {
59+
await delay();
60+
startDrag();
61+
await delay();
62+
expect(shown).toEqual(["hidden", "ready"]);
63+
expect(isPending(drag)).toBe(false);
64+
} finally {
65+
release();
66+
await done;
67+
await delay();
68+
}
69+
// The action settled: the override is gone and the held backing write shows.
70+
expect(failed()).toBe(false);
71+
expect(shown.at(-1)).toBe("ready");
72+
dispose();
73+
});
74+
75+
it("covers a held reset on a nested row without replacing that row", async () => {
76+
let dispose!: () => void;
77+
let release!: () => void;
78+
let move!: () => Promise<void>;
79+
let startDrag!: () => void;
80+
let drag!: () => boolean;
81+
let failed!: () => boolean | undefined;
82+
const shown: string[] = [];
83+
createRoot(d => {
84+
dispose = d;
85+
const [source] = createSignal<{ id: number; failed?: boolean }[]>([{ id: 1 }]);
86+
const [local, setLocal] = createStore(() => source().map(row => ({ ...row })), []);
87+
const [view, setView] = createOptimisticStore(local);
88+
const row = view[0];
89+
failed = () => row.failed;
90+
const [dragging, setDragging] = createSignal(false);
91+
drag = dragging;
92+
startDrag = () => setDragging(true);
93+
const preview = createMemo(() => {
94+
if (!dragging()) return () => "hidden";
95+
return createMemo(() => (row.failed ? "failed" : "ready"));
96+
});
97+
createRenderEffect(
98+
() => preview()(),
99+
value => void shown.push(value)
100+
);
101+
move = action(function* () {
102+
setLocal(draft => {
103+
draft[0].failed = false;
104+
});
105+
setView(draft => {
106+
draft[0].failed = false;
107+
});
108+
yield new Promise<void>(resolve => {
109+
release = resolve;
110+
});
111+
});
112+
});
113+
await delay();
114+
const done = move();
115+
try {
116+
await delay();
117+
startDrag();
118+
await delay();
119+
expect(shown).toEqual(["hidden", "ready"]);
120+
expect(isPending(drag)).toBe(false);
121+
} finally {
122+
release();
123+
await done;
124+
await delay();
125+
}
126+
expect(failed()).toBe(false);
127+
expect(shown.at(-1)).toBe("ready");
128+
dispose();
129+
});
130+
131+
it("composes with guesses on an inner optimistic store and reverts on settle", async () => {
132+
let dispose!: () => void;
133+
let release!: () => void;
134+
let edit!: () => Promise<void>;
135+
const shown: number[] = [];
136+
createRoot(d => {
137+
dispose = d;
138+
const [base] = createStore({ count: 0 });
139+
const [inner, setInner] = createOptimisticStore(base);
140+
const [outer, setOuter] = createOptimisticStore(inner);
141+
createRenderEffect(
142+
() => outer.count,
143+
value => void shown.push(value)
144+
);
145+
edit = action(function* () {
146+
setInner(draft => {
147+
draft.count = 1;
148+
});
149+
setOuter(draft => {
150+
draft.count++;
151+
});
152+
yield new Promise<void>(resolve => {
153+
release = resolve;
154+
});
155+
});
156+
});
157+
await delay();
158+
const done = edit();
159+
try {
160+
await delay();
161+
expect(shown).toEqual([0, 2]);
162+
} finally {
163+
release();
164+
await done;
165+
await delay();
166+
dispose();
167+
}
168+
expect(shown.at(-1)).toBe(0);
169+
});
170+
});

‎scripts/size/scenarios.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,8 +2148,17 @@ module.exports = [
21482148
// the same signals fixes as the + isPending/latest note (this app
21492149
// retains isPending/latest). Cap set at measured + 10 B rounded up to
21502150
// 0.01 KB. Accepted by the maintainer 2026-10-07.
2151-
limit: "28.97 KB",
2152-
capMinified: 91798,
2151+
// Size-Exception (optimistic writes over a writable derived store, #3855,
2152+
// 2026-10-07): 28.97 -> 29.02 KB, measured at 29,003 B by CI (Size run
2153+
// 37602728903) against `next` @ fd9f381cc's 28,951 (+52 B; 33 B over the
2154+
// cap; +84 B minified, 91,798 -> 91,882) — `store/optimistic.ts`: the
2155+
// optimistic writer's baseline and draft are built from each chained
2156+
// link's committed frame (composing that link's guesses), not from the
2157+
// backing proxy, which exposes a derived store's held staging. Store
2158+
// engine only; every other scenario byte-identical. Cap set at measured
2159+
// + 10 B rounded up to 0.01 KB. Accepted by the maintainer 2026-10-07.
2160+
limit: "29.02 KB",
2161+
capMinified: 91882,
21532162
alias
21542163
},
21552164
{

0 commit comments

Comments
 (0)