In typescipt/nodejs, how can i gracefully shutdown a component that is repeatedly doing stuff. I.e., i want the user to be able to send SIGINT signal e.g. by hitting <ctrl+c> to stop the program gracefully:
export class Repeater {
private running: boolean;
constructor() {
this.running = false;
}
public do(): void {
this.running = true;
setTimeout(() => {
if (this.running) {
// do stuff
this.do();
}
}, 3000);
}
public stop(): void {
this.running = false;
}
}
Usage:
const repeater = new Repeater();
process.on("SIGINT", () => {
repeater.stop();
process.exit();
});
repeater.start();
I've started with the code above, however what is missing is the part where the call to stop() is actually blocking until the current execution of do() is finished?