Skip to content

Commit 76f4776

Browse files
refactor(web): file breadcrumb menus refetch on a new workspace change, not on every open
The breadcrumb contents menu paired its listEntries query with useWorkspaceMutationRefresh. That hook's handled-token ref is per mount, so every time the menu opened while a workspace mutation id existed, the effect refreshed a listing the mount had just requested: two requests per open, and an effect whose only job was to compare a prop with the last value it saw. Add revisionedQuery, which reads a query atom under a revision (the workspace mutation id) and refreshes it only when the revision differs from the one the query was last read under. The first read only records the revision, and the record outlives the reading node, so a remount under the same revision does not refetch. useProjectEntriesQuery takes an optional revision and the breadcrumb menu passes its mutation id instead of running the refresh effect. Effects: FileBreadcrumbs.tsx 1 -> 0 (useWorkspaceMutationRefresh call removed). useWorkspaceMutationRefresh itself stays for its other callers. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bfc7177 commit 76f4776

5 files changed

Lines changed: 175 additions & 8 deletions

File tree

‎apps/web/src/components/files/FileBreadcrumbs.tsx‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
} from "~/components/ui/menu";
1818
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
1919
import { useTheme } from "~/hooks/useTheme";
20-
import { useWorkspaceMutationRefresh } from "~/hooks/useWorkspaceMutationRefresh";
2120
import { cn } from "~/lib/utils";
2221
import { isHostPath } from "@t3tools/shared/path";
2322

@@ -85,12 +84,12 @@ function BreadcrumbMenuContent(props: {
8584
readonly rootPath: string;
8685
readonly workspaceMutationId: string | null;
8786
}) {
88-
const entriesQuery = useProjectEntriesQuery(props.environmentId, props.cwd, props.directoryPath);
89-
useWorkspaceMutationRefresh({
90-
mutationId: props.workspaceMutationId,
91-
refresh: entriesQuery.refresh,
92-
resourceKey: `files:${props.environmentId}:${props.cwd}`,
93-
});
87+
const entriesQuery = useProjectEntriesQuery(
88+
props.environmentId,
89+
props.cwd,
90+
props.directoryPath,
91+
props.workspaceMutationId,
92+
);
9493
const { resolvedTheme } = useTheme();
9594
const entries = entriesQuery.data?.entries ?? [];
9695
const entriesTruncated = entriesQuery.data?.truncated ?? false;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ describe("project query refresh", () => {
161161
}
162162
});
163163

164+
it("refetches a listing when the workspace mutation changes, not when it is first read", () => {
165+
let fetches = 0;
166+
const listing = Atom.make(() => {
167+
fetches += 1;
168+
return AsyncResult.success({ entries: [], truncated: false });
169+
}).pipe(Atom.keepAlive);
170+
projectMocks.listEntries.mockReturnValue(listing);
171+
const registry = AtomRegistry.make();
172+
atomHooks.registry = registry;
173+
const read = (mutationId: string | null) =>
174+
useProjectEntriesQuery(environmentId, "/repo", "src", mutationId);
175+
176+
try {
177+
read("mutation-1");
178+
read("mutation-1");
179+
expect(fetches).toBe(1);
180+
181+
read("mutation-2");
182+
expect(fetches).toBe(2);
183+
} finally {
184+
registry.dispose();
185+
atomHooks.registry = null;
186+
}
187+
});
188+
164189
it("replaces an in-flight initial read when a workspace mutation arrives", async () => {
165190
const requests: Array<ReturnType<typeof deferred<ProjectReadFileResult>>> = [];
166191
const readAtom = Atom.make(

‎apps/web/src/components/files/projectFilesQueryState.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useFilesystemReadAccess } from "~/state/filesystem";
1616
import { projectEnvironment } from "~/state/projects";
1717
import { useProjectPathSearch } from "~/state/queries";
1818
import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
19+
import { revisionedQuery } from "~/state/revisionedQuery";
1920

2021
const EMPTY_PROJECT_FILE_PATH = "";
2122
const EMPTY_PROJECT_ENTRIES_QUERY_ATOM = Atom.make(
@@ -190,17 +191,19 @@ function errorMessage(cause: unknown): string | null {
190191

191192
const isProjectReadFileError = Schema.is(ProjectReadFileError);
192193

194+
/** `revision` is the workspace mutation id; a new one refetches the listing. */
193195
export function useProjectEntriesQuery(
194196
environmentId: EnvironmentId,
195197
cwd: string,
196198
directoryPath?: string,
199+
revision?: string | null,
197200
): ProjectQueryState<ProjectListEntriesResult> {
198201
const fileAccess = useFilesystemReadAccess(environmentId);
199202
const { canReadFiles } = fileAccess;
200203
const atom = canReadFiles
201204
? getProjectEntriesQueryAtom(environmentId, cwd, directoryPath)
202205
: EMPTY_PROJECT_ENTRIES_QUERY_ATOM;
203-
const result = useAtomValue(atom);
206+
const result = useAtomValue(revisionedQuery(atom, canReadFiles ? revision : undefined));
204207
const refreshAtom = useAtomRefresh(atom);
205208
const refresh = () => refreshAtom();
206209
return {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Atom, AtomRegistry } from "effect/reactivity";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { revisionedQuery } from "./revisionedQuery";
5+
6+
// A stand-in query that counts its fetches.
7+
function setup() {
8+
let fetches = 0;
9+
// Node removal runs on the registry's scheduler; releasing flushes it so the
10+
// next mount of the same revision builds a fresh node, as a React remount does.
11+
const scheduled: Array<() => void> = [];
12+
const registry = AtomRegistry.make({
13+
scheduleTask: (task) => {
14+
scheduled.push(task);
15+
return () => {};
16+
},
17+
});
18+
const query = Atom.make(() => {
19+
fetches += 1;
20+
return fetches;
21+
}).pipe(Atom.keepAlive);
22+
const mount = (revision: string | null | undefined) => {
23+
const release = registry.mount(revisionedQuery(query, revision));
24+
return () => {
25+
release();
26+
for (const task of scheduled.splice(0)) task();
27+
};
28+
};
29+
return { registry, query, mount, fetches: () => fetches };
30+
}
31+
32+
describe("revisionedQuery", () => {
33+
it("refetches once per new revision, not on the first read or a remount", () => {
34+
const { mount, fetches } = setup();
35+
36+
mount("mutation-1")();
37+
expect(fetches()).toBe(1);
38+
39+
mount("mutation-1")();
40+
expect(fetches()).toBe(1);
41+
42+
mount("mutation-2")();
43+
expect(fetches()).toBe(2);
44+
});
45+
46+
it("does not ping-pong while readers of the old and new revision overlap", () => {
47+
const { mount, fetches } = setup();
48+
49+
const releaseOld = mount("mutation-1");
50+
const releaseNew = mount("mutation-2");
51+
expect(fetches()).toBe(2);
52+
releaseOld();
53+
releaseNew();
54+
});
55+
56+
it("refetches when an earlier revision is read again", () => {
57+
const { mount, fetches } = setup();
58+
59+
mount("mutation-1")();
60+
mount("mutation-2")();
61+
mount("mutation-1")();
62+
expect(fetches()).toBe(3);
63+
});
64+
65+
it("leaves the query alone when read without a revision", () => {
66+
const { mount, query, fetches } = setup();
67+
68+
mount("mutation-1")();
69+
expect(revisionedQuery(query, undefined)).toBe(query);
70+
mount(undefined)();
71+
mount("mutation-1")();
72+
expect(fetches()).toBe(1);
73+
});
74+
75+
it("refetches the query when the reader is refreshed", () => {
76+
const { registry, query, mount, fetches } = setup();
77+
78+
const release = mount("mutation-1");
79+
registry.refresh(revisionedQuery(query, "mutation-1"));
80+
expect(fetches()).toBe(2);
81+
release();
82+
});
83+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as Option from "effect/Option";
2+
import { Atom, type AtomRegistry } from "effect/reactivity";
3+
4+
// Outside the nodes: a node is dropped once unmounted, and the next read must
5+
// still compare against the revision the query was last read under.
6+
const readRevisions = new WeakMap<AtomRegistry.AtomRegistry, WeakMap<object, string | null>>();
7+
8+
const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
9+
Atom.family((revision: string | null) =>
10+
Atom.readable(
11+
(get) => {
12+
// Only a node's first build compares: a node still mounted under the old
13+
// revision rebuilds when the query refetches, and must not refetch it again.
14+
if (Option.isNone(get.self())) {
15+
let revisions = readRevisions.get(get.registry);
16+
if (revisions === undefined) {
17+
revisions = new WeakMap();
18+
readRevisions.set(get.registry, revisions);
19+
}
20+
if (revisions.has(query) && revisions.get(query) !== revision) get.refresh(query);
21+
revisions.set(query, revision);
22+
}
23+
return get(query);
24+
},
25+
// Refreshing the reader refetches the query it reads.
26+
(refresh) => refresh(query),
27+
),
28+
);
29+
30+
// Held as long as the query lives, so one (query, revision) pair keeps one atom; an
31+
// `Atom.family` would hold the inner family only weakly and could swap the atom under a reader.
32+
const revisionedFamilies = new WeakMap<
33+
Atom.Atom<unknown>,
34+
ReturnType<typeof makeRevisionedFamily>
35+
>();
36+
const revisionedFamily = (query: Atom.Atom<unknown>) => {
37+
let family = revisionedFamilies.get(query);
38+
if (family === undefined) {
39+
family = makeRevisionedFamily(query);
40+
revisionedFamilies.set(query, family);
41+
}
42+
return family;
43+
};
44+
45+
/**
46+
* Reads `query` under a workspace revision (latest mutation id); a different
47+
* revision than the last read refetches it. The first read only records the
48+
* revision. `undefined` reads without taking part, for readers that must not
49+
* refetch now.
50+
*/
51+
export function revisionedQuery<A>(
52+
query: Atom.Atom<A>,
53+
revision: string | null | undefined,
54+
): Atom.Atom<A> {
55+
if (revision === undefined) return query;
56+
return revisionedFamily(query)(revision) as Atom.Atom<A>;
57+
}

0 commit comments

Comments
 (0)