forked from Gitlawb/openclaude
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanupRegistry.ts
More file actions
47 lines (40 loc) · 1.59 KB
/
cleanupRegistry.ts
File metadata and controls
47 lines (40 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Global registry for cleanup functions that should run during graceful shutdown.
* This module is separate from gracefulShutdown.ts to avoid circular dependencies.
*/
// Global registry for cleanup functions
const cleanupFunctions = new Set<() => Promise<void>>()
/**
* Register a cleanup function to run during graceful shutdown.
* @param cleanupFn - Function to run during cleanup (can be sync or async)
* @returns Unregister function that removes the cleanup handler
*/
export function registerCleanup(cleanupFn: () => Promise<void>): () => void {
cleanupFunctions.add(cleanupFn)
return () => cleanupFunctions.delete(cleanupFn) // Return unregister function
}
/**
* Run all registered cleanup functions.
* Used internally by gracefulShutdown.
*/
export async function runCleanupFunctions(): Promise<void> {
await Promise.all(Array.from(cleanupFunctions).map(fn => fn()))
}
// --- Cleanup-safe timer and AbortController helpers ---
export function setCleanupTimeout(fn: (...args: any[]) => void, ms: number): { id: ReturnType<typeof setTimeout>; unregister: () => void } {
const id = setTimeout(fn, ms)
const unregister = registerCleanup(() => clearTimeout(id))
return { id, unregister }
}
export function setCleanupInterval(fn: (...args: any[]) => void, ms: number): ReturnType<typeof setInterval> {
const id = setInterval(fn, ms)
registerCleanup(() => clearInterval(id))
return id
}
export function createCleanupAbortController(): AbortController {
const controller = new AbortController()
registerCleanup(() => {
try { controller.abort() } catch {}
})
return controller
}