Skip to content

Commit 404d06a

Browse files
ryansolidclaude
andauthored
fix(signals): a re-pass equal to a staged first value is not a change (#4540) (#4555)
* 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 * fix(signals): fold the #4540 staged check into the existing staged local The first cut read `_pendingValue` again at the top of recompute and cost +8 B minified on every core row. The equality gate already reads it for compareValue and the effect gate reads it a third time; one `unstaged` local now serves all three, and the uninitialized term checks it at the gate. Core rows are back to +0 B minified, and the gate does one fewer property load per pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(signals): order the #4540 gate term for size `unstaged && wasUninitialized && !isEffect`, with wasUninitialized kept as the raw status bit, minifies 4 B smaller than the boolean-coerced form and keeps the brotli rows under their caps. The `!!` moves to the observe-only devChanged assignment, which needs a boolean. Same logic. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent 718ec9c commit 404d06a

3 files changed

Lines changed: 86 additions & 10 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: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ 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+
const wasUninitialized = el._statusFlags & STATUS_UNINITIALIZED;
524524
// Capture both error and pending status before the compute clears them.
525525
// A conditional can drop its pending source and recover to an unchanged
526526
// value, leaving blocked dependents outside that source’s settle walk.
@@ -790,27 +790,30 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
790790
// publishes to — the staged `_pendingValue` when staged; a lane node's
791791
// lane value (a node entering the lane this pass has none yet — its
792792
// committed value, `laneStage` having voided a frame staging).
793-
const compareValue =
794-
el._pendingValue !== NOT_PENDING
795-
? el._pendingValue
796-
: el._config & CONFIG_OVERRIDE && el._x!._lane !== NOT_PENDING
797-
? el._x!._lane
798-
: el._value;
793+
const unstaged = el._pendingValue === NOT_PENDING;
794+
const compareValue = !unstaged
795+
? el._pendingValue
796+
: el._config & CONFIG_OVERRIDE && el._x!._lane !== NOT_PENDING
797+
? el._x!._lane
798+
: el._value;
799799
// A throwing comparator is contained inside `isSame` (A3, #2837; #4111).
800800
// A pass replacing an error this node staged is a new answer, even the
801801
// committed value (D-41: a recovery equal to the last good value is a
802802
// change): the slot it publishes to held the error, and the readers that
803803
// derived from it — `latest()` throws it — re-derive (#4403).
804+
// A first value already staged this frame is that slot too: a re-pass
805+
// equal to it changes nothing, though the node stays uninitialized until
806+
// the commit (#4540).
804807
const valueChanged =
805-
(!isEffect && wasUninitialized) ||
808+
(unstaged && wasUninitialized && !isEffect) ||
806809
!!el._x?._shownError ||
807810
!!replaced ||
808811
!isSame(el, compareValue, value);
809812

810813
// A committed derived change becomes a cause for this node's subscribers,
811814
// chaining their attribution through this node to the root write.
812815
if (__OBSERVE__ && attrHooks !== null) {
813-
devChanged = valueChanged && !el._x?._error;
816+
devChanged = !!valueChanged && !el._x?._error;
814817
if (devChanged && !isEffect && !create) attrHooks.derivedChanged(el);
815818
}
816819

@@ -830,7 +833,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
830833
Object.is((el as any)._prevValue, value)
831834
) &&
832835
// A re-staging pass owes no run: the commit replays the effect (#3802).
833-
el._pendingValue === NOT_PENDING
836+
unstaged
834837
) {
835838
(el as any)._modified = !el._x?._error;
836839
// Reuse one bound runner per effect — runEffect no-ops on a stale
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)