Skip to content

Commit 4fa4146

Browse files
committed
fix(signals): a re-pass equal to a staged first value is not a change (#4540)
A node whose first value is staged in the running flush stays STATUS_UNINITIALIZED until the commit, and recompute counted every re-pass of it as a change. A non-keyed Show whose condition first answers in a flush (an async memo or projection landing) re-ran its value memo on each re-pass and remounted its child; a child writing the source from its render effect's synchronous first run looped. The uninitialized test now applies only when nothing is staged: with a staged first value, the equality gate compares against it (INV-11), as for any staged node. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 215c365 commit 4fa4146

3 files changed

Lines changed: 79 additions & 2 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 re-pass equal to a first value already staged this flush is not a change, so a non-keyed `Show` over an async condition no longer remounts its child when the child writes the source on mount (#4540)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,11 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
520520
if (__DEV__) clearSignals(el);
521521
}
522522

523-
const wasUninitialized = !!(el._statusFlags & STATUS_UNINITIALIZED);
523+
// #4540: a first value already staged this frame is the slot this pass
524+
// publishes to (INV-11): a re-pass equal to it changes nothing, though the
525+
// node stays uninitialized until the commit.
526+
const wasUninitialized =
527+
el._statusFlags & STATUS_UNINITIALIZED && el._pendingValue === NOT_PENDING;
524528
// Capture both error and pending status before the compute clears them.
525529
// A conditional can drop its pending source and recover to an unchanged
526530
// value, leaving blocked dependents outside that source’s settle walk.
@@ -802,7 +806,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
802806
// change): the slot it publishes to held the error, and the readers that
803807
// derived from it — `latest()` throws it — re-derive (#4403).
804808
const valueChanged =
805-
(!isEffect && wasUninitialized) ||
809+
(!isEffect && !!wasUninitialized) ||
806810
!!el._x?._shownError ||
807811
!!replaced ||
808812
!isSame(el, compareValue, value);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
// #4540: a non-keyed Show whose condition reads an async memo or projection
6+
// remounted its child on every write to the memo's source, though the
7+
// condition stayed true. A child that writes the source on mount looped.
8+
import { expect, test } from "vitest";
9+
import { createMemo, createProjection, createRenderEffect, createSignal, Show } from "solid-js";
10+
import { render } from "../src/index.js";
11+
12+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
13+
14+
type Kind = "sync memo" | "sync projection" | "async memo" | "async projection";
15+
16+
function mount(kind: Kind) {
17+
let mounts = 0;
18+
const [version, setVersion] = createSignal(0, { ownedWrite: true });
19+
let value: number | undefined = kind.startsWith("sync") ? 1 : undefined;
20+
const firstAnswer = wait(10).then(() => {
21+
value = 1;
22+
setVersion(v => v + 1);
23+
return 1;
24+
});
25+
let read: () => number | undefined;
26+
if (kind.endsWith("memo")) {
27+
const m = createMemo(() => (version(), value ?? firstAnswer));
28+
read = () => m();
29+
} else {
30+
const p = createProjection<{ data?: number }>(() => {
31+
version();
32+
return (
33+
value !== undefined ? { data: value } : firstAnswer.then(v => ({ data: v }))
34+
) as never;
35+
}, {});
36+
read = () => p.data;
37+
}
38+
function Child() {
39+
mounts++;
40+
if (mounts > 20) throw new Error("remount loop");
41+
createRenderEffect(
42+
() => 0,
43+
() => void setVersion(v => v + 1)
44+
);
45+
return <p>child</p>;
46+
}
47+
const el = document.createElement("div");
48+
const dispose = render(
49+
() => (
50+
<Show when={read() !== undefined}>
51+
<Child />
52+
</Show>
53+
),
54+
el
55+
);
56+
return { mounts: () => mounts, dispose, el };
57+
}
58+
59+
test.each<Kind>(["sync memo", "sync projection", "async memo", "async projection"])(
60+
"%s: a non-keyed Show whose condition stays true mounts its child once (#4540)",
61+
async kind => {
62+
const { mounts, dispose, el } = mount(kind);
63+
await wait(100);
64+
expect(el.innerHTML).toBe("<p>child</p>");
65+
dispose();
66+
expect(mounts()).toBe(1);
67+
}
68+
);

0 commit comments

Comments
 (0)