You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
682 B
TypeScript
39 lines
682 B
TypeScript
2 years ago
|
export const onExit = (cb: () => void) => {
|
||
|
/*
|
||
|
const origCb = cb;
|
||
|
cb = () => {
|
||
|
origCb();
|
||
|
console.log("finished");
|
||
|
};
|
||
|
*/
|
||
|
const cbAndExit = () => {
|
||
|
cb();
|
||
|
process.exit();
|
||
|
};
|
||
|
|
||
|
const exitSignals = [
|
||
|
"SIGINT",
|
||
|
"SIGTERM",
|
||
|
"uncaughtException",
|
||
|
"SIGUSR1",
|
||
|
"SIGUSR2",
|
||
|
];
|
||
|
const cbSignals = ["beforeExit"];
|
||
|
|
||
|
for (const signal of exitSignals) {
|
||
|
process.on(signal, cbAndExit);
|
||
|
}
|
||
|
for (const signal of cbSignals) {
|
||
|
process.on(signal, cb);
|
||
|
}
|
||
|
|
||
|
return () => {
|
||
|
for (const signal of exitSignals) {
|
||
|
process.off(signal, cbAndExit);
|
||
|
}
|
||
|
for (const signal of cbSignals) {
|
||
|
process.off(signal, cb);
|
||
|
}
|
||
|
};
|
||
|
};
|