Skip to content

Commit 91eee6f

Browse files
fix(signals): derived-store landings finalize their attribution flight (#3947)
Co-authored-by: Cursor <[email protected]>
1 parent 893834c commit 91eee6f

3 files changed

Lines changed: 136 additions & 0 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+
Derived-store landings finalize their attribution flight

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,16 @@ export function handleAsync<T>(
463463
return;
464464
}
465465
if (wasUninitialized) landStatus(el, true);
466+
// Attribution hook: this path landed through the setter (derived
467+
// stores, projections), which does not go through setSignal, so the
468+
// plain branch's asyncEnd never ran and the flight stayed open — the
469+
// next flight counted a committed answer as abandoned (#3947).
470+
// direct=true: asyncEnd finalizes either way and stamps an async write
471+
// only if the commit moved `_value`/`_time` or parked `_pendingValue`.
472+
// A store setter does not move this node's `_value`, so no stamp is
473+
// recorded and waterfall chaining through derived stores stays
474+
// unmeasured. Outside the try (#2883 — see attribution-hooks.ts).
475+
if (__OBSERVE__ && attrHooks !== null) attrHooks.asyncEnd(el, undefined, value, true);
466476
} else {
467477
// CARVE 2: the override-covered landing (hold + A18 supersession) and the
468478
// lane-routed landing (derived override, lane effect queue) went with
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* A derived store's async landing must finalize its flight (#3947).
3+
*
4+
* `createStore(async fn, seed)` lands through the setter branch of
5+
* `asyncWrite`, which used to skip `asyncEnd`. The flight stayed open, so
6+
* the next reload's `trackFlightStart` counted the committed answer as
7+
* abandoned. A plain `createMemo` of the same sequence lands both.
8+
*/
9+
import { afterEach, describe, expect, it, vi } from "vitest";
10+
import { attribution, feedback } from "../src/attribution.js";
11+
import {
12+
createMemo,
13+
createRenderEffect,
14+
createRoot,
15+
createSignal,
16+
createStore,
17+
flush
18+
} from "../src/index.js";
19+
20+
afterEach(() => {
21+
attribution.disable();
22+
flush();
23+
vi.restoreAllMocks();
24+
});
25+
26+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
27+
async function until(cond: () => boolean, what: string, timeout = 5000) {
28+
const start = Date.now();
29+
for (;;) {
30+
flush();
31+
if (cond()) return;
32+
if (Date.now() - start > timeout) throw new Error(`timed out waiting for ${what}`);
33+
await wait(5);
34+
}
35+
}
36+
37+
function arm() {
38+
vi.spyOn(console, "warn").mockImplementation(() => {});
39+
vi.spyOn(console, "info").mockImplementation(() => {});
40+
attribution.enable({ log: false });
41+
}
42+
43+
describe("derived store flight finalize (#3947)", () => {
44+
it("a reload that commits both answers lands both flights", async () => {
45+
arm();
46+
const [page, setPage] = createSignal(1);
47+
let resolve!: (v: { title: string }) => void;
48+
const [doc] = createStore(
49+
() => {
50+
page();
51+
return new Promise<{ title: string }>(r => (resolve = r));
52+
},
53+
{ title: "" },
54+
{ name: "doc" }
55+
);
56+
const shown: string[] = [];
57+
createRoot(() =>
58+
createRenderEffect(
59+
() => doc.title,
60+
title => {
61+
shown.push(title);
62+
}
63+
)
64+
);
65+
flush();
66+
resolve({ title: "one" });
67+
await until(() => shown.includes("one"), "first store answer");
68+
setPage(2);
69+
flush();
70+
resolve({ title: "two" });
71+
await until(() => shown.includes("two"), "second store answer");
72+
expect(shown).toEqual(["one", "two"]);
73+
expect(feedback().flights).toEqual([
74+
{
75+
source: "doc",
76+
flights: 2,
77+
landed: 2,
78+
abandoned: 0,
79+
landedMs: expect.any(Number),
80+
worstMs: expect.any(Number)
81+
}
82+
]);
83+
});
84+
85+
it("a plain async memo of the same sequence lands both flights", async () => {
86+
arm();
87+
const [page, setPage] = createSignal(1);
88+
let resolve!: (v: string) => void;
89+
const title = createMemo(
90+
() => {
91+
page();
92+
return new Promise<string>(r => (resolve = r));
93+
},
94+
{ name: "doc" }
95+
);
96+
const shown: string[] = [];
97+
createRoot(() =>
98+
createRenderEffect(title, v => {
99+
shown.push(v);
100+
})
101+
);
102+
flush();
103+
resolve("one");
104+
await until(() => shown.includes("one"), "first memo answer");
105+
setPage(2);
106+
flush();
107+
resolve("two");
108+
await until(() => shown.includes("two"), "second memo answer");
109+
expect(shown).toEqual(["one", "two"]);
110+
expect(feedback().flights).toEqual([
111+
{
112+
source: "doc",
113+
flights: 2,
114+
landed: 2,
115+
abandoned: 0,
116+
landedMs: expect.any(Number),
117+
worstMs: expect.any(Number)
118+
}
119+
]);
120+
});
121+
});

0 commit comments

Comments
 (0)