mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
Track pending starts, pre-claim children, and teardown removed from routing. Bound cleanup and cancel setup/update waits while preserving settled remote descriptors. Co-authored-by: Gille <[email protected]> Co-authored-by: ChanPark03 <[email protected]>
24 lines
663 B
TypeScript
24 lines
663 B
TypeScript
/** Cancel a startup wait, and check ownership before invoking its continuation. */
|
|
export async function runBackendStartStep<T>(signal: AbortSignal | undefined, run: () => T | Promise<T>): Promise<T> {
|
|
signal?.throwIfAborted()
|
|
|
|
if (!signal) {
|
|
return run()
|
|
}
|
|
|
|
let onAbort: () => void = () => {}
|
|
|
|
const cancelled = new Promise<never>((_resolve, reject) => {
|
|
onAbort = () => reject(signal.reason)
|
|
signal.addEventListener('abort', onAbort, { once: true })
|
|
})
|
|
|
|
try {
|
|
const result = await Promise.race([run(), cancelled])
|
|
signal.throwIfAborted()
|
|
|
|
return result
|
|
} finally {
|
|
signal.removeEventListener('abort', onAbort)
|
|
}
|
|
}
|