Files
tspm/ts/cli/helpers/lifecycle.ts

23 lines
507 B
TypeScript

// Streaming lifecycle helper
export function withStreamingLifecycle(
setup: () => Promise<void>,
teardown: () => Promise<void>,
) {
let isCleaningUp = false;
const cleanup = async () => {
if (isCleaningUp) return;
isCleaningUp = true;
try {
await teardown();
} finally {
process.exit(0);
}
};
process.once('SIGINT', cleanup);
process.once('SIGTERM', cleanup);
return (async () => {
await setup();
await new Promise(() => {}); // keep alive
})();
}