-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathes6-promise-pool.js
216 lines (193 loc) · 5.44 KB
/
es6-promise-pool.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
207
208
209
210
211
212
213
214
215
216
(function (root, factory) {
/* istanbul ignore next */
if (typeof define === 'function' && define.amd) {
define([], factory)
} else if (typeof exports === 'object') {
module.exports = factory()
} else {
root.PromisePool = factory()
// Legacy API
root.promisePool = root.PromisePool
}
})(this, function () {
'use strict'
var EventTarget = function () {
this._listeners = {}
}
EventTarget.prototype.addEventListener = function (type, listener) {
this._listeners[type] = this._listeners[type] || []
if (this._listeners[type].indexOf(listener) < 0) {
this._listeners[type].push(listener)
}
}
EventTarget.prototype.removeEventListener = function (type, listener) {
if (this._listeners[type]) {
var p = this._listeners[type].indexOf(listener)
if (p >= 0) {
this._listeners[type].splice(p, 1)
}
}
}
EventTarget.prototype.dispatchEvent = function (evt) {
if (this._listeners[evt.type] && this._listeners[evt.type].length) {
var listeners = this._listeners[evt.type].slice()
for (var i = 0, l = listeners.length; i < l; ++i) {
listeners[i].call(this, evt)
}
}
}
var isGenerator = function (func) {
return (typeof func.constructor === 'function' &&
func.constructor.name === 'GeneratorFunction')
}
var functionToIterator = function (func) {
return {
next: function () {
var promise = func()
return promise ? {value: promise} : {done: true}
}
}
}
var promiseToIterator = function (promise) {
var called = false
return {
next: function () {
if (called) {
return {done: true}
}
called = true
return {value: promise}
}
}
}
var toIterator = function (obj, Promise) {
var type = typeof obj
if (type === 'object') {
if (typeof obj.next === 'function') {
return obj
}
/* istanbul ignore else */
if (typeof obj.then === 'function') {
return promiseToIterator(obj)
}
}
if (type === 'function') {
return isGenerator(obj) ? obj() : functionToIterator(obj)
}
return promiseToIterator(Promise.resolve(obj))
}
var PromisePoolEvent = function (target, type, data) {
this.target = target
this.type = type
this.data = data
}
var PromisePool = function (source, concurrency, options) {
EventTarget.call(this)
if (typeof concurrency !== 'number' ||
Math.floor(concurrency) !== concurrency ||
concurrency < 1) {
throw new Error('Invalid concurrency')
}
this._concurrency = concurrency
this._options = options || {}
this._options.promise = this._options.promise || Promise
this._iterator = toIterator(source, this._options.promise)
this._done = false
this._size = 0
this._promise = null
this._callbacks = null
}
PromisePool.prototype = new EventTarget()
PromisePool.prototype.constructor = PromisePool
PromisePool.prototype.concurrency = function (value) {
if (typeof value !== 'undefined') {
this._concurrency = value
if (this.active()) {
this._proceed()
}
}
return this._concurrency
}
PromisePool.prototype.size = function () {
return this._size
}
PromisePool.prototype.active = function () {
return !!this._promise
}
PromisePool.prototype.promise = function () {
return this._promise
}
PromisePool.prototype.start = function () {
var that = this
var Promise = this._options.promise
this._promise = new Promise(function (resolve, reject) {
that._callbacks = {
reject: reject,
resolve: resolve
}
that._proceed()
})
return this._promise
}
PromisePool.prototype._fireEvent = function (type, data) {
this.dispatchEvent(new PromisePoolEvent(this, type, data))
}
PromisePool.prototype._settle = function (error) {
if (error) {
this._callbacks.reject(error)
} else {
this._callbacks.resolve()
}
this._promise = null
this._callbacks = null
}
PromisePool.prototype._onPooledPromiseFulfilled = function (promise, result) {
this._size--
if (this.active()) {
this._fireEvent('fulfilled', {
promise: promise,
result: result
})
this._proceed()
}
}
PromisePool.prototype._onPooledPromiseRejected = function (promise, error) {
this._size--
if (this.active()) {
this._fireEvent('rejected', {
promise: promise,
error: error
})
this._settle(error || new Error('Unknown error'))
}
}
PromisePool.prototype._trackPromise = function (promise) {
var that = this
promise
.then(function (result) {
that._onPooledPromiseFulfilled(promise, result)
}, function (error) {
that._onPooledPromiseRejected(promise, error)
})['catch'](function (err) {
that._settle(new Error('Promise processing failed: ' + err))
})
}
PromisePool.prototype._proceed = function () {
if (!this._done) {
var result = { done: false }
while (this._size < this._concurrency &&
!(result = this._iterator.next()).done) {
this._size++
this._trackPromise(result.value)
}
this._done = (result === null || !!result.done)
}
if (this._done && this._size === 0) {
this._settle()
}
}
PromisePool.PromisePoolEvent = PromisePoolEvent
// Legacy API
PromisePool.PromisePool = PromisePool
return PromisePool
})