|
1 | 1 | import type { EnvironmentId, ProjectEntry } from "@t3tools/contracts"; |
2 | 2 | import { executeAtomQuery } from "@t3tools/client-runtime/state/runtime"; |
3 | 3 | import * as Cause from "effect/Cause"; |
4 | | -import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 4 | +import { useEffect, useRef, useState } from "react"; |
5 | 5 |
|
6 | 6 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
7 | 7 | import { projectEnvironment } from "~/state/projects"; |
8 | 8 |
|
9 | 9 | export function useDirectoryEntries(environmentId: EnvironmentId, cwd: string) { |
10 | 10 | const [directories, setDirectories] = useState(new Map<string, readonly ProjectEntry[]>()); |
11 | 11 | 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 | + ); |
14 | 17 | const requests = useRef(new Map<string, Promise<void>>()); |
15 | 18 | const loaded = useRef(new Set<string>()); |
16 | 19 | const requested = useRef(new Set<string>()); |
17 | 20 | const active = useRef(true); |
18 | 21 | const running = useRef(0); |
19 | 22 | const waiting = useRef<Array<() => void>>([]); |
20 | 23 |
|
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, { |
39 | 35 | refresh: true, |
40 | 36 | reportFailure: false, |
41 | 37 | 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; |
42 | 61 | }); |
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 | + ); |
47 | 71 | } |
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 | + }; |
93 | 97 |
|
94 | 98 | useEffect(() => { |
95 | 99 | active.current = true; |
96 | | - void load(""); |
| 100 | + if (!requests.current.has("") && !loaded.current.has("")) void startRequest("", false); |
97 | 101 | return () => { |
98 | 102 | active.current = false; |
99 | 103 | }; |
100 | | - }, [load]); |
| 104 | + }, [startRequest]); |
101 | 105 |
|
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(""); |
113 | 114 |
|
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 | + ]); |
122 | 119 |
|
123 | | - const refresh = useCallback(() => { |
| 120 | + const refresh = () => { |
124 | 121 | const paths = [...requested.current].filter((path) => reachableDirectories.has(path)); |
125 | | - let next = 0; |
| 122 | + const workerCount = Math.min(4, paths.length); |
126 | 123 | 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); |
130 | 128 | } |
131 | 129 | }; |
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 | + }; |
134 | 132 |
|
135 | 133 | return { |
136 | 134 | entries, |
|
0 commit comments