Skip to content

Commit

Permalink
fix typescript typing, checkout npmignore, bump minor, rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbar42 committed Mar 9, 2018
1 parent 9f4bba7 commit 480afdc
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 208 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
189 changes: 0 additions & 189 deletions es5/index.js
Original file line number Diff line number Diff line change
@@ -1,189 +0,0 @@
// +----------------------------------------------------------------------+
// | node-graceful v0.2.2 (https://github.com/mrbar42/node-graceful) |
// | Graceful process exit manager. |
// |----------------------------------------------------------------------|
"use strict";

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

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

// state
this._listeners = Object.create(null);
this.isExiting = false;
}

Graceful.prototype.on = function (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);
};
};

Graceful.prototype.off = function (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);

// clear master listener if no listeners left
if (!this._listeners[signal].length) {
this._unregisterSignal(signal);
}
};

Graceful.prototype.clear = function (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);
});
}
};

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

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

this._processSignal(simulatedSignal);
};

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

this._listeners[signal] = [];

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 {
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;

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 {
process.removeListener(signal, handler);
}

delete this._listeners[signal];
};

Graceful.prototype._processSignal = function (signal, event) {
var _this = this;
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 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();
});
}

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

// exec signal specific listeners
if (listeners) {
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 {
this.isExiting = true;
if (parseInt(this.timeout)) {
setTimeout(function () {
return _this._killProcess(true);
}, this.timeout);
}
exitListeners.forEach(function (listener) {
return _this._invokeListener(listener, quit, event, signal);
});
}
}
};

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

Graceful.prototype._invokeListener = function (listener, quit, event, signal) {
var invoked = false;
// listener specific callback
var done = function () {
if (!invoked) {
invoked = true;
quit();
}
};

var retVal = listener(done, event, signal);
// allow returning a promise
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
File renamed without changes.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "node-graceful",
"version": "0.2.4",
"version": "0.3.0",
"description": "Graceful process exit manager. allows waiting on multiple async services.",
"main": "graceful.js",
"main": "index",
"repository": {
"type": "git",
"url": "https://github.com/mrbar42/node-graceful.git"
Expand All @@ -20,8 +20,8 @@
"test:unit": "node tests/runner",
"test:lint": "tslint -c tslint.json --fix tests/*.ts",
"test": "npm run test:unit && npm run test:lint",
"transpile": "cat graceful.js | 6to5 > es5/index.js",
"prepublish": "npm run test && npm run transpile"
"transpile": "cat index.js | 6to5 > es5/index.js",
"prepublishOnly": "npm run test && npm run transpile"
},
"author": "mrbar42",
"license": "WTFPL",
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeScript.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Graceful} from "../graceful";
import {Graceful} from "../";

const listener = (done, event, signal) => {
done();
Expand Down
4 changes: 2 additions & 2 deletions tests/async-exit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

Graceful.on('exit', done => {
setTimeout(() => {
Expand All @@ -9,4 +9,4 @@ Graceful.on('exit', done => {
}, 100)
});

Graceful.exit();
Graceful.exit();
4 changes: 2 additions & 2 deletions tests/clear-listeners.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

let handler = done => {
process.stdout.write('should-not-run');
Expand All @@ -13,4 +13,4 @@ Graceful.off('exit', handler);
let removeListener = Graceful.on('exit', handler.bind(null));
removeListener();

Graceful.exit();
Graceful.exit();
2 changes: 1 addition & 1 deletion tests/exit-on-double.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

Graceful.on('exit', done => {
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/forced-exit.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

Graceful.timeout = 1000;

Expand All @@ -11,4 +11,4 @@ Graceful.on('exit', done => {
}, 2000);
});

Graceful.exit();
Graceful.exit();
4 changes: 2 additions & 2 deletions tests/multiple-listeners.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

let count = 0;
Graceful.on('exit', done => {
Expand All @@ -20,4 +20,4 @@ Graceful.on('exit', done => {
}, 200)
});

Graceful.exit('SIGINT');
Graceful.exit('SIGINT');
2 changes: 1 addition & 1 deletion tests/self-triggered.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

Graceful.on('exit', done => {
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions tests/wait-for-promise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Graceful = require('../graceful');
const Graceful = require('../');

let handlerSuccess = () => {
return new Promise(function (resolve) {
Expand All @@ -23,4 +23,4 @@ let handlerReject = () => {
Graceful.on('exit', handlerSuccess);
Graceful.on('exit', handlerReject);

Graceful.exit();
Graceful.exit();

0 comments on commit 480afdc

Please sign in to comment.