Skip to content

Commit b0db6c9

Browse files
fix(signals): preserve manual writes to derived state (#2692)
Manual setter calls for writable derived signals and derived stores now suppress same-tick dependency recomputes, so user writes consistently trump queued derivations until the scheduler drains. Co-authored-by: Cursor <[email protected]>
1 parent 263be3f commit b0db6c9

10 files changed

Lines changed: 212 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Ensure manual writes to writable derived signals and derived stores win over queued same-tick recomputes.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const REACTIVE_DISPOSED = 1 << 6;
99
export const REACTIVE_OPTIMISTIC_DIRTY = 1 << 7;
1010
export const REACTIVE_SNAPSHOT_STALE = 1 << 8;
1111
export const REACTIVE_LAZY = 1 << 9;
12+
export const REACTIVE_MANUAL_WRITE = 1 << 10;
1213

1314
// Static configuration bits packed into Owner/Computed/Signal _config.
1415
export const CONFIG_OWNED_WRITE = 1 << 0;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
REACTIVE_IN_HEAP,
1818
REACTIVE_IN_HEAP_HEIGHT,
1919
REACTIVE_LAZY,
20+
REACTIVE_MANUAL_WRITE,
2021
REACTIVE_NONE,
2122
REACTIVE_OPTIMISTIC_DIRTY,
2223
REACTIVE_RECOMPUTING_DEPS,
@@ -878,6 +879,32 @@ export function setSignal<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T
878879
return v;
879880
}
880881

882+
/**
883+
* Suppresses automatic recomputation of `el` until the scheduler drains. Used
884+
* when a manual write should win over dependency changes queued in the same
885+
* tick. The MANUAL_WRITE flag is cleared by the pending-node drain; projection
886+
* computeds don't commit values, but they still need the same end-of-tick
887+
* cleanup point.
888+
*/
889+
export function suppressComputedRecompute(el: Computed<unknown>): void {
890+
deleteFromHeap(el, el._flags & REACTIVE_ZOMBIE ? zombieQueue : dirtyQueue);
891+
if (!(el._flags & REACTIVE_MANUAL_WRITE) && el._pendingValue === NOT_PENDING)
892+
queuePendingNode(el);
893+
el._flags = (el._flags & ~(REACTIVE_DIRTY | REACTIVE_CHECK)) | REACTIVE_MANUAL_WRITE;
894+
}
895+
896+
/**
897+
* User-facing setter for the memo form of `createSignal(fn)`. Behaves like
898+
* `setSignal`, but also cancels any pending recompute of the memo so the
899+
* manual value wins over a value that would otherwise be produced by an
900+
* upstream change in the same tick.
901+
*/
902+
export function setMemo<T>(el: Computed<T>, v: T | ((prev: T) => T)): T {
903+
const result = setSignal(el, v);
904+
suppressComputedRecompute(el as Computed<unknown>);
905+
return result;
906+
}
907+
881908
/**
882909
* Executes `fn` with the given `owner` set as the current owner. Any reactive
883910
* primitives (`createSignal`, `createMemo`, `createEffect`, `onCleanup`,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
REACTIVE_DIRTY,
44
REACTIVE_IN_HEAP,
55
REACTIVE_IN_HEAP_HEIGHT,
6+
REACTIVE_MANUAL_WRITE,
67
REACTIVE_RECOMPUTING_DEPS
78
} from "./constants.js";
89
import type { Computed, FirewallSignal, Root } from "./types.js";
@@ -39,7 +40,7 @@ function actualInsertIntoHeap(n: Computed<unknown>, heap: Heap) {
3940
}
4041
export function insertIntoHeap(n: Computed<any>, heap: Heap) {
4142
let flags = n._flags;
42-
if (flags & (REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS)) return;
43+
if (flags & (REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS | REACTIVE_MANUAL_WRITE)) return;
4344
if (flags & REACTIVE_CHECK) {
4445
n._flags = (flags & ~(REACTIVE_CHECK | REACTIVE_DIRTY)) | REACTIVE_DIRTY | REACTIVE_IN_HEAP;
4546
} else n._flags = flags | REACTIVE_IN_HEAP;
@@ -48,7 +49,11 @@ export function insertIntoHeap(n: Computed<any>, heap: Heap) {
4849

4950
export function insertIntoHeapHeight(n: Computed<unknown>, heap: Heap) {
5051
let flags = n._flags;
51-
if (flags & (REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS | REACTIVE_IN_HEAP_HEIGHT)) return;
52+
if (
53+
flags &
54+
(REACTIVE_IN_HEAP | REACTIVE_RECOMPUTING_DEPS | REACTIVE_IN_HEAP_HEIGHT | REACTIVE_MANUAL_WRITE)
55+
)
56+
return;
5257
n._flags = flags | REACTIVE_IN_HEAP_HEIGHT;
5358
actualInsertIntoHeap(n, heap);
5459
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export {
77
signal,
88
read,
99
setSignal,
10+
setMemo,
11+
suppressComputedRecompute,
1012
optimisticSignal,
1113
optimisticComputed,
1214
isPending,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
EFFECT_USER,
77
NOT_PENDING,
88
REACTIVE_DISPOSED,
9+
REACTIVE_MANUAL_WRITE,
910
REACTIVE_OPTIMISTIC_DIRTY,
1011
REACTIVE_SNAPSHOT_STALE,
1112
REACTIVE_ZOMBIE,
@@ -529,6 +530,7 @@ function commitPendingNode(n: Signal<any>): void {
529530
// Set _modified for effects, but not for tracked effects (they handle their own scheduling)
530531
if ((n as any)._type && (n as any)._type !== EFFECT_TRACKED) (n as any)._modified = true;
531532
}
533+
c._flags! &= ~REACTIVE_MANUAL_WRITE;
532534
if (!(c._statusFlags! & STATUS_PENDING)) c._statusFlags! &= ~STATUS_UNINITIALIZED;
533535
if (c._pendingFirstChild !== null || c._pendingDisposal !== null)
534536
GlobalQueue._dispose(c as Computed<unknown>, false, true);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
optimisticSignal,
1616
read,
1717
runWithOwner,
18+
setMemo,
1819
setSignal,
1920
signal,
2021
trackedEffect,
@@ -264,7 +265,7 @@ export function createSignal<T>(
264265
node._config &= ~CONFIG_AUTO_DISPOSE;
265266
return [
266267
accessor<T | undefined>(node),
267-
setSignal.bind(null, node as any) as Setter<T | undefined>
268+
setMemo.bind(null, node as any) as Setter<T | undefined>
268269
];
269270
}
270271
const node = signal<T>(first as any, second as SignalOptions<T>);

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
setSignal,
1313
signal,
1414
STORE_SNAPSHOT_PROPS,
15+
suppressComputedRecompute,
1516
trackOptimisticStore,
1617
untrack,
1718
type Computed,
@@ -727,5 +728,13 @@ export function createStore<T extends object = {}>(
727728

728729
if (__DEV__) registerGraph(wrappedStore, getOwner());
729730

730-
return [wrappedStore, (fn: (draft: T) => void): void => storeSetter(wrappedStore, fn)];
731+
return [
732+
wrappedStore,
733+
derived
734+
? (fn: (draft: T) => void): void => (
735+
storeSetter(wrappedStore, fn),
736+
suppressComputedRecompute((wrappedStore as any)[$REFRESH])
737+
)
738+
: (fn: (draft: T) => void): void => storeSetter(wrappedStore, fn)
739+
];
731740
}

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,114 @@ describe("async compute", () => {
726726
});
727727
});
728728

729+
it("should let a manual setSignal write to a memo win over a queued recompute (#2692)", () => {
730+
const [original, setOriginal, derived, setDerived] = createRoot(() => {
731+
const [o, setO] = createSignal(0);
732+
const [d, setD] = createSignal(o);
733+
return [o, setO, d, setD] as const;
734+
});
735+
expect(derived()).toBe(0);
736+
setOriginal(1);
737+
setDerived(2);
738+
flush();
739+
expect(original()).toBe(1);
740+
expect(derived()).toBe(2);
741+
// Subsequent change to upstream still recomputes the memo
742+
setOriginal(3);
743+
flush();
744+
expect(derived()).toBe(3);
745+
});
746+
747+
it("should let a manual setSignal write win even when an intermediate memo re-marks it (#2692)", () => {
748+
let observed: number | undefined;
749+
const [setA, setC] = createRoot(() => {
750+
const [a, sA] = createSignal(0);
751+
const b = createMemo(() => a() * 10);
752+
const [c, sC] = createSignal(b);
753+
createRenderEffect(c, v => {
754+
observed = v;
755+
});
756+
return [sA, sC] as const;
757+
});
758+
flush();
759+
expect(observed).toBe(0);
760+
setA(1);
761+
setC(99);
762+
flush();
763+
expect(observed).toBe(99);
764+
setA(2);
765+
flush();
766+
expect(observed).toBe(20);
767+
});
768+
769+
it("write trumps recompute regardless of order within a tick (#2692)", () => {
770+
const [setOriginal, setDerived, derived] = createRoot(() => {
771+
const [original, setO] = createSignal(0);
772+
const [d, setD] = createSignal(original);
773+
return [setO, setD, d] as const;
774+
});
775+
setDerived(2);
776+
setOriginal(1);
777+
flush();
778+
expect(derived()).toBe(2);
779+
});
780+
781+
it("latest manual write wins on a memo within a tick (#2692)", () => {
782+
const [setOriginal, setDerived, derived] = createRoot(() => {
783+
const [original, setO] = createSignal(0);
784+
const [d, setD] = createSignal(original);
785+
return [setO, setD, d] as const;
786+
});
787+
setOriginal(1);
788+
setDerived(2);
789+
setDerived(3);
790+
flush();
791+
expect(derived()).toBe(3);
792+
});
793+
794+
it("same-value manual write still trumps recompute within a tick (#2692)", () => {
795+
const [setOriginal, setDerived, derived] = createRoot(() => {
796+
const [original, setO] = createSignal(0);
797+
const [d, setD] = createSignal(original);
798+
return [setO, setD, d] as const;
799+
});
800+
setOriginal(1);
801+
setDerived(0);
802+
flush();
803+
expect(derived()).toBe(0);
804+
});
805+
806+
it("refresh() forces a recompute even after a manual memo write in the same tick (#2692)", () => {
807+
const [setOriginal, setDerived, derived] = createRoot(() => {
808+
const [original, setO] = createSignal(0);
809+
const [d, setD] = createSignal(original);
810+
return [setO, setD, d] as const;
811+
});
812+
setOriginal(1);
813+
setDerived(2);
814+
refresh(derived);
815+
flush();
816+
expect(derived()).toBe(1);
817+
});
818+
819+
it("should propagate manual setSignal write to subscribers when upstream also changed (#2692)", () => {
820+
let observed: number | undefined;
821+
const [setOriginal, setDerived] = createRoot(() => {
822+
const [original, setO] = createSignal(0);
823+
const [derived, setD] = createSignal(original);
824+
createRenderEffect(derived, v => {
825+
observed = v;
826+
});
827+
return [setO, setD] as const;
828+
});
829+
flush();
830+
expect(observed).toBe(0);
831+
setOriginal(1);
832+
setDerived(2);
833+
flush();
834+
expect(observed).toBe(2);
835+
});
836+
729837
it("should allow opting into chained refresh via isRefreshing()", () => {
730838
let depRuns = 0;
731839
let targetRuns = 0;

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,3 +1193,51 @@ describe("arrays", () => {
11931193
expect(effectC).toHaveBeenCalledTimes(2);
11941194
});
11951195
});
1196+
1197+
describe("derived store manual writes", () => {
1198+
it("manual setStore on a derived store wins over a queued recompute (#2692)", () => {
1199+
const [setSource, store, setStore] = createRoot(() => {
1200+
const [source, sSource] = createSignal({ count: 0 });
1201+
const [s, sStore] = createStore<{ count: number }>(() => ({ count: source().count }), {
1202+
count: 0
1203+
});
1204+
return [sSource, s, sStore] as const;
1205+
});
1206+
flush();
1207+
expect(store.count).toBe(0);
1208+
setSource({ count: 1 });
1209+
setStore(s => {
1210+
s.count = 99;
1211+
});
1212+
flush();
1213+
expect(store.count).toBe(99);
1214+
setSource({ count: 2 });
1215+
flush();
1216+
expect(store.count).toBe(2);
1217+
});
1218+
1219+
it("same-value setStore on a derived store keeps the override for the tick (#2692)", () => {
1220+
const [setSource, store, setStore] = createRoot(() => {
1221+
const [source, sSource] = createSignal({ count: 0 });
1222+
const [s, sStore] = createStore<{ count: number }>(() => ({ count: source().count }), {
1223+
count: 0
1224+
});
1225+
return [sSource, s, sStore] as const;
1226+
});
1227+
flush();
1228+
setStore(s => {
1229+
s.count = 99;
1230+
});
1231+
flush();
1232+
expect(store.count).toBe(99);
1233+
setSource({ count: 1 });
1234+
setStore(s => {
1235+
s.count = 99;
1236+
});
1237+
flush();
1238+
expect(store.count).toBe(99);
1239+
setSource({ count: 2 });
1240+
flush();
1241+
expect(store.count).toBe(2);
1242+
});
1243+
});

0 commit comments

Comments
 (0)