Skip to content

Instantly share code, notes, and snippets.

@dabroder
Last active February 20, 2018 13:13
Show Gist options
  • Save dabroder/dfdd48da6281bdbdd8c460e448ae109a to your computer and use it in GitHub Desktop.
Save dabroder/dfdd48da6281bdbdd8c460e448ae109a to your computer and use it in GitHub Desktop.
OnClose handler
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])
}
}
{
"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