es6-promise-pool = 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
}n/a
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
}n/a
PromisePoolEvent = function (target, type, data) {
this.target = target
this.type = type
this.data = data
}n/a
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
}n/a
PromisePoolEvent = function (target, type, data) {
this.target = target
this.type = type
this.data = data
}n/a
_fireEvent = function (type, data) {
this.dispatchEvent(new PromisePoolEvent(this, type, data))
}...
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()
}
}
..._onPooledPromiseFulfilled = function (promise, result) {
this._size--
if (this.active()) {
this._fireEvent('fulfilled', {
promise: promise,
result: result
})
this._proceed()
}
}...
}
}
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))
})
}
..._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) {
..._proceed = function () {
if (!this._done) {
var result = null
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.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
..._settle = function (error) {
if (error) {
this._callbacks.reject(error)
} else {
this._callbacks.resolve()
}
this._promise = null
this._callbacks = null
}...
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) {
..._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 = null
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()
}
}
...active = function () {
return !!this._promise
}...
}
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 () {
...concurrency = function (value) {
if (typeof value !== 'undefined') {
this._concurrency = value
if (this.active()) {
this._proceed()
}
}
return this._concurrency
}n/a
constructor = 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
}n/a
promise = function () {
return this._promise
}n/a
size = function () {
return this._size
}n/a
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
}...
// The number of promises to process simultaneously.
var concurrency = 3
// Create a pool.
var pool = new PromisePool(promiseProducer, concurrency)
// Start the pool.
var poolPromise = pool.start()
// Wait for the pool to settle.
poolPromise.then(function () {
console.log('All promises fulfilled')
}, function (error) {
console.log('Some promise rejected: ' + error.message)
})
...