-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathindex.js
206 lines (185 loc) · 4.97 KB
/
index.js
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
const has = Object.prototype.hasOwnProperty
/**
* Since CustomEvent is only supported in nodejs since version 19,
* you have to create your own class instead of using CustomEvent
* @see https://github.com/nodejs/node/issues/40678
* */
export class QueueEvent extends Event {
constructor (name, detail) {
super(name)
this.detail = detail
}
}
export default class Queue extends EventTarget {
constructor (options = {}) {
super()
const { concurrency = Infinity, timeout = 0, autostart = false, results = null } = options
this.concurrency = concurrency
this.timeout = timeout
this.autostart = autostart
this.results = results
this.pending = 0
this.session = 0
this.running = false
this.jobs = []
this.timers = []
this.addEventListener('error', this._errorHandler)
}
_errorHandler (evt) {
this.end(evt.detail.error)
}
pop () {
return this.jobs.pop()
}
shift () {
return this.jobs.shift()
}
indexOf (searchElement, fromIndex) {
return this.jobs.indexOf(searchElement, fromIndex)
}
lastIndexOf (searchElement, fromIndex) {
if (fromIndex !== undefined) return this.jobs.lastIndexOf(searchElement, fromIndex)
return this.jobs.lastIndexOf(searchElement)
}
slice (start, end) {
this.jobs = this.jobs.slice(start, end)
return this
}
reverse () {
this.jobs.reverse()
return this
}
push (...workers) {
const methodResult = this.jobs.push(...workers)
if (this.autostart) this._start()
return methodResult
}
unshift (...workers) {
const methodResult = this.jobs.unshift(...workers)
if (this.autostart) this._start()
return methodResult
}
splice (start, deleteCount, ...workers) {
this.jobs.splice(start, deleteCount, ...workers)
if (this.autostart) this._start()
return this
}
get length () {
return this.pending + this.jobs.length
}
start (callback) {
if (this.running) throw new Error('already started')
let awaiter
if (callback) {
this._addCallbackToEndEvent(callback)
} else {
awaiter = this._createPromiseToEndEvent()
}
this._start()
return awaiter
}
_start () {
this.running = true
if (this.pending >= this.concurrency) {
return
}
if (this.jobs.length === 0) {
if (this.pending === 0) {
this.done()
}
return
}
const job = this.jobs.shift()
const session = this.session
const timeout = (job !== undefined) && has.call(job, 'timeout') ? job.timeout : this.timeout
let once = true
let timeoutId = null
let didTimeout = false
let resultIndex = null
const next = (error, ...result) => {
if (once && this.session === session) {
once = false
this.pending--
if (timeoutId !== null) {
this.timers = this.timers.filter(tID => tID !== timeoutId)
clearTimeout(timeoutId)
}
if (error) {
this.dispatchEvent(new QueueEvent('error', { error, job }))
} else if (!didTimeout) {
if (resultIndex !== null && this.results !== null) {
this.results[resultIndex] = [...result]
}
this.dispatchEvent(new QueueEvent('success', { result: [...result], job }))
}
if (this.session === session) {
if (this.pending === 0 && this.jobs.length === 0) {
this.done()
} else if (this.running) {
this._start()
}
}
}
}
if (timeout) {
timeoutId = setTimeout(() => {
didTimeout = true
this.dispatchEvent(new QueueEvent('timeout', { next, job }))
next()
}, timeout)
this.timers.push(timeoutId)
}
if (this.results != null) {
resultIndex = this.results.length
this.results[resultIndex] = null
}
this.pending++
this.dispatchEvent(new QueueEvent('start', { job }))
job.promise = job(next)
if (job.promise !== undefined && typeof job.promise.then === 'function') {
job.promise.then(function (result) {
return next(undefined, result)
}).catch(function (err) {
return next(err || true)
})
}
if (this.running && this.jobs.length > 0) {
this._start()
}
}
stop () {
this.running = false
}
end (error) {
this.clearTimers()
this.jobs.length = 0
this.pending = 0
this.done(error)
}
clearTimers () {
this.timers.forEach(timer => {
clearTimeout(timer)
})
this.timers = []
}
_addCallbackToEndEvent (cb) {
const onend = evt => {
this.removeEventListener('end', onend)
cb(evt.detail.error, this.results)
}
this.addEventListener('end', onend)
}
_createPromiseToEndEvent () {
return new Promise((resolve, reject) => {
this._addCallbackToEndEvent((error, results) => {
if (error) reject(error)
else resolve(results)
})
})
}
done (error) {
this.session++
this.running = false
this.dispatchEvent(new QueueEvent('end', { error }))
}
}