Skip to content

Commit 064834c

Browse files
fix(web): a deferred revision refetch skips a disposed registry
The refetch a new workspace revision queues now runs only while the query still has a node, so a reader released and its registry disposed before the microtask drains no longer throws "registry is disposed". Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cbc8adb commit 064834c

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎apps/web/src/components/files/projectFilesQueryState.test.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe("project query refresh", () => {
161161
}
162162
});
163163

164-
it("refetches a listing when the workspace mutation changes, not when it is first read", () => {
164+
it("refetches a listing when the workspace mutation changes, not when it is first read", async () => {
165165
let fetches = 0;
166166
const listing = Atom.make(() => {
167167
fetches += 1;
@@ -178,6 +178,10 @@ describe("project query refresh", () => {
178178
read("mutation-1");
179179
expect(fetches).toBe(1);
180180

181+
read("mutation-2");
182+
// The refetch runs after the render that read the new revision; a reader that is not
183+
// subscribed sees it on its next read.
184+
await Promise.resolve();
181185
read("mutation-2");
182186
expect(fetches).toBe(2);
183187
} finally {

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,16 @@ describe("revisionedQuery", () => {
103103
expect(fetches()).toBe(2);
104104
release();
105105
});
106+
107+
it("drops a pending refetch when the registry is disposed before it runs", async () => {
108+
const { registry, query, mount } = setup();
109+
110+
(await mount("mutation-1"))();
111+
const release = registry.mount(revisionedQuery(query, "mutation-2"));
112+
release();
113+
registry.dispose();
114+
115+
// A refetch that ran now would throw "registry is disposed" as an uncaught error.
116+
await Promise.resolve();
117+
});
106118
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ 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-
// 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.
29+
// Deferred: this builds while React renders, and a refresh would synchronously
30+
// re-render the query's other readers, which React rejects.
3231
if (stale) {
3332
const registry = get.registry;
34-
queueMicrotask(() => registry.refresh(query));
33+
queueMicrotask(() => {
34+
// No node, as after the registry is disposed: nothing cached to refetch.
35+
if (registry.getNodes().has(queryKey(query))) registry.refresh(query);
36+
});
3537
}
3638
revisions.set(query, revision);
3739
}

0 commit comments

Comments
 (0)