Yet another POSIX signal handler.
Features
- Ensures provided functions are called before any other event listeners and are run concurrently, minimizing shutdown time.
- Works with any combination of synchronous and asynchronous functions.
- Ensures a clean exit if all functions resolve/return.
- Exits with an error if any functions reject/throw.
- Ensures processes exit cleanly, even when they have asynchronous shut-down functions and the Node debugger is in use. (See this issue)
Install
npm i @darkobits/adeiu
Use
import adeiu from '@darkobits/adeiu';
const annuler = adeiu(async signal => {
console.log(`Hey, we got ${signal}. Exiting...`);
await someAsyncStuff();
console.log('All done!');
});
annuler();
Advanced Usage
Usually, responding to signals dynamically can be accomplished by inspecting the signal
argument passed to your callback. However, if it is important that listeners are only installed on a particular signal, you may optionally provide a custom array of signals to assign a callback to.
import adeiu from '@darkobits/adeiu';
adeiu(() => {
}, ['SIGINT']);
import adeiu, {SIGNALS} from '@darkobits/adeiu';
adeiu(() => {
}, [...SIGNALS, 'SIGUSR1']);