Skip to content

Commit 075bcaf

Browse files
committed
fix(signals): only a reader in the frame's hold keeps observing a flight a guess was written over (#4536)
The pending-source check kept any reader of the family pending on the flight, so a user effect (#2951's bare write during a refetch) no longer showed the guess. Limit it to the frame's hold (scheduler `blockedBy`): a render effect, or a derivation a pending render effect reads. The split test gains a memo between the list and its binding; that shape may wait with the pending read instead of showing the guess, as the shared test already allows. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 67f0af8 commit 075bcaf

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,15 +1838,30 @@ function serveDataKey(
18381838

18391839
/** Does `c` observe `n`'s flight: among its subscribers (any pass's link — a
18401840
* kept tail included), or, with guesses written over its question (`w`),
1841-
* pending on it? Walks `n`'s subs, not `c`'s deps: a reader of many leaves asks this
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
18421842
* per leaf, and the derive's subs are its few flight observers (#4536). */
18431843
function observes(c: Computed<any>, n: Signal<any>, w: unknown): boolean {
1844-
if (w && c._statusFlags & STATUS_PENDING && c._x?._pendingSources?.has(n as Computed<any>))
1844+
if (
1845+
w &&
1846+
c._statusFlags & STATUS_PENDING &&
1847+
c._x?._pendingSources?.has(n as Computed<any>) &&
1848+
frames(c)
1849+
)
18451850
return true;
18461851
for (let s = n._subs; s !== null; s = s._nextSub) if (s._sub === c) return true;
18471852
return false;
18481853
}
18491854

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+
18501865
/** The projection's obligation (plan sec. 31.7 Q-B): before any value is served
18511866
* through a family target, the derive — the firewall — is brought up to
18521867
* date WITHOUT the reader subscribing to it. Core's `read()` of the

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

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

36-
for (const split of [false, true])
37+
for (const split of [false, "effects", "memo"] as const)
3738
for (const on of [false, true])
3839
it(`#4536: a guess over a question in flight holds its readers' frame (split=${split}, on=${on})`, async () => {
3940
const asks: { key: string; res: (v: any) => void }[] = [];
@@ -69,7 +70,9 @@ for (const split of [false, true])
6970
() => {
7071
if (split) {
7172
createRenderEffect(filter, v => void seen.push(screen(), (f = v)));
72-
createRenderEffect(list, v => void seen.push(screen(), (l = v)));
73+
createRenderEffect(split === "memo" ? createMemo(list) : list, v => {
74+
seen.push(screen(), (l = v));
75+
});
7376
} else
7477
createRenderEffect(
7578
() => `${filter()}: ${list()}`,
@@ -90,7 +93,9 @@ for (const split of [false, true])
9093
expect(screen()).toBe(on ? "loading" : "open: [#1][#3][#4]");
9194
closeOne(1);
9295
await tick();
93-
expect(screen()).toBe(on ? "loading" : "open: [#3][#4]");
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());
9499
await settle("issues:closed", [{ id: 1 }, { id: 2 }]);
95100
expect(screen()).toBe("closed: [#2]");
96101
await settle("close:1", {});

0 commit comments

Comments
 (0)