Skip to content

Commit 217ee4e

Browse files
perf(web): useDirectoryEntries compiles with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f84deb6 commit 217ee4e

2 files changed

Lines changed: 97 additions & 98 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,5 @@ export const REACT_COMPILED_FILES = [
5757
"apps/web/src/components/files/AttachmentFilePreview.tsx",
5858
"apps/web/src/components/usage/UsagePage.tsx",
5959
"apps/web/src/components/ProviderUpdateEnvironmentRows.tsx",
60+
"apps/web/src/components/files/useDirectoryEntries.ts",
6061
] as const;

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

Lines changed: 96 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,134 @@
11
import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts";
22
import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime";
33
import * as Cause from "effect/Cause";
4-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
4+
import { useEffect, useRef, useState } from "react";
55

66
import { appAtomRegistry } from "~/rpc/atomRegistry";
77
import { projectEnvironment } from "~/state/projects";
88

99
export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) {
1010
const [directories, setDirectories] = useState(new Map<string, readonly ProjectEntry[]>());
1111
const [errors, setErrors] = useState(new Map<string, string>());
12-
const [pending, setPending] = useState(0);
13-
const [loadingDirectories, setLoadingDirectories] = useState<ReadonlySet<string>>(new Set());
12+
// The root starts counted as loading because the mount effect requests it.
13+
const [pending, setPending] = useState(1);
14+
const [loadingDirectories, setLoadingDirectories] = useState<ReadonlySet<string>>(
15+
() => new Set([""]),
16+
);
1417
const requests = useRef(new Map<string, Promise<void>>());
1518
const loaded = useRef(new Set<string>());
1619
const requested = useRef(new Set<string>());
1720
const active = useRef(true);
1821
const running = useRef(0);
1922
const waiting = useRef<Array<() => void>>([]);
2023

21-
const load = useCallback(
22-
function loadDirectory(directoryPath: string, refresh = false): Promise<void> {
23-
const existing = requests.current.get(directoryPath);
24-
if (existing)
25-
return refresh ? existing.then(() => loadDirectory(directoryPath, true)) : existing;
26-
if (!refresh && loaded.current.has(directoryPath)) return Promise.resolve();
27-
loaded.current.add(directoryPath);
28-
requested.current.add(directoryPath);
29-
const atom = projectEnvironment.listEntries({ environmentId, input: { cwd, directoryPath } });
30-
setPending((count) => count + 1);
31-
if (!refresh) setLoadingDirectories((previous) => new Set(previous).add(directoryPath));
32-
const request = (async () => {
33-
if (running.current >= 4)
34-
await new Promise<void>((resolve) => waiting.current.push(resolve));
35-
else running.current++;
36-
try {
37-
if (!active.current) return undefined;
38-
return await executeAtomQuery(appAtomRegistry, atom, {
24+
// Callers count the request as pending first; the mount load is counted by the initial state.
25+
const startRequest = (directoryPath: string, refresh: boolean): Promise<void> => {
26+
loaded.current.add(directoryPath);
27+
requested.current.add(directoryPath);
28+
const atom = projectEnvironment.listEntries({ environmentId, input: { cwd, directoryPath } });
29+
const request = (async () => {
30+
if (running.current >= 4) await new Promise<void>((resolve) => waiting.current.push(resolve));
31+
else running.current++;
32+
// executeAtomQuery settles instead of throwing, so the slot is always released.
33+
const result = active.current
34+
? await executeAtomQuery(appAtomRegistry, atom, {
3935
refresh: true,
4036
reportFailure: false,
4137
reportDefect: false,
38+
})
39+
: undefined;
40+
const next = waiting.current.shift();
41+
if (next) next();
42+
else running.current--;
43+
return result;
44+
})()
45+
.then((result) => {
46+
if (!active.current || !result) return;
47+
if (result._tag === "Success") {
48+
setDirectories((previous) =>
49+
new Map(previous).set(
50+
directoryPath,
51+
result.value.entries.filter(
52+
(entry) =>
53+
entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
54+
),
55+
),
56+
);
57+
setErrors((previous) => {
58+
const next = new Map(previous);
59+
next.delete(directoryPath);
60+
return next;
4261
});
43-
} finally {
44-
const next = waiting.current.shift();
45-
if (next) next();
46-
else running.current--;
62+
} else {
63+
loaded.current.delete(directoryPath);
64+
const cause = Cause.squash(result.cause);
65+
setErrors((previous) =>
66+
new Map(previous).set(
67+
directoryPath,
68+
cause instanceof Error ? cause.message : "Unable to load folder.",
69+
),
70+
);
4771
}
48-
})()
49-
.then((result) => {
50-
if (!active.current || !result) return;
51-
if (result._tag === "Success") {
52-
setDirectories((previous) =>
53-
new Map(previous).set(
54-
directoryPath,
55-
result.value.entries.filter(
56-
(entry) =>
57-
entry.path.slice(0, Math.max(0, entry.path.lastIndexOf("/"))) === directoryPath,
58-
),
59-
),
60-
);
61-
setErrors((previous) => {
62-
const next = new Map(previous);
63-
next.delete(directoryPath);
64-
return next;
65-
});
66-
} else {
67-
loaded.current.delete(directoryPath);
68-
const cause = Cause.squash(result.cause);
69-
setErrors((previous) =>
70-
new Map(previous).set(
71-
directoryPath,
72-
cause instanceof Error ? cause.message : "Unable to load folder.",
73-
),
74-
);
75-
}
76-
})
77-
.finally(() => {
78-
requests.current.delete(directoryPath);
79-
if (!active.current) return;
80-
setPending((count) => count - 1);
81-
if (!refresh)
82-
setLoadingDirectories((previous) => {
83-
const next = new Set(previous);
84-
next.delete(directoryPath);
85-
return next;
86-
});
87-
});
88-
requests.current.set(directoryPath, request);
89-
return request;
90-
},
91-
[cwd, environmentId],
92-
);
72+
})
73+
.finally(() => {
74+
requests.current.delete(directoryPath);
75+
if (!active.current) return;
76+
setPending((count) => count - 1);
77+
if (!refresh)
78+
setLoadingDirectories((previous) => {
79+
const next = new Set(previous);
80+
next.delete(directoryPath);
81+
return next;
82+
});
83+
});
84+
requests.current.set(directoryPath, request);
85+
return request;
86+
};
87+
88+
const load = function loadDirectory(directoryPath: string, refresh = false): Promise<void> {
89+
const existing = requests.current.get(directoryPath);
90+
if (existing)
91+
return refresh ? existing.then(() => loadDirectory(directoryPath, true)) : existing;
92+
if (!refresh && loaded.current.has(directoryPath)) return Promise.resolve();
93+
setPending((count) => count + 1);
94+
if (!refresh) setLoadingDirectories((previous) => new Set(previous).add(directoryPath));
95+
return startRequest(directoryPath, refresh);
96+
};
9397

9498
useEffect(() => {
9599
active.current = true;
96-
void load("");
100+
if (!requests.current.has("") && !loaded.current.has("")) void startRequest("", false);
97101
return () => {
98102
active.current = false;
99103
};
100-
}, [load]);
104+
}, [startRequest]);
101105

102-
const entries = useMemo(() => {
103-
const result: ProjectEntry[] = [];
104-
const visit = (path: string) => {
105-
for (const entry of directories.get(path) ?? []) {
106-
result.push(entry);
107-
if (entry.kind === "directory") visit(entry.path);
108-
}
109-
};
110-
visit("");
111-
return result;
112-
}, [directories]);
106+
const entries: ProjectEntry[] = [];
107+
const visit = (path: string) => {
108+
for (const entry of directories.get(path) ?? []) {
109+
entries.push(entry);
110+
if (entry.kind === "directory") visit(entry.path);
111+
}
112+
};
113+
visit("");
113114

114-
const reachableDirectories = useMemo(
115-
() =>
116-
new Set([
117-
"",
118-
...entries.filter((entry) => entry.kind === "directory").map((entry) => entry.path),
119-
]),
120-
[entries],
121-
);
115+
const reachableDirectories = new Set([
116+
"",
117+
...entries.filter((entry) => entry.kind === "directory").map((entry) => entry.path),
118+
]);
122119

123-
const refresh = useCallback(() => {
120+
const refresh = () => {
124121
const paths = [...requested.current].filter((path) => reachableDirectories.has(path));
125-
let next = 0;
122+
const workerCount = Math.min(4, paths.length);
126123
const worker = async () => {
127-
while (next < paths.length && active.current) {
128-
const path = paths[next++];
129-
if (path !== undefined) await load(path, true);
124+
while (active.current) {
125+
const path = paths.shift();
126+
if (path === undefined) return;
127+
await load(path, true);
130128
}
131129
};
132-
for (let index = 0; index < Math.min(4, paths.length); index++) void worker();
133-
}, [load, reachableDirectories]);
130+
for (let index = 0; index < workerCount; index++) void worker();
131+
};
134132

135133
return {
136134
entries,

0 commit comments

Comments
 (0)