@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
49 lines • 1.59 kB
JavaScript
/**
* Race a foreground command's cache deadline against exit and abort without
* introducing an observable gap between the final checks and promotion. The
* native session's `onExit` calls already-settled listeners, which makes the
* listener registration a linearization gate rather than a best-effort peek.
*/
export function createForegroundDetachGate(options) {
let cancelled = false;
let committed = false;
let timer;
let resolveDetached;
const detached = new Promise((resolve) => {
resolveDetached = resolve;
});
const clearDeadline = () => {
if (timer === undefined)
return;
clearTimeout(timer);
timer = undefined;
};
const linearize = () => {
if (cancelled || committed || options.runtime.exited)
return;
if (options.signal?.aborted)
return;
let exitedAtGate = false;
const unsubscribeExit = options.runtime.session.onExit(() => {
if (!committed)
exitedAtGate = true;
});
if (cancelled || options.runtime.exited || exitedAtGate || options.signal?.aborted) {
unsubscribeExit();
return;
}
committed = true;
options.removeAbortListener();
unsubscribeExit();
resolveDetached();
};
timer = setTimeout(() => queueMicrotask(linearize), options.delayMs);
return {
detached,
cancel() {
cancelled = true;
clearDeadline();
},
};
}
//# sourceMappingURL=foreground-detach.js.map