Skip to content

Commit edc407f

Browse files
committed
fix(signals): keep the #4536 observation in the optimistic store's hook, off the plain store path
The size job put the guard and the split-binding check on every store's family read (+202 B minified on plain createStore). They now live in optimistic.ts's `stands` hook, which only an optimistic family installs: a reader linked to the flight is not served the guesses (the contributor's guard), and a render effect pending on a flight the guess was written over (the #4525 mark, `fam.g`) is read as `pullFamily`'s own read would. `pullFamily` asks `stands` before the frame guard (the same outcomes for every reader, so the hook sees unlinked render effects) and `linkedTo` walks the derive's subscribers. The split test drops the memo-between-list-and-binding variant: holding that memo kept it pending, so `<For>` stopped showing the guess over the old answer (the #4525 web spec, `open: [#3][#4]`), which CI caught. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 5bcd2cb commit edc407f

4 files changed

Lines changed: 48 additions & 55 deletions

File tree

‎packages/signals/src/store/optimistic.ts‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
STATUS_PENDING,
3434
REACTIVE_REASK
3535
} from "../core/constants.js";
36-
import { ext, isEqual, joinPassAt, read as readNode, untrack } from "../core/core.js";
36+
import { context, ext, isEqual, joinPassAt, read as readNode, untrack } from "../core/core.js";
3737
import { laneValueOf, optimisticWrite, pendingGuessOf, take } from "../core/lanes.js";
3838
import {
3939
clock,
@@ -584,10 +584,32 @@ installOptHooks({
584584
// ordinary pending read (F-2), so a keyed store's other keys wait for their
585585
// answer, not for the guess's action; in a lane with no guess of its own,
586586
// or with no live guess left, none does (F-6).
587+
// A reader already observing the flight (linked) keeps observing it:
588+
// served the guess, a re-run (the guess's own, the seam) would drop its
589+
// link and release the hold beside a guess over the previous answer
590+
// (F-1, #4536) — the ordinary read serves its committed frame or keeps it
591+
// pending. So does a render effect pending on a flight the guess was
592+
// written over (`fam.g`, the #4525 mark; D-34: the guess does not answer
593+
// it): read here, as `pullFamily`'s own read would, since the frame's
594+
// guard there returns early for an unlinked render effect.
587595
stands: t => {
588-
const o = t._fam!._overlaid;
589-
const a = t._fam!._asked;
590-
return !!o?.size && (a !== clock + 1 || passLane !== null || o.has(t)) && a !== ~clock;
596+
const f = t._fam!;
597+
const o = f._overlaid;
598+
const a = f._asked;
599+
if (!(o?.size && (a !== clock + 1 || passLane !== null || o.has(t)) && a !== ~clock))
600+
return false;
601+
const c: any = context;
602+
const d = f.node!;
603+
if (c === null) return true;
604+
for (let s = d._subs; s !== null; s = s._nextSub) if (s._sub === c) return false;
605+
if (
606+
f.g === d._time &&
607+
c._type === EFFECT_RENDER &&
608+
c._statusFlags & STATUS_PENDING &&
609+
c._x?._pendingSources?.has(d)
610+
)
611+
readNode(d);
612+
return true;
591613
},
592614
// Mainline hold (`clock + 1`): the flush joins the action of each guess a
593615
// reader still reads at the seam; a reader that moved off it (a keyed

‎packages/signals/src/store/store.ts‎

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,32 +1836,13 @@ function serveDataKey(
18361836
return draftServe(target, wrap(v, target, key as any));
18371837
}
18381838

1839-
/** Does `c` observe `n`'s flight: among its subscribers (any pass's link — a
1840-
* kept tail included), or, with guesses written over its question (`w`),
1841-
* pending on it as part of the frame's hold? Walks `n`'s subs, not `c`'s deps: a reader of many leaves asks this
1842-
* per leaf, and the derive's subs are its few flight observers (#4536). */
1843-
function observes(c: Computed<any>, n: Signal<any>, w: unknown): boolean {
1844-
if (
1845-
w &&
1846-
c._statusFlags & STATUS_PENDING &&
1847-
c._x?._pendingSources?.has(n as Computed<any>) &&
1848-
frames(c)
1849-
)
1850-
return true;
1839+
/** Is `c` among `n`'s subscribers (any pass's link — a kept tail included)?
1840+
* `n`'s subs, not `c`'s deps: the derive's are its few flight observers. */
1841+
function linkedTo(c: Computed<any>, n: Signal<any>): boolean {
18511842
for (let s = n._subs; s !== null; s = s._nextSub) if (s._sub === c) return true;
18521843
return false;
18531844
}
18541845

1855-
/** Part of the frame's hold (scheduler.ts `blockedBy`): a render effect, or
1856-
* a derivation a pending render effect reads. A user effect is not. */
1857-
function frames(c: any): boolean {
1858-
if (c._type === EFFECT_RENDER) return true;
1859-
for (let s = c._subs; s !== null; s = s._nextSub)
1860-
if ((s._sub as any)._type === EFFECT_RENDER && s._sub._statusFlags & STATUS_PENDING)
1861-
return true;
1862-
return false;
1863-
}
1864-
18651846
/** The projection's obligation (plan sec. 31.7 Q-B): before any value is served
18661847
* through a family target, the derive — the firewall — is brought up to
18671848
* date WITHOUT the reader subscribing to it. Core's `read()` of the
@@ -1880,39 +1861,35 @@ function pullFamily(target: StoreTarget): void {
18801861
// verdict reader registered, a tracked pass suspended.
18811862
// A staged error (D-41) throws at the pull, before any leaf would link the reader.
18821863
if (fw._statusFlags & (STATUS_PENDING | STATUS_ERROR) || fw._config & CONFIG_STAGED_ERROR) {
1883-
// A render effect outside the flight's own flush is the frame, not a
1884-
// derivation: it keeps what it shows and learns of the landing from the
1885-
// leaves the landing changes (unchanged leaves say nothing — it is not a
1886-
// stale reader of the derive). Inside the flush it suspends like any.
1887-
// One that already observes the flight (linked by the pass that saw it
1888-
// go up, or waiting on it while guesses stand — the frame's hold,
1889-
// `blocked`) keeps observing it: a re-run for another reason (a guess it
1890-
// read, the seam) that dropped the link would release the hold with the
1891-
// flight still up, beside a guess over the previous answer (F-1, #4536).
1892-
// An errored derive has no landing to learn of: the error is what the
1893-
// frame shows.
18941864
// An optimistic family carrying guesses that stand in for the flight
18951865
// (A17): the guesses are the display — a reader is served the leaves
18961866
// (committed plus the guesses) and does not suspend on the derive; a
18971867
// verdict window still asks it (`isPending` is true while the flight is
18981868
// up). The family's, not the target's: the edit stands in for the one
18991869
// flight the family has — not over a new question they do not answer
1900-
// (optimistic.ts `stands`, #4336). Not to a reader observing the flight:
1901-
// the ordinary read below serves its committed frame or keeps it pending.
1870+
// (optimistic.ts `stands`, #4336). A reader observing the flight keeps
1871+
// observing it (`stands`, #4536). Judged first: the hook sees the frame's
1872+
// readers too.
1873+
if (verdict === null && fw._statusFlags & STATUS_PENDING && optHooks?.stands(target)) return;
1874+
// A render effect outside the flight's own flush is the frame, not a
1875+
// derivation: it keeps what it shows and learns of the landing from the
1876+
// leaves the landing changes (unchanged leaves say nothing — it is not a
1877+
// stale reader of the derive). Inside the flush it suspends like any.
1878+
// One that already observes the flight (linked by the pass that saw it
1879+
// go up — the frame's hold, `blocked`) keeps observing it: a re-run for
1880+
// another reason (a guess it read, the seam) that dropped the link would
1881+
// release the hold with the flight still up. An errored derive has no
1882+
// landing to learn of: the error is what the frame shows.
19021883
const c: any = context;
1903-
const s = verdict === null && fw._statusFlags & STATUS_PENDING && optHooks?.stands(target);
1904-
// Guesses written over the question in flight, which they do not answer (D-34).
1905-
const w = s && target._fam!.g === fw._time;
19061884
if (
19071885
c !== null &&
19081886
c._type === EFFECT_RENDER &&
19091887
fw._statusFlags & STATUS_PENDING &&
19101888
fw._config & CONFIG_HELD &&
19111889
flushTransaction !== txOf(fw) &&
1912-
!observes(c, fw, w)
1890+
!linkedTo(c, fw)
19131891
)
19141892
return;
1915-
if (s && !(c && observes(c, fw, w))) return;
19161893
// A pass the pull throws a staged error (D-41) is linked to the derive,
19171894
// untracked too — `handleAsync` asking a returned proxy whether it is a
19181895
// flight: the pass carries the error, and the re-ask or answer that

‎packages/signals/src/store/target.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ export interface StoreFamily {
5454
* pass reads plainly (projection.ts). */
5555
w?: number;
5656
/** An arrangement guess was written while the derive asked a new question
57-
* (optimistic.ts, #4525): the derive's `_time` then, so it lapses when that
58-
* flight lands. Its readers pending on the flight keep observing it
59-
* (store.ts `pullFamily`, #4536). */
57+
* (optimistic.ts, #4525): the derive's `_time` then, so the mark lapses
58+
* when the derive runs again (optimistic.ts `stands`, #4536). */
6059
g?: number;
6160
}
6261

‎packages/signals/tests/optimistic-guess-split-bindings-4536.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { expect, it } from "vitest";
1818
import {
1919
action,
2020
createLoadingBoundary,
21-
createMemo,
2221
createOptimisticStore,
2322
createRenderEffect,
2423
createRoot,
@@ -34,7 +33,7 @@ const tick = async () => {
3433
}
3534
};
3635

37-
for (const split of [false, "effects", "memo"] as const)
36+
for (const split of [false, true])
3837
for (const on of [false, true])
3938
it(`#4536: a guess over a question in flight holds its readers' frame (split=${split}, on=${on})`, async () => {
4039
const asks: { key: string; res: (v: any) => void }[] = [];
@@ -70,9 +69,7 @@ for (const split of [false, "effects", "memo"] as const)
7069
() => {
7170
if (split) {
7271
createRenderEffect(filter, v => void seen.push(screen(), (f = v)));
73-
createRenderEffect(split === "memo" ? createMemo(list) : list, v => {
74-
seen.push(screen(), (l = v));
75-
});
72+
createRenderEffect(list, v => void seen.push(screen(), (l = v)));
7673
} else
7774
createRenderEffect(
7875
() => `${filter()}: ${list()}`,
@@ -93,9 +90,7 @@ for (const split of [false, "effects", "memo"] as const)
9390
expect(screen()).toBe(on ? "loading" : "open: [#1][#3][#4]");
9491
closeOne(1);
9592
await tick();
96-
// The guess shows over the old answer, or waits with the pending read.
97-
if (on) expect(screen()).toBe("loading");
98-
else expect(["open: [#3][#4]", "open: [#1][#3][#4]"]).toContain(screen());
93+
expect(screen()).toBe(on ? "loading" : "open: [#3][#4]");
9994
await settle("issues:closed", [{ id: 1 }, { id: 2 }]);
10095
expect(screen()).toBe("closed: [#2]");
10196
await settle("close:1", {});

0 commit comments

Comments
 (0)