forked from Azure/login
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.js
More file actions
224 lines (209 loc) · 5.49 KB
/
Copy pathQueue.js
File metadata and controls
224 lines (209 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { _setImmediate } from './_setImmediate'
import PrioArray from './PrioArray'
/**
* Run queued `items` through an asynchronous `task`.
*
* Once finishing the `task` an optional callback is called.
* While pushing to the queue, you may define a priority for execution.
* Lower values means faster execution.
*
* @name Queue
* @methodOf: module:parallel
* @class
* @param {Function} task - iterator function of type `function (item: any, cb: Function, index: Number)`
* @param {Number} concurrency - max. number of tasks running in parallel
* @example <caption>Default usage</caption>
* var arr = []
* var q = new Queue((item, cb) => {
* arr.push(item)
* cb(null, item)
* })
* // push item "one" at end of queue
* q.push('one', (err, res) => {
* console.log(res + ' finished')
* })
* // add item "two" at start of queue
* q.unshift('two', () => {
* console.log('two finished')
* })
* // called when all items in queue where processed
* q.drain(() => {
* console.log(arr)
* //> arr = ['one', 'two']
* })
* @example <caption>Using priorities</caption>
* let arr = []
*
* let q = new Queue(function (item, cb) {
* arr.push(item)
* cb()
* }, 2)
*
* q.concat([100, 101, 102], 3) // priority = 3 - last (but 2 items already processed)
* q.concat([0, 1, 2], 1) // priority = 1 - first
* q.concat([10, 11, 12], 2) // priority = 2 - second
*
* q.drain(() => {
* //> arr = [ 100, 101, 0, 1, 2, 10, 11, 12, 102 ])
* })
*/
export default function Queue (task, concurrency) {
this._task = task
this._concurrency = Math.abs(concurrency || 1)
this._worker = 0
this._paused = false
this._items = new PrioArray()
}
Queue.prototype = {
/**
* process items in queue
* @private
*/
_run () {
const { _items, _drain } = this
this._worker -= 1
if (_items.length === 0) {
if (this._worker <= 0) {
this._worker = 0
_drain && _drain()
}
} else {
this._worker += 1
const [item, cb] = _items.shift()
this._task(item, (err, res) => {
cb && cb(err, res)
_setImmediate(() => { // prevent RangeError: Maximum call stack size exceeded for sync tasks
this._run()
})
})
}
},
/**
* start processing queue or add workers up to concurrency
* @private
*/
_start () {
while (!this._paused && this._worker < Math.min(this._concurrency, this._items.length)) {
this._worker += 1
this._run()
}
return this
},
/**
* Check if queue is paused
* @return {Boolean} `true` if paused
*/
get paused () {
return this._paused
},
/**
* Check if queue is idle - means no items in queue and no workers running
* @return {Boolean} `true` if idle
*/
get idle () {
return !this.length && this._worker === 0
},
/**
* Number of items waiting in the queue to get processed
* @return {Number} number of items in queue
*/
get length () {
return this._items.length
},
/**
* Pause processing
* @return {this} for chaining
*/
pause () {
this._paused = true
return this
},
/**
* Resume processing
* @return {this} for chaining
*/
resume () {
this._paused = false
return this._start()
},
/**
* Reset the queue by removing all pending items from the queue
* @return {this} for chaining
*/
reset () {
this._items.reset()
return this
},
/**
* Number of items being processed
* @return {Number} number of items processed
*/
running () {
return this._worker
},
/**
* push `item` onto queue
* @param {Any} item
* @param {Function} [callback] - optional callback if item was processed
* @param {Number} [priority] - priority `0 ... Infinity` of the item to process. Smaller values, faster processing
* @return {this} for chaining
*/
push (item, callback, priority) {
return this.concat([item], callback, priority)
},
/**
* concat `items` onto queue - fills the queue first with `items` before starting processing
* @param {Any[]} items
* @param {Function} [callback] - optional callback if single item was processed
* @param {Number} [priority] - priority `0 ... Infinity` of the item to process. Smaller values, faster processing
* @return {this} for chaining
*/
concat (items, callback, priority) {
if (typeof callback === 'number') {
priority = callback
callback = undefined
}
items.forEach((item) => {
this._items.push([item, callback], priority)
})
return this._start()
},
/**
* put `item` at the very beginnning of the queue
* @param {Any} item
* @param {Function} [callback] - optional callback if item was processed
* @return {this} for chaining
*/
unshift (item, callback) {
this._items.unshift([item, callback])
return this._start()
},
/**
* @param {Function} [callback] - optional callback called if all queue items got processed
* @return {this} for chaining
*/
drain (callback) {
this._drain = callback
return this
}
}
/**
* Run queued `items` through an asynchronous `task`.
*
* Once finishing the `task` an optional callback is called.
* While pushing to the queue, you may define a priority for execution.
* Lower values means faster execution.
*
* See full API here {@link Queue}.
*
* @name queue
* @memberOf module:parallel
* @static
* @method
* @param {Function} task - iterator function of type `function (item: any, cb: Function, index: Number)`
* @param {Number} concurrency - max. number of tasks running in parallel
* @return {Queue}
*/
export function queue (task, concurrency) {
return new Queue(task, concurrency)
}