Skip to content

Commit

Permalink
style: upgrade to new xo style
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapppi committed Aug 3, 2017
1 parent 4c3cf40 commit 25f33a9
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 476 deletions.
99 changes: 50 additions & 49 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
'use strict';

var hooks = [];
var errHooks = [];
var called = false;
var waitingFor = 0;
var asyncTimeoutMs = 10000;
const hooks = [];
const errHooks = [];
let called = false;
let waitingFor = 0;
let asyncTimeoutMs = 10000;

var events = {};
var filters = {};
const events = {};
const filters = {};

function exit(exit, code, err) {
// Only execute hooks once
if (called) {
return;
}
// Helper functions
let doExitDone = false;

called = true;
function doExit() {
if (doExitDone) {
return;
}
doExitDone = true;

// Run hooks
if (err) {
// Uncaught exception, run error hooks
errHooks.map(runHook.bind(null, 1, err));
if (exit === true) {
// All handlers should be called even if the exit-hook handler was registered first
process.nextTick(process.exit.bind(null, code));
}
}
hooks.map(runHook.bind(null, 0, null));

if (waitingFor) {
// Force exit after x ms (10000 by default), even if async hooks in progress
setTimeout(function () {
doExit();
}, asyncTimeoutMs);
} else {
// No asynchronous hooks, exit immediately
doExit();
// Async hook callback, decrements waiting counter
function stepTowardExit() {
process.nextTick(() => {
if (--waitingFor === 0) {
doExit();
}
});
}

// Runs a single hook
function runHook(syncArgCount, err, hook) {
// cannot perform async hooks in `exit` event
// Cannot perform async hooks in `exit` event
if (exit && hook.length > syncArgCount) {
// hook is async, expects a finish callback
// Hook is async, expects a finish callback
waitingFor++;

if (err) {
Expand All @@ -48,35 +48,36 @@ function exit(exit, code, err) {
return hook(stepTowardExit);
}

// hook is synchronous
// Hook is synchronous
if (err) {
// Pass error, calling uncaught exception handlers
return hook(err);
}
return hook();
}

// Async hook callback, decrements waiting counter
function stepTowardExit() {
process.nextTick(function () {
if (--waitingFor === 0) {
doExit();
}
});
// Only execute hooks once
if (called) {
return;
}

var doExitDone = false;
called = true;

function doExit() {
if (doExitDone) {
return;
}
doExitDone = true;
// Run hooks
if (err) {
// Uncaught exception, run error hooks
errHooks.map(runHook.bind(null, 1, err));
}
hooks.map(runHook.bind(null, 0, null));

if (exit === true) {
// All handlers should be called even if the exit-hook handler was registered first
process.nextTick(process.exit.bind(null, code));
}
if (waitingFor) {
// Force exit after x ms (10000 by default), even if async hooks in progress
setTimeout(() => {
doExit();
}, asyncTimeoutMs);
} else {
// No asynchronous hooks, exit immediately
doExit();
}
}

Expand All @@ -95,7 +96,7 @@ function add(hook) {
// PM2 Cluster shutdown message. Caught to support async handlers with pm2, needed because
// explicitly calling process.exit() doesn't trigger the beforeExit event, and the exit
// event cannot support async handlers, since the event loop is never called after it.
add.hookEvent('message', 0, function (msg) {
add.hookEvent('message', 0, function (msg) { // eslint-disable-line prefer-arrow-callback
if (msg !== 'shutdown') {
return true;
}
Expand All @@ -107,7 +108,7 @@ function add(hook) {
add.hookEvent = function (event, code, filter) {
events[event] = function () {
const eventFilters = filters[event];
for (var i = 0; i < eventFilters.length; i++) {
for (let i = 0; i < eventFilters.length; i++) {
if (eventFilters[i].apply(this, arguments)) {
return;
}
Expand All @@ -134,8 +135,8 @@ add.unhookEvent = function (event) {

// List hooked events
add.hookedEvents = function () {
var ret = [];
for (var name in events) {
const ret = [];
for (const name in events) {
if ({}.hasOwnProperty.call(events, name)) {
ret.push(name);
}
Expand Down
Loading

0 comments on commit 25f33a9

Please sign in to comment.