Skip to content

Commit 9a213bb

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(signals): derived writes apply first, then derivations re-run (#3742)
A manual write to createSignal(fn) / createStore(fn) lands at once and never re-runs the derivation on its own; a source change, in the same flush or later, inside or outside an action, across a hold, re-runs it with the write as `prev` (the store's draft). Reverses #2692's beta.11 same-tick precedence (maintainer-approved): within a flush call order isn't observable, and for stores a partial write dropped the whole fold. Supersedes #3740's frame-scoped mask: the heap refusal, `_manualWriteTime` and the setter's deleteFromHeap / DIRTY-CHECK clear are removed; REACTIVE_MANUAL_WRITE keeps only its A34 proposal role. Refs #3733. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 3ed3810 commit 9a213bb

22 files changed

Lines changed: 611 additions & 250 deletions

‎.changeset/derived-write-mask-lasts-its-frame.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
---
5+
6+
Derived writes apply first, then derivations re-run. A manual write to a writable derived value (`createSignal(fn)`, `createStore(fn)`) lands at once; when one of its sources changes — in the same update or later, inside or outside an action, across async holds — the function re-runs and receives the write as `prev` (or as the draft for `createStore(fn)`), and decides what to keep. A write on its own never re-runs the function.
7+
8+
This reverses beta.11's same-tick precedence (#2692), where a write beat a source change in the same flush: a function that ignores `prev` now discards a write made in the same update as a source change, and a same-value write no longer holds against it. To keep a local value across source changes, carry it in the data as a flag the function honors. It also fixes #3733 (a write inside an action blocked later source changes for the whole hold, a regression since #2692) and supersedes the frame-scoped mask from #3740, whose changeset this replaces.

‎documentation/solid-2.0/02-signals-derived-ownership.md‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,28 @@ const [cached, setCached] = createSignal((prev = props.something) => prev);
110110
// setValue(...) writes like a normal signal; the compute receives prev on recompute.
111111
```
112112

113-
A write to a writable derived value (`createSignal(fn)`, `createStore(fn)`) wins over its sources' changes in the same batch. A source change in a later update re-runs the function, which receives your write as `prev` (or as the draft for `createStore(fn)`). To keep a local value across source changes, read it from `prev`. Inside an action and across async holds this works the same way: a hold changes when the result is shown, not what it is.
113+
A write to a writable derived value (`createSignal(fn)`, `createStore(fn)`) lands immediately. When one of its sources changes, in the same update or later, the function runs again and receives your write as `prev` (or as the draft for `createStore(fn)`), and it decides what to keep. A write on its own never re-runs the function.
114+
115+
To keep a local value across source changes, keep it in the data, as a flag the function honors:
116+
117+
```ts
118+
const [cards, setCards] = createStore<Card[]>(
119+
draft => {
120+
const previous = new Map(draft.map(card => [card.id, card]));
121+
return serverCards().map(remote => {
122+
const old = previous.get(remote.id);
123+
return old?.moveFailed ? { ...remote, column: old.column, moveFailed: true } : remote;
124+
});
125+
},
126+
[]
127+
);
128+
129+
const [card, setCard] = createSignal<Card>(prev => (prev?.pinned ? prev : source()));
130+
```
131+
132+
Setting `moveFailed` (or `pinned`) keeps the local value through later updates. Clearing it lets the next source change take over, even when both happen in the same update.
133+
134+
This is the same inside an action and across async holds: a hold changes when the result is shown, not what it is.
114135

115136
A held derivation is not a write: while a transaction holds a value the compute derived (an async dependent of the source is still in flight), a write from outside that transaction joins it and becomes the `prev` of the transaction's re-derivation instead of replacing it — the frame that reveals is `fn(inputs)`, never a value the writer computed against the old frame. A second write to the same value still replaces the first. The same holds for the derived `createStore` below.
116137

‎packages/signals/docs/INTERNALS-ASYNC-STATE.md‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎packages/signals/docs/INTERNALS-STORE-STATE.md‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,9 @@ implementation, deduplicated across reports.
673673
- **RUL-10 — The equality trio.** One precise rule needed spanning: no-op
674674
writes must not entangle lanes (opt R38); equal-value action writes must
675675
still register ownership and dirty downstream (lanes R17); same-value
676-
manual writes on derived stores must mask the recompute for the tick (core
677-
R31; for the write's own frame — a later frame's source change, and a
678-
hold's re-derivation, take the write as `prev`, A34 (3), #3612, #3733)
679-
despite equality-checked core signals.
676+
manual writes on derived stores stage like any write and are the fold's
677+
prior state, with no precedence over a re-run (core R31, rule B, #3733; a
678+
hold's re-derivation takes the write as `prev`, A34 (3), #3612).
680679
- **RUL-11 — SPEC'D (2026-08-17): §6d.** Sticky descendants flag ported from
681680
`STORE_DESC`; reference-skip precedes reachability; monotone by design.
682681
- **RUL-12 — Smaller rulings, each with a proposed default** (proceeding on

0 commit comments

Comments
 (0)