Skip to content

Commit 1ea378c

Browse files
authored
perf(web): reveal nested files faster (pingdotgg#21048)
1 parent df198f9 commit 1ea378c

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { joinHostPath } from "./filePath";
3131
import { createFileTreeDragMentionController } from "./fileTreeDragMention";
3232
import { areAllDirectoriesExpanded, setAllDirectoriesExpanded } from "./fileTreeExpansion";
3333
import { buildFileTreePathUpdates } from "./fileTreePathReconciliation";
34+
import { loadFileAncestors } from "./loadFileAncestors";
3435
import { useDirectoryEntries } from "./useDirectoryEntries";
3536
import { useProjectPathSearch } from "~/state/queries";
3637

@@ -366,12 +367,7 @@ export default function FileBrowserPanel({
366367
useEffect(() => {
367368
if (!selectedPath) return;
368369
const controller = new AbortController();
369-
void (async () => {
370-
const segments = selectedPath.split("/");
371-
for (let index = 0; index < segments.length && !controller.signal.aborted; index++) {
372-
await load(segments.slice(0, index).join("/"));
373-
}
374-
})();
370+
void loadFileAncestors(selectedPath, load, { signal: controller.signal });
375371
return () => {
376372
controller.abort();
377373
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, it } from "vite-plus/test";
2+
import { loadFileAncestors } from "./loadFileAncestors";
3+
4+
function queuedLoader() {
5+
const paths: string[] = [];
6+
const resolve: Array<() => void> = [];
7+
const load = (path: string) => {
8+
paths.push(path);
9+
return new Promise<void>((done) => resolve.push(done));
10+
};
11+
return { paths, resolve, load };
12+
}
13+
14+
it("bounds loads to four and finishes children even when parents finish last", async () => {
15+
const queue = queuedLoader();
16+
const loading = loadFileAncestors("a/b/c/d/e/f/file.ts", queue.load, new AbortController());
17+
for (let index = 3; index < 6; index++) {
18+
queue.resolve[index]?.();
19+
await Promise.resolve();
20+
expect(queue.paths).toHaveLength(index + 2);
21+
}
22+
expect(queue.paths).toEqual(["", "a", "a/b", "a/b/c", "a/b/c/d", "a/b/c/d/e", "a/b/c/d/e/f"]);
23+
for (const resolve of queue.resolve) resolve();
24+
await loading;
25+
});
26+
27+
it("stops aborted reveals while a new selection loads independently", async () => {
28+
const old = queuedLoader();
29+
const controller = new AbortController();
30+
const oldReveal = loadFileAncestors("old/a/b/c/d/file.ts", old.load, controller);
31+
controller.abort();
32+
const fresh = queuedLoader();
33+
const newReveal = loadFileAncestors("new/file.ts", fresh.load, new AbortController());
34+
for (const resolve of [...old.resolve, ...fresh.resolve]) resolve();
35+
await Promise.all([oldReveal, newReveal]);
36+
await loadFileAncestors("aborted/path/file.ts", old.load, controller);
37+
expect(old.paths).toEqual(["", "old", "old/a", "old/a/b"]);
38+
expect(fresh.paths).toEqual(["", "new"]);
39+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** The loader owns deduplication and the shared request cap, including other tree loads. */
2+
export async function loadFileAncestors(
3+
selectedPath: string,
4+
load: (directoryPath: string) => Promise<void>,
5+
options: { readonly signal: AbortSignal },
6+
) {
7+
const segments = selectedPath.split("/");
8+
let next = 0;
9+
const worker = async () => {
10+
while (next < segments.length && !options.signal.aborted) {
11+
await load(segments.slice(0, next++).join("/"));
12+
}
13+
};
14+
await Promise.all(Array.from({ length: Math.min(4, segments.length) }, worker));
15+
}

0 commit comments

Comments
 (0)