Skip to content

Commit fa12ca7

Browse files
committed
fix(signals): a deferred effect's held mount keeps its first value as prev (#4556)
#4547 made a deferred effect born held drop its staged first value, so the commit has nothing to replay. Dropping it also lost the value: the effect's next pass got `prev` undefined where a plain mount gets the first value. The staging now applies at creation instead (the commit's own `applyStaged`, shared with commitPendingNode), so the commit still replays nothing and the next pass sees the same `prev` as on a plain mount (D-13: a held mount lands as if it had mounted then). Size: the helper is shared with commitPendingNode's two staged-value sites; two equivalent shorter forms in the scheduler (`lane ?? this` in enqueue, `includes` in liveTx) keep the floors at or under next. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 62bf4c4 commit fa12ca7

4 files changed

Lines changed: 86 additions & 12 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(signals): a deferred effect mounted by a held update keeps its first value as the next pass's `prev` (#4556)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import {
4242
setTrackedQueueCallback,
4343
setEffectCallback,
4444
heldRunHook,
45-
queueEvent
45+
queueEvent,
46+
applyStaged
4647
} from "./scheduler.js";
4748
import type { Computed, NodeOptions, Owner } from "./types.js";
4849

@@ -80,9 +81,11 @@ export function effect<T>(
8081
// creation's (A29). A lane's node likewise: its run is the lane's, queued
8182
// by the pass and released at the reveal (lanes.ts).
8283
// `defer` skips the first run, the commit's replay of a node born staged
83-
// included (#4543): it drops the staging, so the commit replays nothing.
84+
// included (#4543): its staged first value applies now, so the commit
85+
// replays nothing and the next pass gets it as `prev`, as on a plain mount
86+
// (#4556; D-13).
8487
options?.defer
85-
? (node._pendingValue = NOT_PENDING)
88+
? applyStaged(node)
8689
: node._pendingValue === NOT_PENDING &&
8790
!node._x?._transaction?._lane &&
8891
(node._type === EFFECT_USER || options?.schedule

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export function txOf(n: Signal<any> | Computed<any>): Transaction {
385385
* it has landed. */
386386
export function liveTx(t: Transaction): Transaction | null {
387387
t = resolveTx(t);
388-
return transactions.indexOf(t) !== -1 ? t : null;
388+
return transactions.includes(t) ? t : null;
389389
}
390390
export function resolveTx(t: Transaction): Transaction {
391391
while (t._into !== null) t = t._into;
@@ -1144,7 +1144,7 @@ export class GlobalQueue implements IQueue {
11441144
// the frame (which may park). A lane pass that read the frame's own
11451145
// staging is the frame's (`recompute` passes null): its run waits with
11461146
// the frame.
1147-
if (type) (lane !== null ? lane : this)._queues[type - 1].push(fn);
1147+
if (type) (lane ?? this)._queues[type - 1].push(fn);
11481148
schedule();
11491149
}
11501150
/** Status reaching the root: pending is absorbed (the root mount defers —
@@ -1403,6 +1403,14 @@ export function sameLane(a: Transaction, b: Transaction): boolean {
14031403
return a === b || (a._links !== null && a._links === b._links);
14041404
}
14051405

1406+
/** A staged value becomes the node's value: truthy when one was staged. */
1407+
export function applyStaged(n: Signal<any>): unknown {
1408+
return (
1409+
n._pendingValue !== NOT_PENDING &&
1410+
((n._value = n._pendingValue), (n._pendingValue = NOT_PENDING))
1411+
);
1412+
}
1413+
14061414
export function commitPendingNode(n: Signal<any>): void {
14071415
const c = n as Partial<Computed<unknown>>;
14081416
// L2: the commit is where a pass, held or not, stops being uncommitted —
@@ -1412,10 +1420,7 @@ export function commitPendingNode(n: Signal<any>): void {
14121420
// commits a value.
14131421
if (!c._fn) {
14141422
n._config &= ~(CONFIG_STAGED | CONFIG_HELD);
1415-
if (n._pendingValue !== NOT_PENDING) {
1416-
n._value = n._pendingValue as any;
1417-
n._pendingValue = NOT_PENDING;
1418-
}
1423+
applyStaged(n);
14191424
return;
14201425
}
14211426
// Computeds only from here (`_statusFlags` is not a signal field). A
@@ -1444,9 +1449,7 @@ export function commitPendingNode(n: Signal<any>): void {
14441449
n._pendingValue = NOT_PENDING;
14451450
n._time = clock + 1;
14461451
notifyStatus(c as Computed<unknown>, STATUS_ERROR, e);
1447-
} else if (n._pendingValue !== NOT_PENDING) {
1448-
n._value = n._pendingValue as any;
1449-
n._pendingValue = NOT_PENDING;
1452+
} else if (applyStaged(n)) {
14501453
// A node born staged (recompute) initializes at this commit.
14511454
c._statusFlags! &= ~STATUS_UNINITIALIZED;
14521455
// A quiet re-ask's classification survives its landing and dies with
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
import { expect, it } from "vitest";
6+
import { createEffect, createMemo, createSignal, flush, Loading, Show } from "solid-js";
7+
import { render } from "@solidjs/web";
8+
9+
// #4556: a held mount lands at its commit as if it had mounted then (D-13,
10+
// D-3). A deferred effect skips its first run (#4543), but its first value is
11+
// still the `prev` its next pass receives, as on a plain mount.
12+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
13+
14+
async function mountChild(held: boolean) {
15+
const prevs: unknown[] = [];
16+
const runs: unknown[] = [];
17+
const [path, setPath] = createSignal("a");
18+
const matched = createMemo(() => {
19+
const p = path();
20+
return p === "a" || !held ? p : wait(10).then(() => p);
21+
});
22+
let bump!: (v: number) => void;
23+
function Child() {
24+
const [local, setLocal] = createSignal(1);
25+
createEffect(
26+
(prev?: number) => (prevs.push(prev), local()),
27+
(v, prev) => void runs.push([v, prev]),
28+
{ defer: true }
29+
);
30+
bump = setLocal;
31+
return <p>B</p>;
32+
}
33+
const el = document.createElement("div");
34+
const dispose = render(
35+
() => (
36+
<Loading fallback="root">
37+
<Show when={matched() === "b"} fallback={<p>A</p>}>
38+
<Child />
39+
</Show>
40+
</Loading>
41+
),
42+
el
43+
);
44+
setPath("b");
45+
flush();
46+
await wait(80);
47+
flush();
48+
const text = el.textContent;
49+
bump(2);
50+
flush();
51+
dispose();
52+
// The last compute's `prev`: earlier passes differ between the paths
53+
// (a held mount's pass re-runs at the hold), the pass after the write does not.
54+
return { text, lastPrev: prevs[prevs.length - 1], runs };
55+
}
56+
57+
it("control: a deferred effect's next pass sees its first value on a plain mount", async () => {
58+
expect(await mountChild(false)).toEqual({ text: "B", lastPrev: 1, runs: [[2, undefined]] });
59+
});
60+
61+
it("a deferred effect's next pass sees its first value on a held mount (#4556)", async () => {
62+
expect(await mountChild(true)).toEqual({ text: "B", lastPrev: 1, runs: [[2, undefined]] });
63+
});

0 commit comments

Comments
 (0)