Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
105 lines
3.6 KiB
TypeScript
105 lines
3.6 KiB
TypeScript
import { useCallback, useEffect, useRef, type MutableRefObject } from "react";
|
|
|
|
export async function cleanupFailedExternalOpenTemp(
|
|
bridge: Pick<NetcattyBridge, "unregisterTempFile" | "deleteTempFile">,
|
|
sftpId: string,
|
|
localPath: string,
|
|
): Promise<void> {
|
|
if (!localPath) return;
|
|
if (bridge.unregisterTempFile) {
|
|
await bridge.unregisterTempFile(sftpId, localPath);
|
|
return;
|
|
}
|
|
await bridge.deleteTempFile?.(localPath);
|
|
}
|
|
|
|
export type StopExternalFileWatch = (
|
|
watchId: string,
|
|
cleanupTempFile: boolean,
|
|
) => void | Promise<unknown>;
|
|
|
|
export type SubscribeExternalFileWatchStopped = (
|
|
callback: (payload: {
|
|
watchId: string;
|
|
localPath?: string;
|
|
remotePath?: string;
|
|
sftpId?: string;
|
|
}) => void,
|
|
) => (() => void) | void;
|
|
|
|
export interface ExternalFileWatchLifecycle {
|
|
activeCountRef: MutableRefObject<number>;
|
|
captureGeneration(): number;
|
|
remember(watchId: string | undefined, generation?: number): void;
|
|
releaseAll(cleanupTempFiles?: boolean): Promise<void>;
|
|
}
|
|
|
|
export function useExternalFileWatchLifecycle(
|
|
stopWatch: StopExternalFileWatch,
|
|
subscribeStopped?: SubscribeExternalFileWatchStopped,
|
|
onStoppedLocalPath?: (localPath: string) => void,
|
|
): ExternalFileWatchLifecycle {
|
|
const watchIdsRef = useRef<Set<string>>(new Set());
|
|
const activeCountRef = useRef(0);
|
|
const stopWatchRef = useRef(stopWatch);
|
|
const subscribeStoppedRef = useRef(subscribeStopped);
|
|
const onStoppedLocalPathRef = useRef(onStoppedLocalPath);
|
|
const disposedRef = useRef(false);
|
|
const generationRef = useRef(0);
|
|
const invalidatedCleanupTempFilesRef = useRef(false);
|
|
stopWatchRef.current = stopWatch;
|
|
subscribeStoppedRef.current = subscribeStopped;
|
|
onStoppedLocalPathRef.current = onStoppedLocalPath;
|
|
|
|
const captureGeneration = useCallback(() => generationRef.current, []);
|
|
|
|
const remember = useCallback((watchId: string | undefined, generation = generationRef.current) => {
|
|
if (!watchId) return;
|
|
if (disposedRef.current || generation !== generationRef.current) {
|
|
// startFileWatch may resolve after the owning React tree unmounts. There
|
|
// may also have been an explicit disconnect cleanup while IPC was pending.
|
|
// There will be no later cleanup for the old generation, so release now.
|
|
const cleanupTempFile = disposedRef.current
|
|
? false
|
|
: invalidatedCleanupTempFilesRef.current;
|
|
void Promise.resolve()
|
|
.then(() => stopWatchRef.current(watchId, cleanupTempFile))
|
|
.catch(() => {});
|
|
return;
|
|
}
|
|
watchIdsRef.current.add(watchId);
|
|
activeCountRef.current = watchIdsRef.current.size;
|
|
}, []);
|
|
|
|
const releaseAll = useCallback(async (cleanupTempFiles = false) => {
|
|
invalidatedCleanupTempFilesRef.current = cleanupTempFiles;
|
|
generationRef.current += 1;
|
|
const watchIds = [...watchIdsRef.current];
|
|
watchIdsRef.current.clear();
|
|
activeCountRef.current = 0;
|
|
await Promise.all(watchIds.map(async (watchId) => {
|
|
try {
|
|
await stopWatchRef.current(watchId, cleanupTempFiles);
|
|
} catch {
|
|
// The owning SFTP session or worker may already have released it.
|
|
}
|
|
}));
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
disposedRef.current = false;
|
|
return () => {
|
|
disposedRef.current = true;
|
|
void releaseAll(false);
|
|
};
|
|
}, [releaseAll]);
|
|
|
|
useEffect(() => subscribeStoppedRef.current?.(({ watchId, localPath }) => {
|
|
if (!watchId || !watchIdsRef.current.delete(watchId)) return;
|
|
activeCountRef.current = watchIdsRef.current.size;
|
|
if (localPath) onStoppedLocalPathRef.current?.(localPath);
|
|
}), []);
|
|
|
|
return { activeCountRef, captureGeneration, remember, releaseAll };
|
|
}
|