es6-promise-pool
Advanced tools
Comparing version 0.2.5 to 0.2.7
@@ -1,142 +0,144 @@ | ||
(function(root, factory) { | ||
/* global define */ | ||
(function (root, factory) { | ||
if (typeof define === 'function' && define.amd) { | ||
define(['es6-promise'], factory); | ||
define(['es6-promise'], factory) | ||
} else if (typeof exports === 'object') { | ||
module.exports = factory(require('es6-promise')); | ||
module.exports = factory(require('es6-promise')) | ||
} else { | ||
root.promisePool = factory(root.ES6Promise); | ||
root.promisePool = factory(root.ES6Promise) | ||
} | ||
})(this, function(es6promise) { | ||
'use strict'; | ||
})(this, function (es6promise) { | ||
'use strict' | ||
var Promise = es6promise.Promise; | ||
var Promise = es6promise.Promise | ||
var generatorFunctionToProducer = function(gen) { | ||
gen = gen(); | ||
return function() { | ||
var res = gen.next(); | ||
return res.done ? null : res.value; | ||
}; | ||
}; | ||
var generatorFunctionToProducer = function (gen) { | ||
gen = gen() | ||
return function () { | ||
var res = gen.next() | ||
return res.done ? null : res.value | ||
} | ||
} | ||
var promiseToProducer = function(promise) { | ||
var called = false; | ||
return function() { | ||
var promiseToProducer = function (promise) { | ||
var called = false | ||
return function () { | ||
if (called) { | ||
return null; | ||
return null | ||
} | ||
called = true; | ||
return promise; | ||
}; | ||
}; | ||
called = true | ||
return promise | ||
} | ||
} | ||
var toProducer = function(obj) { | ||
var type = typeof obj; | ||
var toProducer = function (obj) { | ||
var type = typeof obj | ||
if (type === 'function') { | ||
if (obj.constructor && obj.constructor.name === 'GeneratorFunction') { | ||
return generatorFunctionToProducer(obj); | ||
return generatorFunctionToProducer(obj) | ||
} else { | ||
return obj; | ||
return obj | ||
} | ||
} | ||
if (type !== 'object' || typeof obj.then !== 'function') { | ||
obj = Promise.resolve(obj); | ||
obj = Promise.resolve(obj) | ||
} | ||
return promiseToProducer(obj); | ||
}; | ||
return promiseToProducer(obj) | ||
} | ||
var PromisePoolEvent = function(target, type, data) { | ||
this.target = target; | ||
this.type = type; | ||
this.data = data; | ||
}; | ||
var PromisePoolEvent = function (target, type, data) { | ||
this.target = target | ||
this.type = type | ||
this.data = data | ||
} | ||
var PromisePool = function(source, concurrency, options) { | ||
var PromisePool = function (source, concurrency, options) { | ||
if (typeof concurrency !== 'number' || | ||
Math.floor(concurrency) !== concurrency || | ||
concurrency < 1) { | ||
throw new Error('Invalid concurrency'); | ||
Math.floor(concurrency) !== concurrency || | ||
concurrency < 1) { | ||
throw new Error('Invalid concurrency') | ||
} | ||
this._producer = toProducer(source); | ||
this._concurrency = concurrency; | ||
this._options = options || {}; | ||
this._listeners = {}; | ||
this._producerDone = false; | ||
this._size = 0; | ||
this._promise = null; | ||
this._callbacks = null; | ||
}; | ||
this._producer = toProducer(source) | ||
this._concurrency = concurrency | ||
this._options = options || {} | ||
this._listeners = {} | ||
this._producerDone = false | ||
this._size = 0 | ||
this._promise = null | ||
this._callbacks = null | ||
} | ||
PromisePool.prototype.concurrency = function(value) { | ||
PromisePool.prototype.concurrency = function (value) { | ||
if (typeof value !== 'undefined') { | ||
this._concurrency = value; | ||
this._concurrency = value | ||
if (this.active()) { | ||
this._proceed(); | ||
this._proceed() | ||
} | ||
} | ||
return this._concurrency; | ||
}; | ||
return this._concurrency | ||
} | ||
PromisePool.prototype.size = function() { | ||
return this._size; | ||
}; | ||
PromisePool.prototype.size = function () { | ||
return this._size | ||
} | ||
PromisePool.prototype.active = function() { | ||
return !!this._promise; | ||
}; | ||
PromisePool.prototype.active = function () { | ||
return !!this._promise | ||
} | ||
PromisePool.prototype.promise = function() { | ||
return this._promise; | ||
}; | ||
PromisePool.prototype.promise = function () { | ||
return this._promise | ||
} | ||
PromisePool.prototype.start = function() { | ||
var that = this; | ||
this._promise = new Promise(function(resolve, reject) { | ||
PromisePool.prototype.start = function () { | ||
var that = this | ||
this._promise = new Promise(function (resolve, reject) { | ||
that._callbacks = { | ||
reject: reject, | ||
resolve: resolve | ||
}; | ||
that._proceed(); | ||
}); | ||
return this._promise; | ||
}; | ||
} | ||
that._proceed() | ||
}) | ||
return this._promise | ||
} | ||
PromisePool.prototype.addEventListener = function(type, listener) { | ||
this._listeners[type] = this._listeners[type] || []; | ||
PromisePool.prototype.addEventListener = function (type, listener) { | ||
this._listeners[type] = this._listeners[type] || [] | ||
if (this._listeners[type].indexOf(listener) < 0) { | ||
this._listeners[type].push(listener); | ||
this._listeners[type].push(listener) | ||
} | ||
}; | ||
} | ||
PromisePool.prototype.removeEventListener = function(type, listener) { | ||
PromisePool.prototype.removeEventListener = function (type, listener) { | ||
if (this._listeners[type]) { | ||
var p = this._listeners[type].indexOf(listener); | ||
var p = this._listeners[type].indexOf(listener) | ||
if (p >= 0) { | ||
this._listeners[type].splice(p, 1); | ||
this._listeners[type].splice(p, 1) | ||
} | ||
} | ||
}; | ||
} | ||
PromisePool.prototype._fireEvent = function(type, data) { | ||
PromisePool.prototype._fireEvent = function (type, data) { | ||
if (this._listeners[type] && this._listeners[type].length) { | ||
var evt = new PromisePoolEvent(this, type, data); | ||
var listeners = this._listeners[type].slice(); | ||
var evt = new PromisePoolEvent(this, type, data) | ||
var listeners = this._listeners[type].slice() | ||
for (var i = 0, l = listeners.length; i < l; ++i) { | ||
listeners[i].call(this, evt); | ||
listeners[i].call(this, evt) | ||
} | ||
} | ||
}; | ||
} | ||
PromisePool.prototype._settle = function(error) { | ||
PromisePool.prototype._settle = function (error) { | ||
if (error) { | ||
this._callbacks.reject(error); | ||
this._callbacks.reject(error) | ||
} else { | ||
this._callbacks.resolve(); | ||
this._callbacks.resolve() | ||
} | ||
this._promise = null; | ||
this._callbacks = null; | ||
}; | ||
this._promise = null | ||
this._callbacks = null | ||
} | ||
PromisePool.prototype._onPooledPromiseFulfilled = function(promise, result) { | ||
this._size--; | ||
PromisePool.prototype._onPooledPromiseFulfilled = function (promise, result) { | ||
this._size-- | ||
if (this.active()) { | ||
@@ -146,9 +148,9 @@ this._fireEvent('fulfilled', { | ||
result: result | ||
}); | ||
this._proceed(); | ||
}) | ||
this._proceed() | ||
} | ||
}; | ||
} | ||
PromisePool.prototype._onPooledPromiseRejected = function(promise, error) { | ||
this._size--; | ||
PromisePool.prototype._onPooledPromiseRejected = function (promise, error) { | ||
this._size-- | ||
if (this.active()) { | ||
@@ -158,69 +160,69 @@ this._fireEvent('rejected', { | ||
error: error | ||
}); | ||
this._settle(error || new Error('Unknown 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); | ||
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)); | ||
}); | ||
}; | ||
['catch'](function (err) { | ||
that._settle(new Error('Promise processing failed: ' + err)) | ||
}) | ||
} | ||
PromisePool.prototype._proceed = function() { | ||
PromisePool.prototype._proceed = function () { | ||
if (!this._producerDone) { | ||
var promise; | ||
var promise | ||
while (this._size < this._concurrency && | ||
!!(promise = this._producer.call(this))) { | ||
this._size++; | ||
this._trackPromise(promise); | ||
!!(promise = this._producer.call(this))) { | ||
this._size++ | ||
this._trackPromise(promise) | ||
} | ||
if (!promise) { | ||
this._producerDone = true; | ||
this._producerDone = true | ||
} | ||
} | ||
if (this._producerDone && this._size === 0) { | ||
this._settle(); | ||
this._settle() | ||
} | ||
}; | ||
} | ||
var modernizeOption = function(options, listeners, optKey, eventType, eventKey) { | ||
var modernizeOption = function (options, listeners, optKey, eventType, eventKey) { | ||
if (options[optKey]) { | ||
var cb = options[optKey]; | ||
listeners[eventType] = function(evt) { | ||
cb(evt.target._promise, evt.data.promise, evt.data[eventKey]); | ||
}; | ||
var cb = options[optKey] | ||
listeners[eventType] = function (evt) { | ||
cb(evt.target._promise, evt.data.promise, evt.data[eventKey]) | ||
} | ||
} | ||
}; | ||
} | ||
var modernizeOptions = function(options) { | ||
var listeners = {}; | ||
modernizeOption(options, listeners, 'onresolve', 'fulfilled', 'result'); | ||
modernizeOption(options, listeners, 'onreject', 'rejected', 'error'); | ||
return listeners; | ||
}; | ||
var modernizeOptions = function (options) { | ||
var listeners = {} | ||
modernizeOption(options, listeners, 'onresolve', 'fulfilled', 'result') | ||
modernizeOption(options, listeners, 'onreject', 'rejected', 'error') | ||
return listeners | ||
} | ||
var createPool = function(source, concurrency, options) { | ||
var createPool = function (source, concurrency, options) { | ||
// Legacy API: options.onresolve and options.onreject | ||
var listeners = options ? modernizeOptions(options) : null; | ||
var pool = new PromisePool(source, concurrency, options); | ||
var listeners = options ? modernizeOptions(options) : null | ||
var pool = new PromisePool(source, concurrency, options) | ||
if (listeners) { | ||
for (var type in listeners) { | ||
pool.addEventListener(type, listeners[type]); | ||
pool.addEventListener(type, listeners[type]) | ||
} | ||
} | ||
return pool.start(); | ||
}; | ||
return pool.start() | ||
} | ||
createPool.Promise = Promise; | ||
createPool.PromisePool = PromisePool; | ||
createPool.PromisePoolEvent = PromisePoolEvent; | ||
createPool.Promise = Promise | ||
createPool.PromisePool = PromisePool | ||
createPool.PromisePoolEvent = PromisePoolEvent | ||
return createPool; | ||
}); | ||
return createPool | ||
}) |
{ | ||
"name": "es6-promise-pool", | ||
"version": "0.2.5", | ||
"version": "0.2.7", | ||
"description": "Runs Promises in a pool that limits their maximum concurrency.", | ||
@@ -43,3 +43,4 @@ "author": { | ||
"mocha-phantomjs": "^3.5.2", | ||
"phantomjs": "^1.9.13" | ||
"phantomjs": "^1.9.13", | ||
"standard": "^3.7.3" | ||
}, | ||
@@ -50,7 +51,7 @@ "engines": { | ||
"scripts": { | ||
"test": "npm run mocha && npm run ph && node --version | grep -qv ^v0\\.11\\. || npm run coveralls", | ||
"mocha": "node --harmony node_modules/mocha/bin/_mocha test/test.js", | ||
"ph": "node --harmony node_modules/mocha-phantomjs/bin/mocha-phantomjs test/test.html", | ||
"cover": "node --harmony node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec", | ||
"coveralls": "node --harmony node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && node ./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info && rm -rf ./coverage" | ||
"test": "npm run standard && npm run mocha && node --version | grep -qv ^v0\\.12\\. || npm run coveralls", | ||
"standard": "node node_modules/standard/bin/cmd.js", | ||
"mocha": "node node_modules/mocha/bin/_mocha", | ||
"cover": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec", | ||
"coveralls": "node node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && node node_modules/coveralls/bin/coveralls.js < coverage/lcov.info && rm -rf coverage" | ||
}, | ||
@@ -72,3 +73,9 @@ "files": [ | ||
} | ||
] | ||
], | ||
"standard": { | ||
"ignore": [ | ||
"bower_components/**", | ||
"demo/demo-generator.js" | ||
] | ||
} | ||
} |
110
README.md
@@ -1,3 +0,5 @@ | ||
# es6-promise-pool [![npm](https://img.shields.io/npm/v/es6-promise-pool.svg)](https://www.npmjs.com/package/es6-promise-pool) ![Bower](https://img.shields.io/bower/v/es6-promise-pool.svg) [![Build Status](https://img.shields.io/travis/timdp/es6-promise-pool.svg)](https://travis-ci.org/timdp/es6-promise-pool) [![Coverage Status](https://img.shields.io/coveralls/timdp/es6-promise-pool.svg)](https://coveralls.io/r/timdp/es6-promise-pool) | ||
# es6-promise-pool | ||
[![npm](https://img.shields.io/npm/v/es6-promise-pool.svg)](https://www.npmjs.com/package/es6-promise-pool) ![Bower](https://img.shields.io/bower/v/es6-promise-pool.svg) [![Build Status](https://img.shields.io/travis/timdp/es6-promise-pool.svg)](https://travis-ci.org/timdp/es6-promise-pool) [![Coverage Status](https://img.shields.io/coveralls/timdp/es6-promise-pool.svg)](https://coveralls.io/r/timdp/es6-promise-pool) [![JavaScript Standard Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) | ||
Runs `Promise`s in a pool that limits their maximum concurrency. | ||
@@ -42,3 +44,3 @@ | ||
<script src="es6-promise.js"></script> | ||
<script>ES6Promise.polyfill();</script> | ||
<script>ES6Promise.polyfill()</script> | ||
<script src="es6-promise-pool.js"></script> | ||
@@ -51,8 +53,8 @@ ``` | ||
// On the Web, leave out these two lines and use the script tags above instead. | ||
var Promise = require('es6-promise').Promise; | ||
var promisePool = require('es6-promise-pool'); | ||
var Promise = require('es6-promise').Promise | ||
var promisePool = require('es6-promise-pool') | ||
var PromisePool = promisePool.PromisePool; | ||
var PromisePool = promisePool.PromisePool | ||
var promiseProducer = function() { | ||
var promiseProducer = function () { | ||
// Your code goes here. | ||
@@ -62,19 +64,19 @@ // If there is work left to be done, return the next work item as a promise. | ||
// Scroll down for an example. | ||
}; | ||
} | ||
// The number of promises to process simultaneously. | ||
var concurrency = 3; | ||
var concurrency = 3 | ||
// Create a pool. | ||
var pool = new PromisePool(promiseProducer, concurrency); | ||
var pool = new PromisePool(promiseProducer, concurrency) | ||
// Start the pool. | ||
var poolPromise = pool.start(); | ||
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); | ||
}); | ||
poolPromise.then(function () { | ||
console.log('All promises fulfilled') | ||
}, function (error) { | ||
console.log('Some promise rejected: ' + error.message) | ||
}) | ||
``` | ||
@@ -89,11 +91,11 @@ | ||
```js | ||
var delayValue = function(value, time) { | ||
return new Promise(function(resolve, reject) { | ||
console.log('Resolving ' + value + ' in ' + time + ' ms'); | ||
setTimeout(function() { | ||
console.log('Resolving: ' + value); | ||
resolve(value); | ||
}, time); | ||
}); | ||
}; | ||
var delayValue = function (value, time) { | ||
return new Promise(function (resolve, reject) { | ||
console.log('Resolving ' + value + ' in ' + time + ' ms') | ||
setTimeout(function () { | ||
console.log('Resolving: ' + value) | ||
resolve(value) | ||
}, time) | ||
}) | ||
} | ||
``` | ||
@@ -109,18 +111,18 @@ | ||
```js | ||
var count = 0; | ||
var promiseProducer = function() { | ||
var count = 0 | ||
var promiseProducer = function () { | ||
if (count < 5) { | ||
count++; | ||
return delayValue(count, 1000); | ||
count++ | ||
return delayValue(count, 1000) | ||
} else { | ||
return null; | ||
return null | ||
} | ||
}; | ||
} | ||
var pool = new PromisePool(promiseProducer, 3); | ||
var pool = new PromisePool(promiseProducer, 3) | ||
pool.start() | ||
.then(function() { | ||
console.log('Complete'); | ||
}); | ||
.then(function () { | ||
console.log('Complete') | ||
}) | ||
``` | ||
@@ -133,14 +135,14 @@ | ||
```js | ||
var promiseProducer = function*() { | ||
var promiseProducer = function* () { | ||
for (var count = 1; count <= 5; count++) { | ||
yield delayValue(count, 1000); | ||
yield delayValue(count, 1000) | ||
} | ||
}; | ||
var pool = new PromisePool(promiseProducer, 3); | ||
var pool = new PromisePool(promiseProducer, 3) | ||
pool.start() | ||
.then(function() { | ||
console.log('Complete'); | ||
}); | ||
.then(function () { | ||
console.log('Complete') | ||
}) | ||
``` | ||
@@ -155,26 +157,26 @@ | ||
```js | ||
var pool = new PromisePool(promiseProducer, concurrency); | ||
var pool = new PromisePool(promiseProducer, concurrency) | ||
pool.addEventListener('fulfilled', function(event) { | ||
pool.addEventListener('fulfilled', function (event) { | ||
// The event contains: | ||
// - target: the PromisePool itself; | ||
// - target: the PromisePool itself | ||
// - data: | ||
// - promise: the Promise that got fulfilled; | ||
// - promise: the Promise that got fulfilled | ||
// - result: the result of that Promise. | ||
console.log('Fulfilled: ' + event.data.result); | ||
}); | ||
console.log('Fulfilled: ' + event.data.result) | ||
}) | ||
pool.addEventListener('rejected', function(event) { | ||
pool.addEventListener('rejected', function (event) { | ||
// The event contains: | ||
// - target: the PromisePool itself; | ||
// - target: the PromisePool itself | ||
// - data: | ||
// - promise: the Promise that got rejected; | ||
// - promise: the Promise that got rejected | ||
// - error: the Error for the rejection. | ||
console.log('Rejected: ' + event.data.error.message); | ||
}); | ||
console.log('Rejected: ' + event.data.error.message) | ||
}) | ||
pool.start() | ||
.then(function() { | ||
console.log('Complete'); | ||
}); | ||
.then(function () { | ||
console.log('Complete') | ||
}) | ||
``` | ||
@@ -181,0 +183,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15904
201
208
10
1