refactor(safety): 两个换会话计数器合一,resolveEntry 去掉 async 空壳
FileTree 的 batchGenRef 与 archiveGenRef 用途相同、触发条件相同、生命周期 相同(都由 sessionId/cwd 切换作废),合并成一个 scopeGenRef,重复的说明也 收敛成一份。 resolveEntry 在丢掉 real 字段后已是纯同步的两次调用,去掉 async 外壳与三处 await,不再读起来像碰了文件系统。
This commit is contained in:
1 parent
28102176b8
commit
8a3bd0f9e3
2 files changed
+33
-43
No files matched your search
+27
-35
@@ -980,6 +980,17 @@ export function FileTree(props: {
|
||||
setSelection(new Set())
|
||||
}, [setSelection])
|
||||
|
||||
/**
|
||||
* Bumped by the swap effect below, and the boundary it marks is shared: the
|
||||
* two asynchronous paths this component starts — the batch-delete walk and
|
||||
* the archive build — capture their OWN scope, so the requests they issue
|
||||
* stay correct across a swap, while everything they SETTLE (pruneTree, the
|
||||
* error strip, the selection, the busy flags, the archive job) is state of
|
||||
* the project on screen, which may no longer be the one they started in.
|
||||
* Work whose generation is stale goes quiet instead.
|
||||
*/
|
||||
const scopeGenRef = useRef(0)
|
||||
|
||||
/**
|
||||
* A session/cwd swap REUSES this component: the workbench keeps one mounted
|
||||
* instance per tab id, and tab ids restart per session. Every path-shaped
|
||||
@@ -1010,12 +1021,11 @@ export function FileTree(props: {
|
||||
newFolderRef.current = null
|
||||
setConfirmDelete(null)
|
||||
setConfirmDeleteSelected(false)
|
||||
// A batch walk of the PREVIOUS session may still be in flight. Its
|
||||
// removals carry their own scope, so they stay correct — but the walk must
|
||||
// not settle over THIS session (see batchGenRef), and its flag must not
|
||||
// lock the new session's bar either.
|
||||
// A batch walk of the PREVIOUS session may still be in flight: its flag
|
||||
// must not lock the new session's bar, and the bump below retires the
|
||||
// state it would otherwise settle here.
|
||||
setDeletingSelected(false)
|
||||
batchGenRef.current += 1
|
||||
scopeGenRef.current += 1
|
||||
setActionError(null)
|
||||
setLoadError(null)
|
||||
pendingUploadDir.current = undefined
|
||||
@@ -1140,14 +1150,6 @@ export function FileTree(props: {
|
||||
}
|
||||
|
||||
// ── Batch delete ───────────────────────────────────────────────────────
|
||||
/**
|
||||
* Bumped by a session/cwd swap. The walk below carries its OWN scope, so the
|
||||
* removals it issues stay correct — but everything it SETTLES (pruneTree, the
|
||||
* error strip, the selection, the busy flag) is state of the project on
|
||||
* screen, which may no longer be the one it started in. A walk whose
|
||||
* generation is stale goes quiet instead.
|
||||
*/
|
||||
const batchGenRef = useRef(0)
|
||||
/**
|
||||
* Delete every selected row, ONE AT A TIME (the host refuses nothing here,
|
||||
* but a partial batch must be debuggable). The first failure stops the walk
|
||||
@@ -1160,7 +1162,7 @@ export function FileTree(props: {
|
||||
setConfirmDeleteSelected(false)
|
||||
if (paths.length === 0) return
|
||||
const scope = { sessionId: live.sessionId, cwd: live.cwd }
|
||||
const generation = batchGenRef.current
|
||||
const generation = scopeGenRef.current
|
||||
setDeletingSelected(true)
|
||||
void (async () => {
|
||||
const removed: string[] = []
|
||||
@@ -1168,7 +1170,7 @@ export function FileTree(props: {
|
||||
try {
|
||||
await api.fsRemove(scope, path)
|
||||
} catch (error: unknown) {
|
||||
if (generation !== batchGenRef.current) return
|
||||
if (generation !== scopeGenRef.current) return
|
||||
setActionError(error instanceof Error ? error.message : String(error))
|
||||
// Keep the rows that were NOT removed selected, so a retry is one click.
|
||||
const next = new Set(selectedRef.current)
|
||||
@@ -1177,16 +1179,14 @@ export function FileTree(props: {
|
||||
setDeletingSelected(false)
|
||||
return
|
||||
}
|
||||
// The reader moved on mid-walk: drop the rest of the batch rather than
|
||||
// settle it into the project that took over. The swap already released
|
||||
// the busy flag, so nothing here needs to.
|
||||
if (generation !== batchGenRef.current) return
|
||||
// Stale walk: the swap already released the busy flag.
|
||||
if (generation !== scopeGenRef.current) return
|
||||
setActionError(null)
|
||||
pruneTree(path)
|
||||
live.onPathDeleted?.(path, kindRef.current.get(path) ?? false)
|
||||
removed.push(path)
|
||||
}
|
||||
if (generation !== batchGenRef.current) return
|
||||
if (generation !== scopeGenRef.current) return
|
||||
setDeletingSelected(false)
|
||||
clearSelection()
|
||||
})()
|
||||
@@ -1484,13 +1484,6 @@ export function FileTree(props: {
|
||||
const [archiveBusy, setArchiveBusy] = useState(false)
|
||||
const [archiveProgress, setArchiveProgress] = useState<{ done: number; total: number } | null>(null)
|
||||
const archiveBusyRef = useRef(false)
|
||||
/**
|
||||
* Bumped by a session/cwd swap. A build still starting up when the reader
|
||||
* leaves must not pump its job back in: the swap already settled the archive,
|
||||
* and the resurrected id would restart the poller in the new project — asking
|
||||
* with the wrong scope, then reporting a zip failure nobody asked for.
|
||||
*/
|
||||
const archiveGenRef = useRef(0)
|
||||
/** The job being polled (state so the poller starts/stops with it). */
|
||||
const [archiveJobId, setArchiveJobId] = useState<string | null>(null)
|
||||
const archiveJobRef = useRef<{ id: string; name: string } | null>(null)
|
||||
@@ -1510,13 +1503,12 @@ export function FileTree(props: {
|
||||
setArchiveProgress(null)
|
||||
}, [])
|
||||
|
||||
// A session/cwd swap abandons the archive job with it: the progress strip
|
||||
// A session/cwd swap abandons the archive job with it — the progress strip
|
||||
// belongs to the project that started the zip, and a leftover `archiveJobId`
|
||||
// would keep the poller running — whose failures land as `zipFailed` in a
|
||||
// project that never asked for one. The generation bump retires a build that
|
||||
// is still starting up, so it cannot pump its job back in after this.
|
||||
// would keep the poller running in the new one, whose failures land as
|
||||
// `zipFailed` there. A build still starting up is retired by the swap effect
|
||||
// above (scopeGenRef), so it cannot pump its job back in after this.
|
||||
useEffect(() => {
|
||||
archiveGenRef.current += 1
|
||||
settleArchive()
|
||||
}, [sessionId, cwd, settleArchive])
|
||||
|
||||
@@ -1596,20 +1588,20 @@ export function FileTree(props: {
|
||||
const downloadArchive = (paths: readonly string[]): void => {
|
||||
if (archiveBusyRef.current) return
|
||||
const name = paths.length === 1 ? `${baseName(paths[0]!)}.zip` : 'archive.zip'
|
||||
const generation = archiveGenRef.current
|
||||
const generation = scopeGenRef.current
|
||||
archiveBusyRef.current = true
|
||||
archiveHandedOffRef.current = false
|
||||
setArchiveBusy(true)
|
||||
setArchiveProgress(null)
|
||||
void archiveBuild({ sessionId, cwd }, paths, name)
|
||||
.then(({ id, entries }) => {
|
||||
if (generation !== archiveGenRef.current) return
|
||||
if (generation !== scopeGenRef.current) return
|
||||
archiveJobRef.current = { id, name }
|
||||
setArchiveProgress({ done: 0, total: entries })
|
||||
setArchiveJobId(id)
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
if (generation !== archiveGenRef.current) return
|
||||
if (generation !== scopeGenRef.current) return
|
||||
failArchive(error instanceof Error ? error.message : String(error))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -127,13 +127,11 @@ export interface WorkspaceRenameInput {
|
||||
* No realpath, no containment: the path exists (lstat decides) and the
|
||||
* operation addresses it as written.
|
||||
*/
|
||||
async function resolveEntry(
|
||||
function resolveEntry(
|
||||
cwd: string,
|
||||
target: string,
|
||||
): Promise<{ absolute: string; realCwd: string }> {
|
||||
const absolute = resolveTarget(cwd, target)
|
||||
const realCwd = requireAbsolute(cwd)
|
||||
return { absolute, realCwd }
|
||||
): { absolute: string; realCwd: string } {
|
||||
return { absolute: resolveTarget(cwd, target), realCwd: requireAbsolute(cwd) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -211,7 +209,7 @@ export async function renameWorkspaceEntry(input: WorkspaceRenameInput): Promise
|
||||
if (name === '' || name === '.' || name === '..' || name.includes('/') || name.includes('\\')) {
|
||||
throw new SidebarError('bad-request', 'name must be a single path segment', 400)
|
||||
}
|
||||
const { absolute, realCwd } = await resolveEntry(cwd, path)
|
||||
const { absolute, realCwd } = resolveEntry(cwd, path)
|
||||
if (await isWorkspaceRoot(absolute, realCwd)) {
|
||||
throw new SidebarError('fs-error', 'cannot rename the workspace root', 400)
|
||||
}
|
||||
@@ -256,7 +254,7 @@ export async function mkdirWorkspaceEntry(input: WorkspaceMkdirInput): Promise<{
|
||||
if (name === '' || name === '.' || name === '..' || name.includes('/') || name.includes('\\')) {
|
||||
throw new SidebarError('bad-request', 'name must be a single path segment', 400)
|
||||
}
|
||||
const { absolute } = await resolveEntry(cwd, path)
|
||||
const { absolute } = resolveEntry(cwd, path)
|
||||
const destination = await ensureWorkspaceWritePath(cwd, join(absolute, name))
|
||||
if (await pathExists(destination)) {
|
||||
throw new SidebarError('fs-error', `"${name}" already exists`, 409)
|
||||
@@ -291,7 +289,7 @@ export interface WorkspaceRemoveInput {
|
||||
*/
|
||||
export async function removeWorkspaceEntry(input: WorkspaceRemoveInput): Promise<{ path: string }> {
|
||||
const { cwd, path } = input
|
||||
const { absolute, realCwd } = await resolveEntry(cwd, path)
|
||||
const { absolute, realCwd } = resolveEntry(cwd, path)
|
||||
if (await isWorkspaceRoot(absolute, realCwd)) {
|
||||
throw new SidebarError('fs-error', 'cannot remove the workspace root', 400)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user