Skip to content

Commit c0f01ad

Browse files
committed
Create smaller modules for each functionality
1 parent 31b970f commit c0f01ad

3 files changed

Lines changed: 52 additions & 23 deletions

File tree

lib/options.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Options(options) {
2+
options = options || {};
3+
4+
this._defaultOptions = {
5+
replaceNodeKill: true,
6+
warmUp: false
7+
};
8+
9+
this._options = Object.assign({}, this._defaultOptions, options);
10+
};
11+
12+
Options.prototype.get = function (id) {
13+
if (this._options.hasOwnProperty(id)) {
14+
return this._options[id];
15+
}
16+
17+
return null;
18+
}
19+
20+
module.exports = Options;

lib/signals.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Signals() {
2+
this._supportedSignals = {
3+
'SIGBREAK': 1,
4+
'SIGINT': 2
5+
}
6+
}
7+
8+
Signals.prototype.isSupported = function (signal) {
9+
return this._supportedSignals.hasOwnProperty(signal);
10+
}
11+
12+
Signals.prototype.idToNumber = function (signal) {
13+
if (this._supportedSignals.hasOwnProperty(signal)) {
14+
return this._supportedSignals[signal];
15+
}
16+
17+
return null;
18+
}
19+
20+
module.exports = Signals;

lib/windows-kill-class.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
1+
var path = require('path');
2+
13
var binary = require('node-pre-gyp');
2-
var path = require('path')
3-
var bindingPath = binary.find(path.resolve(path.join(__dirname, '../package.json')));
44

5-
var supportedSignals = [
6-
'SIGBREAK',
7-
'SIGINT'
8-
];
5+
var bindingPath = binary.find(path.resolve(path.join(__dirname, '../package.json')));
96

10-
var supportedSignalsAsNumber = {
11-
'SIGBREAK': 1,
12-
'SIGINT': 2
13-
}
7+
var Options = require('./options');
8+
var Signals = require('./signals');
149

1510
function WindowsKillClass(options) {
1611
this._native = require(bindingPath);
1712
this._nodeKill = process.kill;
1813

19-
this._shouldReplaceNodesKill = true;
20-
if (options && options.hasOwnProperty('replaceNodeKill') && (options.replaceNodeKill === false || options.replaceNodeKill === true)) {
21-
this._shouldReplaceNodesKill = options.replaceNodeKill;
22-
}
23-
24-
this._shouldWarmUp = false;
25-
if (options && options.hasOwnProperty('warmUp') && (options.warmUp === false || options.warmUp === true)) {
26-
this._shouldWarmUp = options.warmUp;
27-
}
14+
this._options = new Options(options);
15+
this._signals = new Signals();
2816

2917
this._applyOptions();
3018
};
3119

3220
WindowsKillClass.prototype._applyOptions = function () {
33-
if (this._shouldReplaceNodesKill) {
21+
if (this._options.get('replaceNodeKill') === true) {
3422
this._replaceNodeKill();
3523
}
3624

37-
if (this._shouldWarmUp) {
25+
if (this._options.get('warmUp') === true) {
3826
this._native.warmUp();
3927
}
4028
}
@@ -47,12 +35,13 @@ WindowsKillClass.prototype._replaceNodeKill = function () {
4735
}
4836

4937
WindowsKillClass.prototype.kill = function (pid, signal) {
50-
if (supportedSignals.indexOf(signal) >= 0) {
38+
if (this._signals.isSupported(signal)) {
5139
if (pid != (pid | 0)) {
5240
throw new TypeError('invalid pid.');
5341
}
5442

55-
error = this._native.send(pid, supportedSignalsAsNumber[signal]);
43+
error = this._native.send(pid, this._signals.idToNumber(signal));
44+
5645
if (error) {
5746
throw new Error('windows-kill ' + error);
5847
}

0 commit comments

Comments
 (0)