Skip to content

Commit dd173f1

Browse files
committed
refactor: use es6 arrow functions, const, let
BREAKING CHANGES: `const` and `let` not supported on node < 6
1 parent 0062d2d commit dd173f1

File tree

7 files changed

+76
-80
lines changed

7 files changed

+76
-80
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ A queue with custom concurrency and time limits. Inspired by [caolan/async#queue
1111
```js
1212
const TimeQueue = require('timequeue');
1313

14-
function worker(arg1, arg2, callback) {
14+
const worker = (arg1, arg2, callback) => {
1515
someAsyncFunction(calculation(arg1, arg2), callback);
16-
}
16+
};
1717

1818
// create a queue with max 5 concurrency every second
19-
var q = new TimeQueue(worker, { concurrency: 5, every: 1000 });
19+
let q = new TimeQueue(worker, { concurrency: 5, every: 1000 });
2020

2121
// push tasks onto the queue
2222
q.push(42, 24);

lib/index.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module.exports = class TimeQueue extends EventEmitter {
77
* @extends {EventEmitter}
88
* @param {Function(..., Function(!Error, ...)} worker
99
* @param {Object} options
10-
* @param {Number} concurrency
11-
* @param {Number} time
10+
* @param {number} concurrency
11+
* @param {number} time
1212
*/
1313
constructor(worker, options) {
1414
super();
@@ -40,10 +40,10 @@ module.exports = class TimeQueue extends EventEmitter {
4040
/**
4141
* Pushes a task onto the queue.
4242
*
43-
* @param {Object} args...
43+
* @param {Object} ...args
4444
* @param {Function(!Error, ...)} callback
4545
*/
46-
push() {
46+
push(...args) {
4747
if (this.maxQueued === this.queued) {
4848
return;
4949
}
@@ -54,9 +54,9 @@ module.exports = class TimeQueue extends EventEmitter {
5454
if (this.intransit === this.concurrency) {
5555
this.emit('full');
5656
}
57-
this._process(arguments);
57+
this._process(args);
5858
} else {
59-
this._queue.push(arguments);
59+
this._queue.push(args);
6060
this.queued++;
6161
}
6262
}
@@ -65,15 +65,13 @@ module.exports = class TimeQueue extends EventEmitter {
6565
/**
6666
* Starts a task
6767
*
68-
* @param {Object} arguments
68+
* @param {Array.<Object>} args
6969
*/
7070
_process(args) {
71-
args = Array.prototype.slice.call(args);
72-
var callback = args.splice(this.worker.length - 1, 1)[0];
73-
var finished = false;
74-
75-
var every = ~~this.every;
76-
var timedOut;
71+
const callback = args.splice(this.worker.length - 1, 1)[0];
72+
let finished = false;
73+
let every = ~~this.every;
74+
let timedOut;
7775

7876
if (every) {
7977
timedOut = false;
@@ -91,26 +89,12 @@ module.exports = class TimeQueue extends EventEmitter {
9189
}
9290

9391
// If `timeout` option is set, set a timeout to check the task doesn't lag.
94-
var taskTimedOut = false;
95-
var callbackCalled = false;
96-
var timeout = ~~this.timeout;
97-
98-
if (timeout) {
99-
var tid = setTimeout(() => {
100-
var err = new Error('Task timed out');
101-
err.args = args;
102-
taskCallback(err);
103-
taskTimedOut = true;
104-
}, timeout);
105-
}
92+
let taskTimedOut = false;
93+
let callbackCalled = false;
94+
let timeout = ~~this.timeout;
95+
let tid;
10696

107-
// Add missing arguments.
108-
while (args.length < this.worker.length - 1) {
109-
args.push(undefined);
110-
}
111-
112-
var self = this;
113-
function taskCallback(err) {
97+
const taskCallback = (err, ...args) => {
11498
// If this task has timed out, and the callback is called again
11599
// from the worker, ignore it.
116100
if (!taskTimedOut) {
@@ -125,32 +109,45 @@ module.exports = class TimeQueue extends EventEmitter {
125109
}
126110
callbackCalled = true;
127111

128-
self.finished++;
129-
self.active--;
112+
this.finished++;
113+
this.active--;
130114

131115
if (typeof callback === 'function') {
132116
// If a callback was given with the task,
133117
// call it when the task is finished.
134-
callback.apply(null, arguments);
118+
callback(err, ...args);
135119

136120
} else if (err) {
137121
// Otherwise emit an `error` event if there was an error with the task.
138-
self.emit('error', err);
122+
this.emit('error', err);
139123
}
140124

141125
finished = true;
142126
if (timedOut) {
143-
self._next();
127+
this._next();
144128
}
129+
};
145130

131+
if (timeout) {
132+
tid = setTimeout(() => {
133+
const err = new Error('Task timed out');
134+
err.args = args;
135+
taskCallback(err);
136+
taskTimedOut = true;
137+
}, timeout);
138+
}
139+
140+
// Add missing arguments.
141+
while (args.length < this.worker.length - 1) {
142+
args.push(undefined);
146143
}
147144

148145
// Add custom callback to args.
149-
var args2 = args.slice();
146+
const args2 = args.slice();
150147
args2.push(taskCallback);
151148

152149
// Call the worker.
153-
this.worker.apply(null, args2);
150+
this.worker(...args2);
154151
}
155152

156153

@@ -160,10 +157,9 @@ module.exports = class TimeQueue extends EventEmitter {
160157
*/
161158
_next() {
162159
if (this.intransit <= this.concurrency && this._queue.length) {
163-
var task = this._queue.shift();
164160
this.queued--;
165161
this.active++;
166-
this._process(task);
162+
this._process(this._queue.shift());
167163

168164
if (this._queue.length === 0) {
169165
this.emit('empty');
@@ -176,7 +172,8 @@ module.exports = class TimeQueue extends EventEmitter {
176172

177173

178174
/**
179-
* Empties the queue and kills the timers. Active tasks will still be completed.
175+
* Empties the queue and kills the timers.
176+
* Active tasks will still be completed.
180177
*/
181178
die() {
182179
this._queue = [];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"sinon": "^6.0.1"
2929
},
3030
"engines": {
31-
"node": ">=4"
31+
"node": ">=6"
3232
},
3333
"license": "MIT"
3434
}

test/every-test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ const concurrency = 3;
77
const every = 10;
88
const jobs = 10;
99

10-
var clock;
11-
before(() => { clock = sinon.useFakeTimers(); });
12-
after(() => { clock.restore(); });
10+
let clock;
11+
before(() => clock = sinon.useFakeTimers());
12+
after(() => clock.restore());
1313

14-
function runTest(type, worker) {
14+
const runTest = (type, worker) => {
1515
describe('Create a queue with a time limit with ' + type, () => {
16-
var q = new TimeQueue(worker, { concurrency, every });
16+
const q = new TimeQueue(worker, { concurrency, every });
1717

1818
it('Amount of concurrent tasks are not executed over the time limit',
1919
(done) => {
20-
var tid, n = 0, m = 0, timedOut = true;
20+
let tid, n = 0, m = 0, timedOut = true;
2121

22-
function checkJobsFinished() {
23-
var diff = q.finished - n;
22+
const checkJobsFinished = () => {
23+
const diff = q.finished - n;
2424
assert(diff <= concurrency);
2525
n = q.finished;
2626
clearTimeout(tid);
@@ -37,34 +37,34 @@ function runTest(type, worker) {
3737
clock.tick(every);
3838
});
3939
}
40-
}
40+
};
4141

42-
function jobFinished() {
42+
const jobFinished = () => {
4343
if (++m === concurrency && timedOut) {
4444
checkJobsFinished();
4545
timedOut = false;
4646
}
47-
}
47+
};
4848

49-
for (var i = 0; i < jobs; i++) {
49+
for (let i = 0; i < jobs; i++) {
5050
q.push(i, jobFinished);
5151
}
5252
});
5353
});
54-
}
54+
};
5555

5656
runTest('immediate finish', (n, callback) => {
5757
process.nextTick(callback.bind(null, n));
5858
});
5959

6060
runTest('finishing time decreasing', (n, callback) => {
61-
var ms = n < concurrency ? every + 10 : 0;
61+
let ms = n < concurrency ? every + 10 : 0;
6262
setTimeout(callback.bind(null, n), ms);
6363
clock.tick(ms);
6464
});
6565

6666
runTest('random finishing time', (n, callback) => {
67-
var ms = Math.floor(Math.random() * every * 2);
67+
let ms = Math.floor(Math.random() * every * 2);
6868
setTimeout(callback.bind(null, n), ms);
6969
clock.tick(ms);
7070
});

test/main-test.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ const assert = require('assert');
33

44

55
describe('Create a queue and add to it', () => {
6-
var full = false;
7-
8-
var q = new TimeQueue(process.nextTick.bind(process), { concurrency: 3 });
6+
let full = false;
7+
const q = new TimeQueue(process.nextTick, { concurrency: 3 });
98

109
q.on('full', () => {
1110
full = true;
@@ -49,7 +48,7 @@ describe('Create a queue and add to it', () => {
4948
});
5049

5150
describe('With default options', () => {
52-
var q = new TimeQueue(process.nextTick.bind(process));
51+
const q = new TimeQueue(process.nextTick.bind(process));
5352
it('Has defaults set', () => {
5453
assert.equal(q.concurrency, 1);
5554
assert.equal(q.every, 0);
@@ -60,8 +59,8 @@ describe('Create a queue and add to it', () => {
6059

6160

6261
describe('Create a queue with variable number of arguments', () => {
63-
var lastA, lastB, lastC;
64-
var q = new TimeQueue((a, b, c, callback) => {
62+
let lastA, lastB, lastC;
63+
const q = new TimeQueue((a, b, c, callback) => {
6564
lastA = a;
6665
lastB = b;
6766
lastC = c;
@@ -120,7 +119,7 @@ describe('Create a queue with variable number of arguments', () => {
120119

121120

122121
describe('Create a queue with a worker that always errors', () => {
123-
var q = new TimeQueue((callback) => {
122+
const q = new TimeQueue((callback) => {
124123
process.nextTick(() => {
125124
callback(new Error('gotcha'));
126125
});
@@ -149,7 +148,7 @@ describe('Create a queue with a worker that always errors', () => {
149148

150149
describe('Create a queue with a callback called twice', () => {
151150
it('Throws an error', (done) => {
152-
var q = new TimeQueue((callback) => {
151+
const q = new TimeQueue((callback) => {
153152
assert.throws(() => {
154153
callback();
155154
callback();
@@ -162,8 +161,8 @@ describe('Create a queue with a callback called twice', () => {
162161

163162

164163
describe('Create a queue then call its `die` method', () => {
165-
var n = 0;
166-
var q = new TimeQueue((callback) => {
164+
let n = 0;
165+
const q = new TimeQueue((callback) => {
167166
n++;
168167
process.nextTick(callback);
169168
}, { concurrency: 3 });

test/maxQueued-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const assert = require('assert');
33

44

55
describe('Create a queue and add to it', () => {
6-
var n = 0;
7-
var q = new TimeQueue((callback) => {
6+
let n = 0;
7+
const q = new TimeQueue((callback) => {
88
n++;
99
process.nextTick(callback);
1010
}, {
@@ -13,7 +13,7 @@ describe('Create a queue and add to it', () => {
1313
});
1414

1515
it('Ignores tasks pushed after it is full', (done) => {
16-
for (var i = 0; i < 10; i++) {
16+
for (let i = 0; i < 10; i++) {
1717
q.push();
1818
}
1919

test/timeout-test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const sinon = require('sinon');
55

66
describe('Create a queue with a timeout', () => {
77
describe('With tasks that finish immediately', () => {
8-
var q = new TimeQueue(process.nextTick, { timeout: 1000 });
8+
const q = new TimeQueue(process.nextTick, { timeout: 1000 });
99

1010
it('Should execute tasks without problems', (done) => {
11-
for (var i = 0; i < 10; i++) {
11+
for (let i = 0; i < 10; i++) {
1212
q.push();
1313
}
1414

@@ -17,12 +17,12 @@ describe('Create a queue with a timeout', () => {
1717
});
1818

1919
describe('With tasks that lag', () => {
20-
var clock;
21-
before(() => { clock = sinon.useFakeTimers(); });
22-
after(() => { clock.restore(); });
20+
let clock;
21+
before(() => clock = sinon.useFakeTimers());
22+
after(() => clock.restore());
2323

2424
/* jshint unused: false */
25-
var q = new TimeQueue((a, b, callback) => {
25+
const q = new TimeQueue((a, b, callback) => {
2626
setTimeout(callback, 100);
2727
}, { timeout: 50 });
2828

0 commit comments

Comments
 (0)