Skip to content

Commit 9f30beb

Browse files
committed
fix(signals): a verdict-mounted Loading holds a nested render effect's first read (#4166)
#3851's born-held clause for a verdict lane's mount keyed on `prevLane`, the lane of the pass that created the node. Only the mount's own first pass sees it: its seat is mainline, so a first pass it creates in turn (the boundary's content computation, a component's memo, the `<p>{n()}` binding) sees `prevLane === null`, publishes its staged read, and the boundary shows held content beside the committed value. `recompute` now carries the chain's root lane (`laneRoot`) through nested first passes and restores it in its `finally`; the staged-read born-held test, folded into `joined`, and the boundary hand-off read it instead of `prevLane`. The state is lane-engine only (`laneStageHook`), like the seat, so bundles without the engine fold it away with the old `prevLane` arms: -30 B minified on the core and compiled floors. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 71153bf commit 9f30beb

4 files changed

Lines changed: 176 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+
A `<Loading>` that a verdict reader (`<Show when={latest(x) > 0}>`) mounts during a held action now holds a render effect nested in its content, not only a direct read: it shows its fallback beside the committed value until the action lands (#4166).

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ export let stale = false;
232232
export let context: Owner | null = null;
233233

234234
export let snapshotCaptureActive = false;
235+
/** The lane at the root of the running chain of first passes: the lane whose
236+
* pass created the chain's first node (a mount), inherited by every first
237+
* pass nested under it, whose seats are mainline (`recompute`, #4166). */
238+
let laneRoot: Transaction | null | false | undefined;
235239
export let snapshotSources: Set<any> | null = null;
236240

237241
function ownerInSnapshotScope(owner: Owner | null): boolean {
@@ -317,6 +321,11 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
317321
// Lanes exist only with the lane engine installed (`laneStageHook`): a
318322
// bundle without it seats every pass on mainline, and folds the seat away.
319323
const prevLane = passLane;
324+
// The mount a first pass belongs to: its creator's lane, or the one its
325+
// creator's own mount came from — a render effect a verdict lane's mount's
326+
// content creates is that mount's too (#4166). Lanes only, like the seat.
327+
const prevMount = laneRoot;
328+
const mount = laneStageHook && (laneRoot = create && (prevLane || prevMount));
320329
setPassLane(
321330
!laneStageHook
322331
? null
@@ -455,7 +464,8 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
455464
: el._value;
456465
let oldHeight = el._height;
457466
let missedWake = false;
458-
// L2: did this pass read the future (REACTIVE_JOINED, set by read)?
467+
// L2: did this pass read the future (REACTIVE_JOINED, set by read) — or,
468+
// a lane's mount, a staging of this flush (#3851, #4166)?
459469
let joined = false;
460470
let prevTracking = tracking;
461471
let prevStrictRead: string | false = false;
@@ -531,6 +541,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
531541
}
532542
} finally {
533543
tracking = prevTracking;
544+
if (laneStageHook) laneRoot = prevMount;
534545
if (__DEV__) strictRead = prevStrictRead;
535546
if (isStaleEffect) stale = prevStale;
536547
// Consume the missed-wake latch (#3037, set by insertSubs): a dep write
@@ -539,7 +550,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
539550
// (markNode(c) in read()) marks the running node as part of ordinary
540551
// bookkeeping, and those marks are correctly discarded here.
541552
missedWake = (el._flags & REACTIVE_MISSED_WAKE) !== 0;
542-
joined = (el._flags & REACTIVE_JOINED) !== 0;
553+
joined = !!(el._flags & (mount ? REACTIVE_JOINED | REACTIVE_STAGED_READ : REACTIVE_JOINED));
543554
// A verdict reader is one for as long as it probes, or reads a memo
544555
// that does (`read`): a pass that did neither (REACTIVE_PROBED, the same
545556
// bit) is an ordinary derivation again.
@@ -719,9 +730,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
719730
create
720731
? !(
721732
(flushTransaction !== null || passTx !== null) &&
722-
(joined ||
723-
(prevLane && el._flags & REACTIVE_STAGED_READ) ||
724-
(creatorPass(oldcontext)?._flags as number) & REACTIVE_JOINED)
733+
(joined || (creatorPass(oldcontext)?._flags as number) & REACTIVE_JOINED)
725734
)
726735
: isEffect && el._pendingValue === NOT_PENDING
727736
) {
@@ -755,7 +764,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
755764
// #3851): the mount is part of the hold, and the boundary appears
756765
// at its commit, its fallback never seen (D-13, U-3) — a `<Show>`
757766
// mounting it over a landing included.
758-
freshHook?.(el, !prevLane && flushTransaction);
767+
freshHook?.(el, !mount && flushTransaction);
759768
}
760769
if (__DEV__) devTrackHeldPending(el);
761770
}
@@ -799,12 +808,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
799808
// hold's commit (D-12) — but the hold does not wait for it: its only
800809
// readers are behind the fallback (`_hidden`; F-4, #3540). A boundary
801810
// re-armed by `on` owns what it collects outright (D-14): not held.
802-
if (
803-
create &&
804-
lane === null &&
805-
(joined || (prevLane && el._flags & REACTIVE_STAGED_READ)) &&
806-
el._statusFlags & STATUS_PENDING
807-
) {
811+
if (create && lane === null && joined && el._statusFlags & STATUS_PENDING) {
808812
const t = flushTransaction ?? passTx;
809813
if (t && !t._lane && freshHook?.(el) !== CAUGHT_REARMED) holdNode(el, t);
810814
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* #4166: #3851 for a nested render effect. An action holds `x = 1`;
3+
* `<Show when={latest(x) > 0}>` mounts a `<Loading>` whose content creates
4+
* a DOM render effect reading `x` (`<p>{x()}</p>`). The mount is mainline,
5+
* so that effect's first read is held like a direct read: the boundary
6+
* shows its fallback and the effect publishes nothing until the action
7+
* lands. Before, the effect published `1` beside the committed `x = 0`.
8+
*/
9+
import { describe, expect, it } from "vitest";
10+
import {
11+
action,
12+
createLoadingBoundary,
13+
createMemo,
14+
createRenderEffect,
15+
createRoot,
16+
createSignal,
17+
flush,
18+
latest,
19+
untrack
20+
} from "../src/index.js";
21+
22+
const settle = async () => {
23+
for (let i = 0; i < 3; i++) {
24+
await new Promise(r => setTimeout(r, 0));
25+
flush();
26+
}
27+
};
28+
29+
async function mountUnderVerdict(contentInMemo: boolean) {
30+
const [x, setX] = createSignal(0);
31+
let screenX: unknown;
32+
let inner: unknown;
33+
let slot: unknown;
34+
createRoot(() => {
35+
// <i>{x()}</i>
36+
createRenderEffect(x, v => {
37+
screenX = v;
38+
});
39+
// <Show when={latest(x) > 0}>
40+
// <Loading fallback="pending"><p>{x()}</p></Loading>
41+
// </Show>
42+
const p = () => {
43+
createRenderEffect(x, v => {
44+
inner = v;
45+
});
46+
return "p";
47+
};
48+
// A component that returns a memo: the render effect is created two
49+
// creation passes below the verdict pass that mounted the boundary.
50+
const content = contentInMemo ? () => createMemo(p)() : p;
51+
const when = createMemo(() => latest(x) > 0);
52+
const condition = createMemo(() => when());
53+
const children = createMemo(() =>
54+
condition() ? untrack(() => createLoadingBoundary(content, () => "pending")) : "closed"
55+
);
56+
createRenderEffect(
57+
() => {
58+
const c = children();
59+
return typeof c === "function" ? c() : c;
60+
},
61+
v => {
62+
slot = v;
63+
}
64+
);
65+
});
66+
flush();
67+
const screens: unknown[] = [[screenX, slot, inner]];
68+
69+
let release!: () => void;
70+
const done = action(function* () {
71+
setX(1);
72+
yield new Promise<void>(r => (release = r));
73+
})();
74+
await settle();
75+
screens.push([screenX, slot, inner]);
76+
77+
release();
78+
await done;
79+
await settle();
80+
screens.push([screenX, slot, inner]);
81+
return screens;
82+
}
83+
84+
describe("#4166: a verdict-mounted boundary holds a nested render effect's first read", () => {
85+
it("the boundary shows its fallback while the action holds the value", async () => {
86+
expect(await mountUnderVerdict(false)).toEqual([
87+
[0, "closed", undefined],
88+
[0, "pending", undefined],
89+
[1, "p", 1]
90+
]);
91+
});
92+
93+
it("same with the render effect inside a memo the content creates", async () => {
94+
expect(await mountUnderVerdict(true)).toEqual([
95+
[0, "closed", undefined],
96+
[0, "pending", undefined],
97+
[1, "p", 1]
98+
]);
99+
});
100+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*
5+
* #4166: a `<Loading>` that `<Show when={latest(n) > 0}>` mounts during a
6+
* held action holds its content's text binding like a direct read (#3851):
7+
* the boundary shows its fallback beside the committed `n` until the action
8+
* lands.
9+
*/
10+
import { expect, test } from "vitest";
11+
import { Loading, Show, action, createSignal, flush, latest } from "solid-js";
12+
import { render } from "../src/index.js";
13+
14+
async function settle() {
15+
for (let i = 0; i < 3; i++) {
16+
await new Promise(r => setTimeout(r, 0));
17+
flush();
18+
}
19+
}
20+
21+
test("a verdict-mounted Loading holds its content's binding until the action lands (#4166)", async () => {
22+
const root = document.createElement("div");
23+
const [n, setN] = createSignal(0);
24+
let release!: () => void;
25+
const save = action(function* () {
26+
setN(1);
27+
yield new Promise<void>(r => (release = r));
28+
});
29+
30+
const dispose = render(
31+
() => (
32+
<>
33+
<i>{n()}</i>
34+
<Show when={latest(n) > 0}>
35+
<Loading fallback="pending">
36+
<p>{n()}</p>
37+
</Loading>
38+
</Show>
39+
</>
40+
),
41+
root
42+
);
43+
flush();
44+
expect(root.innerHTML).toBe("<i>0</i>");
45+
46+
const done = save();
47+
await settle();
48+
expect(root.innerHTML).toBe("<i>0</i>pending");
49+
50+
release();
51+
await done;
52+
await settle();
53+
expect(root.innerHTML).toBe("<i>1</i><p>1</p>");
54+
dispose();
55+
});

0 commit comments

Comments
 (0)