@@ -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 = [ ] ;
0 commit comments