-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.js
More file actions
71 lines (67 loc) · 1.82 KB
/
queue.js
File metadata and controls
71 lines (67 loc) · 1.82 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
const EventEmitter = require('events')
const promiseFinally = require('promise.prototype.finally')
promiseFinally.shim()
class Queue extends EventEmitter
{
constructor(concurrency = 1) {
super()
this.max = concurrency
this.active = 0
this.pending = []
this.results = []
this.completedIndex = 0
this.nextIndex = 0
this.waiter = null
this.finaldata = undefined
}
wait() {
if (this.waiter) { return this.waiter }
return new Promise((resolve, reject) => {
this.waiter={'resolve':resolve, 'reject':reject}
})
}
add(asyncfunc, ...data) {
this.pending.push({'func': asyncfunc, 'index': this.nextIndex, 'data': data})
++ this.nextIndex
this._checkpending()
}
stop(resolveorreject, data) {
this.nextIndex -= this.pending.length
this.pending = []
this.finaldata = data
if (resolveorreject == 'reject') {
this.waiter.resolve = this.waiter.reject
}
}
_checkpending() {
if (this.active >= this.max) { return }
if (this.pending.length == 0) { return }
++ this.active
let next = this.pending.shift()
promiseFinally(next.func(...next.data)).then(x => {
this.results[next.index - this.completedIndex] = { 'status': 'resolve', 'data': x }
}).catch(x => {
this.results[next.index - this.completedIndex] = { 'status': 'reject', 'data': x }
}).finally(() => {
-- this.active
this._checkpending()
if (next.index == this.completedIndex) {
while(this.results[0] !== undefined) {
let data = this.results.shift()
this.completedIndex ++
this.emit(data.status, data.data)
this.emit('any', data)
if (data.status == 'reject') {
this.waiter.resolve = this.waiter.reject
}
}
if (this.completedIndex == this.nextIndex) {
this.completedIndex = 0
this.nextIndex = 0
this.waiter.resolve(this.finaldata)
}
}
})
}
}
module.exports = Queue