nginx-testing
Version:
Support for integration/acceptance testing of nginx configuration.
39 lines • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCleanup = void 0;
const logger_1 = require("../logger");
/**
* Returns a tuple of two functions:
*
* 1. **onCleanup**: Queues the given function to be run by `cleanup()`.
* It may be called multiple times; the functions are added to a LIFO
* queue, i.e. they will be called in reverse order.
* 2. **cleanup**: Runs the functions given to `onCleanup()`.
*/
function useCleanup({ registerExitHook } = {}) {
const funcs = [];
const cleanup = async () => {
let fn;
while ((fn = funcs.pop())) {
try {
const res = fn();
if (res && 'then' in res) {
await res;
}
}
catch (err) {
logger_1.log.error(err);
}
}
if (registerExitHook) {
process.removeListener('exit', cleanup);
}
};
const onCleanup = funcs.push.bind(funcs);
if (registerExitHook) {
process.once('exit', cleanup);
}
return [onCleanup, cleanup];
}
exports.useCleanup = useCleanup;
//# sourceMappingURL=useCleanup.js.map