Skip to content

Commit 5600428

Browse files
committed
fix(signals): a memo of latest() computes once on a plain write; a Loading fallback behind an error keeps the error up
- A transaction only a `latest()` read opened holds nothing: a verdict reader's plain read of a staging in it, from the frame's seat, is the frame's read, watched by the seam, which re-derives it once only if the flush parks (D-22, F-10). So `[s(), L()]` with `L = createMemo(() => latest(s))` computes once per write, as the inline form does (F-6), and the inline form beside such a memo does too. Lane work still sees the screen (D-37). - A Loading boundary catching under an error boundary that shows its fallback records the reader on the error boundary too: the inner fallback is hidden behind the error, which stays until the re-ask answers (D-39) — a body read now shows `[E]` like an effect read. - Pins: runs-once over inline, memo, inline beside a memo, latest(memo) × memo, render effect, user effect (two writes); JSX inline/memo × attributes/template; the D-39 body read. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent ba5c167 commit 5600428

8 files changed

Lines changed: 168 additions & 71 deletions

File tree

‎packages/signals/src/boundaries.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ function caught(b: Boundary, node: Computed<any>, error: unknown, source = node)
199199
reportClientError(caught, b._owner, source);
200200
}
201201
record(b, node, source);
202+
// A loading fallback behind an error fallback is hidden: the error stays
203+
// the display until what the fallback waits on answers (D-39) — the
204+
// error boundary waits on it too.
205+
if (b._type === STATUS_PENDING)
206+
for (let p = b._parent; p; p = p._parent)
207+
if (p._type === STATUS_ERROR && p._fallback) record(p, node);
202208
if (b._armed) flip(b);
203209
// Already showing the fallback: one more reader to wait on changes
204210
// nothing on screen. The fallback is instantiated once per show — a later

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import {
126126
passTx,
127127
joinPassTx,
128128
stagedReaders,
129+
latestOpened,
129130
staleReader,
130131
laneDirty,
131132
externalUntrackHook,
@@ -2189,6 +2190,17 @@ export function stagedRead(c: Computed<any>, el?: Signal<any> | Computed<any>):
21892190
* (`bornInto`, #4010): its branch exists only there. */
21902191
export function stagedScreen(c: Computed<any>): boolean {
21912192
if (flushTransaction === null || bornInto(c, flushTransaction)) return false;
2193+
// A transaction only a `latest()` read opened holds nothing: the staging
2194+
// is the frame's read for a pass in the frame's seat, as it is before the
2195+
// frame has one, and the seam re-derives the pass once only if the flush
2196+
// parks (D-22, F-10). Lane work sees the screen (D-37).
2197+
if (
2198+
observeFlightHook &&
2199+
flushTransaction === latestOpened &&
2200+
passLane === null &&
2201+
c._config & CONFIG_VERDICT
2202+
)
2203+
return !stagedReaders.push(c);
21922204
staleReader(c, flushTransaction);
21932205
return true;
21942206
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ export function setPassLane(l: Transaction | null): void {
326326
* schedules the seam that consumes it: membership is the tick's — the same
327327
* synchronous frame — and the next tick's writes are not this one's. */
328328
export let flushTransaction: Transaction | null = null;
329+
/** The flush's transaction while only a `latest()` read of a staging opened
330+
* it (verdict.ts `provisionalVerdict`): it holds nothing, and a verdict
331+
* reader's plain read of a staging in it is the frame's read (D-22). */
332+
export let latestOpened: Transaction | null = null;
333+
export function openedByLatest(): void {
334+
latestOpened = flushTransaction;
335+
}
329336
/** The transaction the passes running OUTSIDE a flush this tick derive from
330337
* (a mainline mount, a mainline re-pass): the passes' own (A29, creation-
331338
* time form — "the entry is the pass's alone"): their results are staged

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
globalQueue,
5858
GlobalQueue,
5959
joinFuture,
60+
openedByLatest,
6061
passLane,
6162
queuePendingNode,
6263
setPassLane,
@@ -177,8 +178,10 @@ function provisionalVerdict(c: Computed<any>, notFinal: boolean): void {
177178
// (`observeFlight`), which the seam must not take as this probe's answer.
178179
const t = flushTransaction;
179180
if (probing && !t) return watchVerdict(c, notFinal);
180-
if (!probing) joinFuture(null);
181-
else if (notFinal) probeFound = true;
181+
if (!probing) {
182+
joinFuture(null);
183+
if (!t) openedByLatest();
184+
} else if (notFinal) probeFound = true;
182185
verdictRead(c, flushTransaction!);
183186
// A `latest` reader watches only a staged read made before the frame had
184187
// a transaction: one made in it (an action's world) is the frame's, and

‎packages/signals/tests/latest-memo-inline-parity.test.ts‎

Lines changed: 96 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -154,42 +154,53 @@ describe("a plain write beside an async memo: the screen beside latest(), inline
154154
t.dispose();
155155
});
156156

157-
// A plain synchronous write (no action, no async) commits at its seam: the
158-
// pass that read `s` before the frame had a transaction and then
159-
// `latest(s)` stood — one compute, one effect run — whether the reader is
160-
// a memo, a render effect or a user effect, of `latest(s)` or of
161-
// `latest(memo)`. The seam re-derives such a reader only if the frame
162-
// parks (D-22: `latest()` of a staging not yet held is the frame's read,
163-
// opens no hold, and re-derives once only if the flush parks; F-10).
164-
for (const reader of ["memo", "render effect", "user effect", "render effect of latest(memo)"])
165-
it(`inline [s(), latest] in a flush that commits runs once: ${reader}`, () => {
166-
let set!: (v: number) => void;
167-
let passes = 0;
168-
const seen: string[] = [];
169-
const dispose = createRoot(d => {
170-
const [s, _set] = createSignal(0);
171-
set = _set;
172-
const m = createMemo(() => s());
173-
const f =
174-
reader === "render effect of latest(memo)"
175-
? () => (passes++, `v:${s()} l:${latest(m)}`)
176-
: () => (passes++, `v:${s()} l:${latest(s)}`);
177-
if (reader === "memo") createRenderEffect(createMemo(f), v => void seen.push(v));
178-
else
179-
(reader === "user effect" ? createEffect : createRenderEffect)(
180-
f,
181-
(v: string) => void seen.push(v)
182-
);
183-
return d;
157+
// A plain synchronous write (no action, no async) commits at its seam: a
158+
// pass that read `s` and then `latest(s)` — inline, or through
159+
// `L = createMemo(() => latest(s))` — stood: one compute, one effect run
160+
// per write, whether the reader is a memo, a render effect or a user
161+
// effect, and whether or not another pass's `latest()` opened the flush's
162+
// transaction first (a memo of `latest(s)` beside it). The seam re-derives
163+
// such a reader only if the frame parks (D-22: `latest()` of a staging not
164+
// yet held is the frame's read, opens no hold, and re-derives once only if
165+
// the flush parks; F-10: one frame for every channel; F-6: the memo form
166+
// as inline).
167+
for (const via of [
168+
"inline",
169+
"memo",
170+
"inline beside a memo of latest(s)",
171+
"latest(memo)"
172+
] as const)
173+
for (const reader of ["memo", "render effect", "user effect"] as const)
174+
it(`[s(), latest] in a flush that commits runs once: ${via}, ${reader}`, () => {
175+
let set!: (v: number) => void;
176+
let passes = 0;
177+
const seen: string[] = [];
178+
const dispose = createRoot(d => {
179+
const [s, _set] = createSignal(0);
180+
set = _set;
181+
const m = createMemo(() => s());
182+
const L = createMemo(() => latest(s));
183+
if (via === "inline beside a memo of latest(s)") createRenderEffect(L, () => {});
184+
const l = via === "memo" ? L : via === "latest(memo)" ? () => latest(m) : () => latest(s);
185+
const f = () => (passes++, `v:${s()} l:${l()}`);
186+
if (reader === "memo") createRenderEffect(createMemo(f), v => void seen.push(v));
187+
else
188+
(reader === "user effect" ? createEffect : createRenderEffect)(
189+
f,
190+
(v: string) => void seen.push(v)
191+
);
192+
return d;
193+
});
194+
flush();
195+
passes = 0;
196+
set(1);
197+
flush();
198+
set(2);
199+
flush();
200+
expect(seen).toEqual(["v:0 l:0", "v:1 l:1", "v:2 l:2"]);
201+
expect(passes).toBe(2);
202+
dispose();
184203
});
185-
flush();
186-
passes = 0;
187-
set(1);
188-
flush();
189-
expect(seen).toEqual(["v:0 l:0", "v:1 l:1"]);
190-
expect(passes).toBe(1);
191-
dispose();
192-
});
193204
});
194205

195206
// A memo of a verdict read moves its async derivation into the verdict lane
@@ -499,3 +510,53 @@ describe("an error stays the display while the memo it came through re-asks", ()
499510
dispose();
500511
});
501512
});
513+
514+
// The same re-ask read in the Loading boundary's body (its tree) rather than
515+
// in a render effect: the inner fallback is hidden behind the error (D-39,
516+
// "over `Errored > Loading(never shown)` ... the inner fallback is hidden
517+
// behind the error"): `[E]` stays until `m1` lands. Before, the Loading —
518+
// never shown, still collecting — caught the re-ask and the Errored healed
519+
// onto `[L]`.
520+
it("an error stays the display while a Loading body read of the memo it came through re-asks", async () => {
521+
const flights: { v: number; res: (v: number) => void }[] = [];
522+
let set!: (v: number) => void;
523+
let view = "";
524+
const screens: string[] = [];
525+
const dispose = createRoot(d => {
526+
const [s, _set] = createSignal(1);
527+
set = _set;
528+
const m0 = createMemo(() => {
529+
const v = 1 + s();
530+
if (v % 4 === 1) throw new Error("m0:" + v);
531+
return v;
532+
});
533+
const m1 = createMemo(() => {
534+
const v = 1 + m0();
535+
return new Promise<number>(res => flights.push({ v, res }));
536+
});
537+
const out = createErrorBoundary(
538+
() =>
539+
createLoadingBoundary(
540+
() => `m1:${m1()}`,
541+
() => "[L]"
542+
),
543+
() => "[E]"
544+
);
545+
createRenderEffect(out, v => void (view = String(v)));
546+
return d;
547+
});
548+
flush();
549+
screens.push(view);
550+
set(0);
551+
await tick();
552+
screens.push(view);
553+
set(2);
554+
await tick();
555+
screens.push(view);
556+
expect(flights.map(f => f.v)).toEqual([3, 4]);
557+
flights[1].res(4);
558+
await tick();
559+
screens.push(view);
560+
expect(screens).toEqual(["[L]", "[E]", "[E]", "m1:4"]);
561+
dispose();
562+
});

‎packages/web/test/latest-memo-inline-parity.spec.tsx‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -176,31 +176,39 @@ test("isPending() of a first load in flight keeps the boundary's fallback", asyn
176176
// inline commits at its seam: one compute per write, whether the holes are
177177
// grouped attributes or one template string.
178178
describe("a plain write beside latest() in JSX computes once", () => {
179-
for (const form of ["attributes", "template string"] as const)
180-
test(form, () => {
181-
const div = document.createElement("div");
182-
let passes = 0;
183-
let set!: (v: number) => void;
184-
const dispose = render(() => {
185-
const [s, _set] = createSignal(0);
186-
set = _set;
187-
const count = <T,>(v: T) => (passes++, v);
188-
return form === "attributes" ? (
189-
<div title={count(`${s()}`)} data-l={latest(s)} />
190-
) : (
191-
<div title={count(`${s()} ${latest(s)}`)} />
179+
// D-22 (a staging not yet held is the frame's read; one re-derive only if
180+
// the flush parks), F-6 (the memo form as inline): one compute per write,
181+
// inline or through `L = createMemo(() => latest(s))`.
182+
for (const via of ["inline", "memo"] as const)
183+
for (const form of ["attributes", "template string"] as const)
184+
test(`${via}, ${form}`, () => {
185+
const div = document.createElement("div");
186+
let passes = 0;
187+
let set!: (v: number) => void;
188+
const dispose = render(() => {
189+
const [s, _set] = createSignal(0);
190+
set = _set;
191+
const L = createMemo(() => latest(s));
192+
const l = via === "memo" ? L : () => latest(s);
193+
const count = <T,>(v: T) => (passes++, v);
194+
return form === "attributes" ? (
195+
<div title={count(`${s()}`)} data-l={l()} />
196+
) : (
197+
<div title={count(`${s()} ${l()}`)} />
198+
);
199+
}, div);
200+
flush();
201+
passes = 0;
202+
set(1);
203+
flush();
204+
set(2);
205+
flush();
206+
expect(div.innerHTML).toBe(
207+
form === "attributes" ? '<div title="2" data-l="2"></div>' : '<div title="2 2"></div>'
192208
);
193-
}, div);
194-
flush();
195-
passes = 0;
196-
set(1);
197-
flush();
198-
expect(div.innerHTML).toBe(
199-
form === "attributes" ? '<div title="1" data-l="1"></div>' : '<div title="1 1"></div>'
200-
);
201-
expect(passes).toBe(1);
202-
dispose();
203-
});
209+
expect(passes).toBe(2);
210+
dispose();
211+
});
204212
});
205213

206214
// async-gen seed 91501 #1831 (E1): `<Errored><Loading><Loading>` over a leaf

‎scripts/size/floor-caps.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"page: live server components (base + live/GET + action + isPending/latest)": {
1919
"cap": "37.75 KB",
20-
"minified": 115556
20+
"minified": 115708
2121
},
2222
"server: floor (getRequestEvent + isServer)": {
2323
"cap": "1.34 KB",

0 commit comments

Comments
 (0)