Skip to content

Commit 352b964

Browse files
authored
Merge branch 'main' into fix/desktop-linux-follow-system-theme
2 parents 02f5b8f + a240408 commit 352b964

5 files changed

Lines changed: 205 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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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("refetches a query another reader already cached on its first revisioned read", () => {
66+
const { mount, fetches } = setup();
67+
68+
mount(undefined)();
69+
expect(fetches()).toBe(1);
70+
71+
mount("mutation-1")();
72+
expect(fetches()).toBe(2);
73+
74+
mount("mutation-1")();
75+
expect(fetches()).toBe(2);
76+
});
77+
78+
it("does not refetch a cached query on a first read before any workspace change", () => {
79+
const { mount, fetches } = setup();
80+
81+
mount(undefined)();
82+
mount(null)();
83+
expect(fetches()).toBe(1);
84+
});
85+
86+
it("leaves the query alone when read without a revision", () => {
87+
const { mount, query, fetches } = setup();
88+
89+
mount("mutation-1")();
90+
expect(revisionedQuery(query, undefined)).toBe(query);
91+
mount(undefined)();
92+
mount("mutation-1")();
93+
expect(fetches()).toBe(1);
94+
});
95+
96+
it("refetches the query when the reader is refreshed", () => {
97+
const { registry, query, mount, fetches } = setup();
98+
99+
const release = mount("mutation-1");
100+
registry.refresh(revisionedQuery(query, "mutation-1"));
101+
expect(fetches()).toBe(2);
102+
release();
103+
});
104+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
// The registry keys serializable atoms' nodes by their key, not the atom.
9+
const queryKey = (query: Atom.Atom<unknown>) =>
10+
Atom.isSerializable(query) ? query[Atom.SerializableTypeId].key : query;
11+
12+
const makeRevisionedFamily = (query: Atom.Atom<unknown>) =>
13+
Atom.family((revision: string | null) =>
14+
Atom.readable(
15+
(get) => {
16+
// Only a node's first build compares: a node still mounted under the old
17+
// revision rebuilds when the query refetches, and must not refetch it again.
18+
if (Option.isNone(get.self())) {
19+
let revisions = readRevisions.get(get.registry);
20+
if (revisions === undefined) {
21+
revisions = new WeakMap();
22+
readRevisions.set(get.registry, revisions);
23+
}
24+
const stale = revisions.has(query)
25+
? revisions.get(query) !== revision
26+
: // A reader without a revision may have cached it before the latest
27+
// workspace change; a query nobody has read yet fetches fresh anyway.
28+
revision !== null && get.registry.getNodes().has(queryKey(query));
29+
if (stale) get.refresh(query);
30+
revisions.set(query, revision);
31+
}
32+
return get(query);
33+
},
34+
// Refreshing the reader refetches the query it reads.
35+
(refresh) => refresh(query),
36+
),
37+
);
38+
39+
// Held as long as the query lives, so one (query, revision) pair keeps one atom; an
40+
// `Atom.family` would hold the inner family only weakly and could swap the atom under a reader.
41+
const revisionedFamilies = new WeakMap<
42+
Atom.Atom<unknown>,
43+
ReturnType<typeof makeRevisionedFamily>
44+
>();
45+
const revisionedFamily = (query: Atom.Atom<unknown>) => {
46+
let family = revisionedFamilies.get(query);
47+
if (family === undefined) {
48+
family = makeRevisionedFamily(query);
49+
revisionedFamilies.set(query, family);
50+
}
51+
return family;
52+
};
53+
54+
/**
55+
* Reads `query` under a workspace revision (latest mutation id); a different
56+
* revision than the last read refetches it. The first read records the revision,
57+
* and refetches only a query that a reader without a revision already cached.
58+
* `undefined` reads without taking part, for readers that must not refetch now.
59+
*/
60+
export function revisionedQuery<A>(
61+
query: Atom.Atom<A>,
62+
revision: string | null | undefined,
63+
): Atom.Atom<A> {
64+
if (revision === undefined) return query;
65+
return revisionedFamily(query)(revision) as Atom.Atom<A>;
66+
}

0 commit comments

Comments
 (0)