index.js 444 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var cbs = [];
  3. var called = false;
  4. function exit(exit, signal) {
  5. if (called) {
  6. return;
  7. }
  8. called = true;
  9. cbs.forEach(function (el) {
  10. el();
  11. });
  12. if (exit === true) {
  13. process.exit(128 + signal);
  14. }
  15. };
  16. module.exports = function (cb) {
  17. cbs.push(cb);
  18. if (cbs.length === 1) {
  19. process.once('exit', exit);
  20. process.once('SIGINT', exit.bind(null, true, 2));
  21. process.once('SIGTERM', exit.bind(null, true, 15));
  22. }
  23. };