Last active
February 20, 2018 13:13
-
-
Save dabroder/dfdd48da6281bdbdd8c460e448ae109a to your computer and use it in GitHub Desktop.
OnClose handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const events = ['uncaughtException', 'SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGUSR2'] | |
module.exports = function onClose (cb) { | |
let called = false | |
const handlers = {} | |
events.forEach(ev => { | |
const handler = (...arg) => { | |
if (called) return | |
called = true | |
cb(ev, ...arg) | |
} | |
process.once(ev, handler) | |
handlers[ev] = handler | |
}) | |
return function offOnClose () { | |
for (const ev in handlers) process.removeListener(ev, handlers[ev]) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "on-close", | |
"version": "0.1.0", | |
"main": "onClose.js" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment