Skip to content

Commit 8890092

Browse files
brenelzclaude
andauthored
fix(signals): refresh() lifts a stale manual-write mask inside a transaction (#3026) (#3029)
A derived store's setter masks the recompute for the tick (REACTIVE_MANUAL_WRITE) so a manual write wins over a queued recompute (#2692). The mask only clears at the pending-node drain, which inside an action's transaction is the whole action — so a setStore absorbed into the transaction made every later refresh() in that action a silent no-op: the store source never re-ran. Stamp the clock on the mask (suppressComputedRecompute) and let refresh() distinguish the two cases: a manual write in the same synchronous tick still wins in both orders (the #2692 contract, unchanged), while a mask stamped in an earlier tick — only possible when a transaction holds the drain open — is lifted by an explicit refresh, which re-runs the source and reconciles over the transaction draft at commit. No quiet re-ask (REASK) classification when lifting: the batch carries a real value change. Fixes #3026 Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 4fe1536 commit 8890092

5 files changed

Lines changed: 174 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+
Fix `refresh(store)` being silently swallowed inside an `action()` after a `setStore` in the same transaction (#3026). The derived-store manual-write mask (which lets a same-tick manual write win over a queued recompute, #2692) previously persisted for the whole transaction, so any setStore early in an action made every later `refresh()` a no-op. An explicit `refresh()` now lifts a mask stamped in an earlier tick and re-runs the source; a manual write in the same synchronous tick still wins in both orders, preserving the #2692 contract.

‎packages/solid-signals/src/core/core.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ export function suppressComputedRecompute(el: Computed<unknown>): void {
11081108
schedule();
11091109
}
11101110
el._flags = (el._flags & ~(REACTIVE_DIRTY | REACTIVE_CHECK)) | REACTIVE_MANUAL_WRITE;
1111+
el._manualWriteTime = clock;
11111112
}
11121113

11131114
/**
@@ -1232,18 +1233,27 @@ export function refresh<T>(target: Refreshable<T>): void {
12321233
});
12331234
throw new Error(REACTIVE_WRITE_IN_OWNED_SCOPE_REFRESH_MESSAGE);
12341235
}
1235-
if (
1236-
typeof node._fn === "function" &&
1237-
!(node._flags & (REACTIVE_DISPOSED | REACTIVE_MANUAL_WRITE))
1238-
) {
1236+
if (typeof node._fn === "function" && !(node._flags & REACTIVE_DISPOSED)) {
1237+
if (node._flags & REACTIVE_MANUAL_WRITE) {
1238+
// A manual write in the CURRENT tick wins over the refresh (#2692).
1239+
// A mask stamped in an earlier tick only survives because a
1240+
// transaction (action) is holding the pending drain open; there the
1241+
// refresh is a later, explicit re-ask and lifts the mask — otherwise
1242+
// any setStore early in an action silently swallows every refresh()
1243+
// for the rest of the transaction (#3026).
1244+
if (node._manualWriteTime === clock) return;
1245+
node._flags &= ~REACTIVE_MANUAL_WRITE;
1246+
// No REASK below: the batch carries a manual value change, so the
1247+
// recompute is not a quiet re-ask of an unchanged question.
1248+
}
12391249
// A refresh with no value-change dirt already queued is a re-ask of the
12401250
// same question: mark it so the recompute classifies any resulting
12411251
// pending window as quiet (not pending). If the node is already dirty
12421252
// from a real input change, the question changed — don't mark.
12431253
// REACTIVE_IN_HEAP counts as dirt: insertSubs schedules subscribers by
12441254
// heap insertion alone (no DIRTY/CHECK flag), so a same-batch value
12451255
// change followed by refresh() must not be laundered into a quiet re-ask.
1246-
if (!(node._flags & (REACTIVE_DIRTY | REACTIVE_CHECK | REACTIVE_IN_HEAP))) {
1256+
else if (!(node._flags & (REACTIVE_DIRTY | REACTIVE_CHECK | REACTIVE_IN_HEAP))) {
12471257
node._flags |= REACTIVE_REASK;
12481258
armReaskClear();
12491259
}

‎packages/solid-signals/src/core/types.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ export interface Computed<T> extends RawSignal<T>, Owner {
136136
* (`clearStatus`). Meaningless while not STATUS_PENDING.
137137
*/
138138
_reask: boolean;
139+
/**
140+
* Clock tick at which REACTIVE_MANUAL_WRITE was last applied
141+
* (`suppressComputedRecompute`). Lets `refresh()` distinguish a same-tick
142+
* manual write (which wins over the refresh, #2692) from a mask carried
143+
* across ticks by a transaction (which an explicit refresh lifts, #3026).
144+
* Only meaningful while REACTIVE_MANUAL_WRITE is set.
145+
*/
146+
_manualWriteTime?: number;
139147
/**
140148
* True while a `loadingValue` node's first real answer hasn't landed: the
141149
* node was born committed (commit #0 = the loading value) and `handleAsync`

‎packages/solid-signals/tests/action.test.ts‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import {
55
createRenderEffect,
66
createRoot,
77
createSignal,
8-
flush
8+
createStore,
9+
flush,
10+
refresh
911
} from "../src/index.js";
1012

1113
afterEach(() => flush());
@@ -578,6 +580,89 @@ describe("action", () => {
578580
});
579581
});
580582

583+
describe("refresh(store) inside an action (#3026)", () => {
584+
// A setStore on a derived store masks its recompute for the tick
585+
// (REACTIVE_MANUAL_WRITE, #2692). Inside an action the mask survives
586+
// until the transaction commits, so a later explicit refresh() was
587+
// silently swallowed and the store source never re-ran. refresh() now
588+
// lifts a mask stamped in an earlier tick.
589+
it("re-runs the source after a setStore absorbed into the action transaction", async () => {
590+
const data = { messages: [] as number[] };
591+
let evals = 0;
592+
const [store, setStore] = createRoot(() => {
593+
const [s, sStore] = createStore(
594+
() => {
595+
evals++;
596+
return data;
597+
},
598+
{ messages: [] as number[] }
599+
);
600+
createRenderEffect(
601+
() => JSON.stringify(s.messages),
602+
() => {}
603+
);
604+
return [s, sStore] as const;
605+
});
606+
flush();
607+
expect(evals).toBe(1);
608+
609+
const myAction = action(function* () {
610+
yield new Promise(r => setTimeout(r, 10));
611+
refresh(store);
612+
});
613+
614+
const p = myAction();
615+
// The issue shape: a setStore write while the action's transition is
616+
// still ambient, plus an external source mutation.
617+
setStore(s => {
618+
s.messages.push(1);
619+
});
620+
data.messages.push(2);
621+
await p;
622+
flush();
623+
expect(evals).toBe(2);
624+
});
625+
626+
it("reconciles a fresh source result over the draft write at action commit", async () => {
627+
let fetched = { messages: [10] as number[] };
628+
let evals = 0;
629+
const [store, setStore] = createRoot(() => {
630+
const [s, sStore] = createStore(
631+
() => {
632+
evals++;
633+
return JSON.parse(JSON.stringify(fetched)) as typeof fetched;
634+
},
635+
{ messages: [] as number[] }
636+
);
637+
createRenderEffect(
638+
() => JSON.stringify(s.messages),
639+
() => {}
640+
);
641+
return [s, sStore] as const;
642+
});
643+
flush();
644+
expect(evals).toBe(1);
645+
expect(store.messages).toEqual([10]);
646+
647+
const myAction = action(function* () {
648+
yield new Promise(r => setTimeout(r, 10));
649+
refresh(store);
650+
});
651+
652+
const p = myAction();
653+
setStore(s => {
654+
s.messages.push(1);
655+
});
656+
fetched = { messages: [10, 20] };
657+
await p;
658+
flush();
659+
// refresh() came after the setStore in program order: the re-derived
660+
// result wins over the transaction draft.
661+
expect(evals).toBe(2);
662+
expect(store.messages).toEqual([10, 20]);
663+
});
664+
});
665+
581666
describe("edge cases", () => {
582667
it("should handle empty generator (immediate return)", () => {
583668
const executed = vi.fn();

‎packages/solid-signals/tests/store/createStore.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
merge,
1616
omit,
1717
reconcile,
18+
refresh,
1819
snapshot,
1920
storePath,
2021
untrack
@@ -1322,6 +1323,65 @@ describe("derived store manual writes", () => {
13221323
flush();
13231324
expect(store.count).toBe(2);
13241325
});
1326+
1327+
it("same-tick manual write wins over refresh() in both orders (#2692)", () => {
1328+
let evals = 0;
1329+
const [store, setStore] = createRoot(() => {
1330+
const [source] = createSignal({ count: 0 });
1331+
const [s, sStore] = createStore<{ count: number }>(
1332+
() => {
1333+
evals++;
1334+
return { count: source().count };
1335+
},
1336+
{ count: 0 }
1337+
);
1338+
return [s, sStore] as const;
1339+
});
1340+
flush();
1341+
expect(evals).toBe(1);
1342+
1343+
setStore(s => {
1344+
s.count = 99;
1345+
});
1346+
refresh(store);
1347+
flush();
1348+
expect(evals).toBe(1);
1349+
expect(store.count).toBe(99);
1350+
1351+
refresh(store);
1352+
setStore(s => {
1353+
s.count = 100;
1354+
});
1355+
flush();
1356+
expect(evals).toBe(1);
1357+
expect(store.count).toBe(100);
1358+
});
1359+
1360+
it("refresh() on a later tick re-runs the derived store source (#3026)", () => {
1361+
let evals = 0;
1362+
const [store, setStore] = createRoot(() => {
1363+
const [source] = createSignal({ count: 0 });
1364+
const [s, sStore] = createStore<{ count: number }>(
1365+
() => {
1366+
evals++;
1367+
return { count: source().count };
1368+
},
1369+
{ count: 0 }
1370+
);
1371+
return [s, sStore] as const;
1372+
});
1373+
flush();
1374+
setStore(s => {
1375+
s.count = 99;
1376+
});
1377+
flush();
1378+
expect(store.count).toBe(99);
1379+
1380+
refresh(store);
1381+
flush();
1382+
expect(evals).toBe(2);
1383+
expect(store.count).toBe(0);
1384+
});
13251385
});
13261386

13271387
describe("Proxy invariant correctness", () => {

0 commit comments

Comments
 (0)