Skip to content

Commit 656f0df

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(signals): optimistic store writes revert after a failed action with attribution enabled (#3692)
checkOptimisticRevert detached the node's _equals and called it bare. Store slot nodes share slotNodeEquals, which reads this._host, so the check threw inside resolveOptimisticNodes before the drop notified subscribers — an optimistic store write never reverted after a failed action whenever the attribution engine was enabled. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent c71486d commit 656f0df

3 files changed

Lines changed: 58 additions & 1 deletion

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+
Fix `createOptimisticStore` writes not reverting after a failed action while the attribution engine is enabled (#3687). The `OPTIMISTIC_REVERTED` check called the node's comparator detached from the node; store slot nodes share one comparator that reads `this._host`, so the check threw inside the settle before the drop notified subscribers. The comparator is now invoked as the node's method.

‎packages/signals/src/core/attribution.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3026,8 +3026,10 @@ function checkOptimisticRevert(
30263026
// derived override promotes rather than reverts. Same predicate the hold
30273027
// census uses to skip them.
30283028
if (!options.optimisticReverts || isCompanion(el)) return;
3029+
// Method call, as every commit path does: store slot nodes share one
3030+
// comparator that reads `this._host` (#3687).
30293031
const equals = (el as { _equals?: false | ((a: unknown, b: unknown) => boolean) })._equals;
3030-
if (equals && equals(shown, truth)) return;
3032+
if (equals && equals.call(el, shown, truth)) return;
30313033
const source = nodeName(el);
30323034
// The two values are user data: quoted only under `values: "full"`; the
30333035
// other levels say what happened without saying what was shown.

‎packages/signals/tests/attribution-optimistic-revert.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
action,
1515
createMemo,
1616
createOptimistic,
17+
createOptimisticStore,
1718
createRenderEffect,
1819
createRoot,
1920
createSignal,
@@ -196,6 +197,55 @@ describe("OPTIMISTIC_REVERTED", () => {
196197
expect(findings).toHaveLength(0);
197198
});
198199

200+
it("a store's optimistic write reverts with the engine on (#3687)", async () => {
201+
// Store slot nodes share one `_equals` that reads `this._host`; the check
202+
// must call it as the node's method. Detached, it threw from inside the
203+
// settle before the drop notified subscribers — the store never reverted
204+
// for anyone reading it, and only when attribution was enabled.
205+
const { findings } = arm();
206+
const gate = deferred();
207+
type Row = { id: string; done: boolean };
208+
const [list, setList] = createOptimisticStore<Row[]>(
209+
() => Promise.resolve([{ id: "a", done: false }]),
210+
[],
211+
{ key: "id", name: "list" }
212+
);
213+
const seen: string[] = [];
214+
createRoot(() =>
215+
createRenderEffect(
216+
() => list.map(r => `${r.id}:${r.done}`).join(" "),
217+
v => void seen.push(v),
218+
{ name: "rows" }
219+
)
220+
);
221+
flush();
222+
await new Promise(r => setTimeout(r, 0));
223+
flush();
224+
expect(seen.at(-1)).toBe("a:false");
225+
const toggle = action(function* toggle() {
226+
setList(l => {
227+
l[0].done = true;
228+
});
229+
yield gate.promise;
230+
// The server said no; nothing writes the truth. The override lifts.
231+
});
232+
const p = toggle();
233+
flush();
234+
expect(seen.at(-1)).toBe("a:true");
235+
gate.resolve();
236+
await p;
237+
flush();
238+
expect(list[0].done).toBe(false);
239+
expect(seen.at(-1)).toBe("a:false");
240+
expect(findings).toHaveLength(1);
241+
expect(findings[0].data).toMatchObject({
242+
source: "list.done",
243+
shown: "true",
244+
truth: "false",
245+
how: "reverted"
246+
});
247+
});
248+
199249
it("`optimisticReverts: false` disables the finding", async () => {
200250
const { findings } = arm({ optimisticReverts: false });
201251
const gate = deferred();

0 commit comments

Comments
 (0)