Skip to content

Commit

Permalink
Merge pull request #2 from mrbar42/fix-typing
Browse files Browse the repository at this point in the history
fix typescript typing, checkout npmignore, bump minor, rebuild
  • Loading branch information
mrbar42 authored Mar 9, 2018
2 parents 9f4bba7 + 2102d55 commit a481985
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 739 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ build/Release
# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules
.npmignore

## Directory-based project format:
.idea/
Expand Down Expand Up @@ -72,3 +71,4 @@ atlassian-ide-plugin.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
.DS_Store
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/
.gitignore
.npmignore
.travis.yml
LICENSE
tslint.json
package-lock.json
141 changes: 50 additions & 91 deletions es5/index.js
Original file line number Diff line number Diff line change
@@ -1,173 +1,135 @@
// +----------------------------------------------------------------------+
// | node-graceful v0.2.2 (https://github.com/mrbar42/node-graceful) |
// | node-graceful v0.3.0 (https://github.com/mrbar42/node-graceful) |
// | Graceful process exit manager. |
// |----------------------------------------------------------------------|
"use strict";

'use strict';
function Graceful() {
// options
this.exitOnDouble = true;
this.timeout = 30000;

// constants
this.DEADLY_SIGNALS = ["SIGTERM", "SIGINT", "SIGBREAK", "SIGHUP"];

this.DEADLY_SIGNALS = ['SIGTERM', 'SIGINT', 'SIGBREAK', 'SIGHUP'];
// state
this._listeners = Object.create(null);
this.isExiting = false;
}

Graceful.prototype.on = function (signal, listener, deadly) {
Graceful.prototype.on = function on(signal, listener, deadly) {
var _this = this;
this._registerSignal(signal);

this._listeners[signal].push(listener);

// add signal to deadly list
if (deadly && this.DEADLY_SIGNALS.indexOf(signal) === -1) {
this.DEADLY_SIGNALS.push(signal);
}

return function () {
return _this.off(signal, listener);
};
return function () { return _this.off(signal, listener); };
};

Graceful.prototype.off = function (signal, listener) {
if (!this._listeners[signal]) return;

Graceful.prototype.off = function off(signal, listener) {
if (!this._listeners[signal])
return;
// remove listener if exists
var index = this._listeners[signal].indexOf(listener);
if (index !== -1) this._listeners[signal].splice(index, 1);

if (index !== -1)
this._listeners[signal].splice(index, 1);
// clear master listener if no listeners left
if (!this._listeners[signal].length) {
this._unregisterSignal(signal);
}
};

Graceful.prototype.clear = function (signal) {
Graceful.prototype.clear = function clear(signal) {
var _this = this;
if (signal) {
delete this._listeners[signal];
this._unregisterSignal(signal);
} else {
Object.keys(this._listeners).forEach(function (sig) {
return _this.clear(signal);
});
}
else {
Object
.keys(this._listeners)
.forEach(function (sig) { return _this.clear(sig); });
}
};

Graceful.prototype.exit = function (code, signal) {
if (typeof code == "number") {
Graceful.prototype.exit = function exit(code, signal) {
if (typeof code === 'number') {
process.exitCode = code;
}

var simulatedSignal = signal || this.DEADLY_SIGNALS[0];

this._processSignal(simulatedSignal);
};

Graceful.prototype._registerSignal = function (signal) {
Graceful.prototype._registerSignal = function _registerSignal(signal) {
var _this = this;
if (this._listeners[signal]) return;

if (this._listeners[signal])
return;
this._listeners[signal] = [];

var handler = function (event) {
return _this._processSignal(signal, event);
};

var handler = function (event) { return _this._processSignal(signal, event); };
// handle special 'exit' event case
if (signal == "exit") {
this.DEADLY_SIGNALS.forEach(function (deadlySignal) {
return process.on(deadlySignal, handler);
});
} else {
if (signal === 'exit') {
this.DEADLY_SIGNALS.forEach(function (deadlySignal) { return process.on(deadlySignal, handler); });
}
else {
process.on(signal, handler);
}

// store handler on listeners array for future ref
this._listeners[signal].__handler__ = handler;
};

Graceful.prototype._unregisterSignal = function (signal) {
if (!this._listeners[signal]) return;

Graceful.prototype._unregisterSignal = function _unregisterSignal(signal) {
if (!this._listeners[signal])
return;
var handler = this._listeners[signal].__handler__;

// handle special 'exit' event case
if (signal == "exit") {
this.DEADLY_SIGNALS.forEach(function (deadlySignal) {
return process.removeListener(deadlySignal, handler);
});
} else {
if (signal === 'exit') {
this.DEADLY_SIGNALS.forEach(function (deadlySignal) { return process.removeListener(deadlySignal, handler); });
}
else {
process.removeListener(signal, handler);
}

delete this._listeners[signal];
};

Graceful.prototype._processSignal = function (signal, event) {
Graceful.prototype._processSignal = function _processSignal(signal, event) {
var _this = this;
var deadly = this.DEADLY_SIGNALS.indexOf(signal) != -1;
var deadly = this.DEADLY_SIGNALS.indexOf(signal) !== -1;
var listeners = this._listeners[signal] && this._listeners[signal].slice();
var exitListeners = this._listeners.exit && this._listeners.exit.slice();
var exitListeners = this._listeners['exit'] && this._listeners['exit'].slice();
var targetCount = listeners && listeners.length || 0;

// also include exit listeners if deadly
if (deadly && exitListeners) {
targetCount += exitListeners.length;
}

// this should never happen
if (!targetCount) {
return process.nextTick(function () {
return _this._killProcess();
});
return process.nextTick(function () { return _this._killProcess(); });
}

var quit = (function () {
var count = 0;
return function () {
count++;
if (count >= targetCount) {
if (deadly) _this._killProcess();
if (deadly)
_this._killProcess();
}
};
})();

// exec signal specific listeners
if (listeners) {
listeners.forEach(function (listener) {
return _this._invokeListener(listener, quit, event, signal);
});
listeners.forEach(function (listener) { return _this._invokeListener(listener, quit, event, signal); });
}


// also invoke exit listeners
if (deadly && exitListeners) {
if (this.isExiting) {
if (this.exitOnDouble) this._killProcess(true);
} else {
if (this.exitOnDouble)
this._killProcess(true);
}
else {
this.isExiting = true;
if (parseInt(this.timeout)) {
setTimeout(function () {
return _this._killProcess(true);
}, this.timeout);
if (Number(this.timeout)) {
setTimeout(function () { return _this._killProcess(true); }, this.timeout);
}
exitListeners.forEach(function (listener) {
return _this._invokeListener(listener, quit, event, signal);
});
exitListeners.forEach(function (listener) { return _this._invokeListener(listener, quit, event, signal); });
}
}
};

Graceful.prototype._killProcess = function (force) {
Graceful.prototype._killProcess = function _killProcess(force) {
process.exit(process.exitCode || (force ? 1 : 0));
};

Graceful.prototype._invokeListener = function (listener, quit, event, signal) {
Graceful.prototype._invokeListener = function _invokeListener(listener, quit, event, signal) {
var invoked = false;
// listener specific callback
var done = function () {
Expand All @@ -176,14 +138,11 @@ Graceful.prototype._invokeListener = function (listener, quit, event, signal) {
quit();
}
};

var retVal = listener(done, event, signal);
// allow returning a promise
if (retVal && typeof retVal.then === "function" && typeof retVal["catch"] === "function") {
if (retVal && typeof retVal.then === 'function' && typeof retVal["catch"] === 'function') {
retVal.then(done)["catch"](done);
}
};

var graceful = new Graceful();
module.exports = graceful;

2 changes: 2 additions & 0 deletions graceful.d.ts → index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export declare module Graceful {

function exit(code?: number | string, signal?: string): void;
}

export default Graceful
Loading

0 comments on commit a481985

Please sign in to comment.