perf(files): keep parked trees idle and batch the watcher re-arm

This commit is contained in:
Menghuan1918 committed 2026-10-24 02:17:07 +08:00
1 parent 27a920f85b
commit 4cb7937a37
6 files changed
+146 -39

No files matched your search

+20 -3
View File
@@ -587,6 +587,9 @@ export function FileTree(props: {
/** The live props for the stable callbacks below (identity churns per render). */
const propsRef = useRef(props)
propsRef.current = props
/** Live visibility for the load effects: a parked tree fetches nothing. */
const visibleRef = useRef(visible !== false)
visibleRef.current = visible !== false
const [data, setData] = useState<Record<string, LevelData>>({})
/**
* Registry revision for the file-icon feature: bumps on ANY registry
@@ -927,6 +930,11 @@ export function FileTree(props: {
loadLevels([dir], { force: true })
}, [loadLevels])
/** Re-list a SET of directories in ONE batch (the parked→visible re-arm). */
const retryDirs = useCallback((dirs: readonly string[]) => {
if (dirs.length > 0) loadLevels([...dirs], { force: true })
}, [loadLevels])
/**
* Settle the tree after one rename/delete landed at `prefix`: drop every
* cached level at or under the old path, collapse the now-stale expanded
@@ -980,6 +988,10 @@ export function FileTree(props: {
useEffect(() => {
if (lastTick.current === refreshTick) return
lastTick.current = refreshTick
// A parked tree drops the tick outright: the watcher's re-arm re-lists the
// whole watched set when the tab comes back, so forcing a load here would
// only duplicate that batch (and do it off screen).
if (visibleRef.current === false) return
generationRef.current += 1
forceNextLoad.current = true
}, [refreshTick])
@@ -1002,8 +1014,13 @@ export function FileTree(props: {
// Load the visible set in ONE batch request; already-loaded levels (kept
// in the cache) are not refetched, so this asks only for what is new — a
// refresh tick re-lists root + every expanded directory in one POST.
//
// A PARKED tree issues nothing: nobody can see the rows, and the watcher's
// re-arm re-lists the whole watched set once on the way back. Visibility is
// read through a ref, NOT a dependency — making it one would fire a second
// batch alongside that re-arm.
const root = cwd
if (root === undefined) return
if (root === undefined || visibleRef.current === false) return
const force = forceNextLoad.current
forceNextLoad.current = false
loadLevels([root, ...expanded], { force })
@@ -1035,13 +1052,13 @@ export function FileTree(props: {
// it was opened stayed stale for the rest of the session. A PARKED tree
// (the host keeps every visited tab body mounted) closes the socket — and
// with it the host's `fs.watch` handles — and re-lists the watched levels
// once when it comes back.
// once (ONE batch) when it comes back.
useDirectoryWatch({
sessionId,
root: cwd,
dirs: expanded,
visible: visible !== false,
onStale: retryDir,
onStale: retryDirs,
})
// Bring a "Show in folder" reveal into view: the ancestors expand above
+7 -1
View File
@@ -118,7 +118,13 @@ export function TreePanel(props: {
// refresh click. Re-focusing the window bumps the tick automatically, and
// integrations can force a refresh by dispatching a bubbling
// `dsh-sidebar:refresh-files` event on `window`.
//
// A PARKED panel (the host keeps every visited tab body mounted) listens to
// neither: a focus bump would force a full re-list of a tree nobody can see.
// It catches up through the directory watcher's one batched refresh when it
// becomes visible again (see FileTree's `visibleRef`).
useEffect(() => {
if (visible === false) return
const bump = (): void => { setRefreshTick(tick => tick + 1) }
window.addEventListener('focus', bump)
window.addEventListener('dsh-sidebar:refresh-files', bump)
@@ -126,7 +132,7 @@ export function TreePanel(props: {
window.removeEventListener('focus', bump)
window.removeEventListener('dsh-sidebar:refresh-files', bump)
}
}, [])
}, [visible])
/** One-line upload status under the search row ('' hides the hint). */
const [uploadStatus, setUploadStatus] = useState('')
/** Whether the status line is a failure/cancel (error color, stays visible). */
+11 -6
View File
@@ -47,10 +47,14 @@ export interface DirectoryWatchOptions {
*/
visible?: boolean
/**
* A directory whose listing is stale.
* @param dir - absolute directory path, exactly the form the tree keys levels by.
* Directories whose listings are stale.
*
* A watch notice names one directory. The parked→visible re-arm names the
* WHOLE watched set at once: the caller re-lists it in one batch request
* instead of firing one request per directory.
* @param dirs - absolute directory paths, exactly the form the tree keys levels by.
*/
onStale: (dir: string) => void
onStale: (dirs: readonly string[]) => void
}
/** How long to wait before re-opening a dropped socket; failures back off to the cap. */
@@ -146,7 +150,7 @@ export function useDirectoryWatch(options: DirectoryWatchOptions): void {
watching.add(frame.dir)
return
}
if (frame.ok === undefined) staleRef.current(frame.dir)
if (frame.ok === undefined) staleRef.current([frame.dir])
}
socket.onclose = () => {
if (closed) return
@@ -175,13 +179,14 @@ export function useDirectoryWatch(options: DirectoryWatchOptions): void {
* Re-armed after being parked: the socket is brand new (the old one's
* notices are gone with it), so nothing the host saw while the tree was
* hidden was delivered. Re-list every watched directory once instead of
* showing a listing that may be minutes old.
* showing a listing that may be minutes old — as ONE batch (the caller
* re-lists the set in a single `fs.trees` request), not one per directory.
*/
const wasArmedRef = useRef(armed)
useEffect(() => {
const previous = wasArmedRef.current
wasArmedRef.current = armed
if (!armed || previous) return
for (const dir of wantedRef.current) staleRef.current(dir)
if (wantedRef.current.length > 0) staleRef.current(wantedRef.current)
}, [armed])
}