@ipp/cli
Version:
An image build orchestrator for the modern web
49 lines (48 loc) • 1.59 kB
JavaScript
;
/**
* Image Processing Pipeline - Copyright (c) Marcus Cemes
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInterruptHandler = exports.InterruptException = void 0;
const common_1 = require("@ipp/common");
class InterruptException extends common_1.Exception {
constructor(message) {
super(message);
this.name = "InterruptException";
}
}
exports.InterruptException = InterruptException;
/**
* Attaches a SIGINT handler to the process, and returns a promise tha rejects
* with an InterruptException when the handler is called.
*
* The handler may be unregistered using the returned destroy function, which resolves
* the promise and removes the signal handler.
*/
function createInterruptHandler() {
let rejectedBool = false;
let destroy = () => void 0;
const rejecter = new Promise((_, rej) => {
const handler = () => {
rej(new InterruptException("Received SIGINT"));
rejectedBool = true;
};
process.on("SIGINT", handler);
destroy = () => {
process.off("SIGINT", handler);
};
});
// Avoid unhandled rejections.
// Promise.race() will register a permanent catch() function anyway.
rejecter.catch(() => void 0);
const rejected = () => rejectedBool;
return {
destroy,
rejecter,
rejected,
};
}
exports.createInterruptHandler = createInterruptHandler;