Skip to content

Commit 8ce1567

Browse files
fix(web): a new workspace revision no longer updates a component mid-render
A revisioned query refetched while its reader rendered, which synchronously notified readers still subscribed under the old revision. With the diff panel now reading its preview this way, a turn landing while it was open logged "Cannot update a component (DiffPanel) while rendering a different component". The refetch now runs in a microtask, after the render. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0c2a374 commit 8ce1567

3 files changed

Lines changed: 70 additions & 26 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { RegistryContext, useAtomValue } from "@effect/atom-react";
2+
import { Atom, AtomRegistry } from "effect/reactivity";
3+
import { describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { render } from "../../test/render";
6+
import { revisionedQuery } from "./revisionedQuery";
7+
8+
describe("revisionedQuery in React", () => {
9+
it("refetches for a new revision without updating a component during render", async () => {
10+
let fetches = 0;
11+
const query = Atom.make(() => {
12+
fetches += 1;
13+
return fetches;
14+
}).pipe(Atom.keepAlive);
15+
const registry = AtomRegistry.make();
16+
function Reader({ revision }: { revision: string }) {
17+
return <span>fetch {useAtomValue(revisionedQuery(query, revision))}</span>;
18+
}
19+
const ui = (revision: string) => (
20+
<RegistryContext.Provider value={registry}>
21+
<Reader revision={revision} />
22+
</RegistryContext.Provider>
23+
);
24+
using consoleError = vi.spyOn(console, "error");
25+
// A bare atom reader: no links or navigation to route.
26+
await using screen = await render(ui("mutation-1"), { router: false });
27+
await expect.element(screen.getByText("fetch 1")).toBeInTheDocument();
28+
29+
await screen.rerender(ui("mutation-2"));
30+
31+
await expect.element(screen.getByText("fetch 2")).toBeInTheDocument();
32+
expect(consoleError.mock.calls.map(([message]) => String(message))).not.toContainEqual(
33+
expect.stringContaining("Cannot update a component"),
34+
);
35+
});
36+
});

‎apps/web/src/state/revisionedQuery.test.ts‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ function setup() {
1919
fetches += 1;
2020
return fetches;
2121
}).pipe(Atom.keepAlive);
22-
const mount = (revision: string | null | undefined) => {
22+
const mount = async (revision: string | null | undefined) => {
2323
const release = registry.mount(revisionedQuery(query, revision));
24+
// A stale read refetches in a microtask, after the render that read it.
25+
await Promise.resolve();
2426
return () => {
2527
release();
2628
for (const task of scheduled.splice(0)) task();
@@ -30,73 +32,73 @@ function setup() {
3032
}
3133

3234
describe("revisionedQuery", () => {
33-
it("refetches once per new revision, not on the first read or a remount", () => {
35+
it("refetches once per new revision, not on the first read or a remount", async () => {
3436
const { mount, fetches } = setup();
3537

36-
mount("mutation-1")();
38+
(await mount("mutation-1"))();
3739
expect(fetches()).toBe(1);
3840

39-
mount("mutation-1")();
41+
(await mount("mutation-1"))();
4042
expect(fetches()).toBe(1);
4143

42-
mount("mutation-2")();
44+
(await mount("mutation-2"))();
4345
expect(fetches()).toBe(2);
4446
});
4547

46-
it("does not ping-pong while readers of the old and new revision overlap", () => {
48+
it("does not ping-pong while readers of the old and new revision overlap", async () => {
4749
const { mount, fetches } = setup();
4850

49-
const releaseOld = mount("mutation-1");
50-
const releaseNew = mount("mutation-2");
51+
const releaseOld = await mount("mutation-1");
52+
const releaseNew = await mount("mutation-2");
5153
expect(fetches()).toBe(2);
5254
releaseOld();
5355
releaseNew();
5456
});
5557

56-
it("refetches when an earlier revision is read again", () => {
58+
it("refetches when an earlier revision is read again", async () => {
5759
const { mount, fetches } = setup();
5860

59-
mount("mutation-1")();
60-
mount("mutation-2")();
61-
mount("mutation-1")();
61+
(await mount("mutation-1"))();
62+
(await mount("mutation-2"))();
63+
(await mount("mutation-1"))();
6264
expect(fetches()).toBe(3);
6365
});
6466

65-
it("refetches a query another reader already cached on its first revisioned read", () => {
67+
it("refetches a query another reader already cached on its first revisioned read", async () => {
6668
const { mount, fetches } = setup();
6769

68-
mount(undefined)();
70+
(await mount(undefined))();
6971
expect(fetches()).toBe(1);
7072

71-
mount("mutation-1")();
73+
(await mount("mutation-1"))();
7274
expect(fetches()).toBe(2);
7375

74-
mount("mutation-1")();
76+
(await mount("mutation-1"))();
7577
expect(fetches()).toBe(2);
7678
});
7779

78-
it("does not refetch a cached query on a first read before any workspace change", () => {
80+
it("does not refetch a cached query on a first read before any workspace change", async () => {
7981
const { mount, fetches } = setup();
8082

81-
mount(undefined)();
82-
mount(null)();
83+
(await mount(undefined))();
84+
(await mount(null))();
8385
expect(fetches()).toBe(1);
8486
});
8587

86-
it("leaves the query alone when read without a revision", () => {
88+
it("leaves the query alone when read without a revision", async () => {
8789
const { mount, query, fetches } = setup();
8890

89-
mount("mutation-1")();
91+
(await mount("mutation-1"))();
9092
expect(revisionedQuery(query, undefined)).toBe(query);
91-
mount(undefined)();
92-
mount("mutation-1")();
93+
(await mount(undefined))();
94+
(await mount("mutation-1"))();
9395
expect(fetches()).toBe(1);
9496
});
9597

96-
it("refetches the query when the reader is refreshed", () => {
98+
it("refetches the query when the reader is refreshed", async () => {
9799
const { registry, query, mount, fetches } = setup();
98100

99-
const release = mount("mutation-1");
101+
const release = await mount("mutation-1");
100102
registry.refresh(revisionedQuery(query, "mutation-1"));
101103
expect(fetches()).toBe(2);
102104
release();

‎apps/web/src/state/revisionedQuery.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
2626
: // A reader without a revision may have cached it before the latest
2727
// workspace change; a query nobody has read yet fetches fresh anyway.
2828
revision !== null && get.registry.getNodes().has(queryKey(query));
29-
if (stale) get.refresh(query);
29+
// A node first builds while React renders its reader, and a refresh synchronously
30+
// notifies the query's other readers, such as the same component still subscribed
31+
// under the old revision: React rejects that as an update during render.
32+
if (stale) {
33+
const registry = get.registry;
34+
queueMicrotask(() => registry.refresh(query));
35+
}
3036
revisions.set(query, revision);
3137
}
3238
return get(query);

0 commit comments

Comments
 (0)