async-promise-pool
Advanced tools
Comparing version 1.0.0 to 1.0.1-0
115
lib/index.js
@@ -36,112 +36,70 @@ "use strict"; | ||
this.concurrency = Number.MAX_VALUE; | ||
this.next = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() { | ||
var promiseProducer, run; | ||
return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
this.next = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var promiseProducer, result, promise; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
if (!_this.error) { | ||
_context2.next = 2; | ||
_context.next = 2; | ||
break; | ||
} | ||
throw _this.error; | ||
return _context.abrupt("return", _this.final.reject(_this.error)); | ||
case 2: | ||
if (!(!_this.queue.length && !_this.pool.length)) { | ||
_context2.next = 4; | ||
_context.next = 4; | ||
break; | ||
} | ||
return _context2.abrupt("return", _this.results); | ||
return _context.abrupt("return", _this.final.resolve(_this.results)); | ||
case 4: | ||
if (!(_this.pool.length >= _this.concurrency || !_this.queue.length)) { | ||
_context2.next = 9; | ||
_context.next = 6; | ||
break; | ||
} | ||
if (!_this.wait) _this.wait = defer(); | ||
_context2.next = 8; | ||
return _this.wait.promise; | ||
return _context.abrupt("return"); | ||
case 8: | ||
return _context2.abrupt("return", _this.next()); | ||
case 6: | ||
case 9: | ||
// At this point we have a new promise to run and the concurrency to run it | ||
promiseProducer = _this.queue.shift(); | ||
result = void 0; | ||
promise = void 0; | ||
_context.prev = 9; | ||
run = function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() { | ||
var promise, result, wait; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
promise = promiseProducer(); | ||
promise = promiseProducer(); | ||
_this.pool.push(promise); | ||
_context.next = 14; | ||
return promise; | ||
_this.pool.push(promise); | ||
result = void 0; | ||
_context.prev = 3; | ||
_context.next = 6; | ||
return promise; | ||
case 14: | ||
result = _context.sent; | ||
case 6: | ||
result = _context.sent; | ||
_context.next = 12; | ||
break; | ||
case 9: | ||
_context.prev = 9; | ||
_context.t0 = _context["catch"](3); | ||
_this.results.push(result); | ||
_this.pool = _this.pool.filter(function (p) { | ||
return p !== promise; | ||
}); | ||
_context.next = 22; | ||
break; | ||
_this.error = _context.t0; | ||
case 19: | ||
_context.prev = 19; | ||
_context.t0 = _context["catch"](9); | ||
case 12: | ||
_this.error = _context.t0; | ||
_this.results.push(result); | ||
_this.pool = _this.pool.filter(function (p) { | ||
return p !== promise; | ||
}); | ||
case 22: | ||
return _context.abrupt("return", _this.next()); | ||
// every time a promise in the queue is resolved then we need to check if | ||
// there is anything waiting to be started. | ||
if (!_this.wait) { | ||
_context.next = 18; | ||
break; | ||
} | ||
wait = _this.wait; | ||
_this.wait = undefined; | ||
return _context.abrupt("return", wait.resolve()); | ||
case 18: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, _this, [[3, 9]]); | ||
})); | ||
return function run() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
// run is not awaited otherwise concurrency would never be more than one | ||
run(); | ||
return _context2.abrupt("return", _this.next()); | ||
case 13: | ||
case 23: | ||
case "end": | ||
return _context2.stop(); | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee2, _this); | ||
}, _callee, _this, [[9, 19]]); | ||
})); | ||
@@ -152,2 +110,3 @@ | ||
} | ||
this.final = defer(); | ||
} | ||
@@ -164,3 +123,3 @@ | ||
value: function all() { | ||
return this.next(); | ||
return this.final.promise; | ||
} | ||
@@ -167,0 +126,0 @@ }]); |
@@ -16,3 +16,3 @@ "use strict"; | ||
promiseSpy(); | ||
resolve(); | ||
resolve("success"); | ||
}); | ||
@@ -78,3 +78,3 @@ }; | ||
it("should allow multiple concurrency", _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() { | ||
var pool, results; | ||
var delayedPromiseSpy, delayedPromiseProducer, pool, results; | ||
return regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
@@ -84,16 +84,29 @@ while (1) { | ||
case 0: | ||
delayedPromiseSpy = jest.fn(); | ||
delayedPromiseProducer = function delayedPromiseProducer() { | ||
return new Promise(function (resolve, reject) { | ||
setTimeout(function () { | ||
delayedPromiseSpy(); | ||
resolve("delayed"); | ||
}, 1000); | ||
}); | ||
}; | ||
pool = new PromisePool({ concurrency: 2 }); | ||
pool.add(delayedPromiseProducer); | ||
pool.add(promiseProducer); | ||
pool.add(promiseProducer); | ||
_context3.next = 5; | ||
_context3.next = 7; | ||
return pool.all(); | ||
case 5: | ||
case 7: | ||
results = _context3.sent; | ||
expect(promiseSpy).toHaveBeenCalledTimes(2); | ||
expect(results.length).toBe(2); | ||
expect(delayedPromiseSpy).toBeCalled(); | ||
expect(promiseSpy).toBeCalled(); | ||
expect(results[0]).toBe("success"); | ||
expect(results[1]).toBe("delayed"); | ||
case 8: | ||
case 12: | ||
case "end": | ||
@@ -100,0 +113,0 @@ return _context3.stop(); |
{ | ||
"name": "async-promise-pool", | ||
"version": "1.0.0", | ||
"version": "1.0.1-0", | ||
"description": "A flexible pool of promises that can be awaited and executed at a chosen level of concurrency", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
[![CircleCI](https://circleci.com/gh/tommoor/promise-pool.svg?style=svg)](https://circleci.com/gh/tommoor/promise-pool) | ||
[![npm version](https://badge.fury.io/js/async-promise-pool.svg)](https://badge.fury.io/js/async-promise-pool) [![CircleCI](https://circleci.com/gh/tommoor/promise-pool.svg?style=svg)](https://circleci.com/gh/tommoor/promise-pool) | ||
@@ -24,2 +24,2 @@ # Promise Pool | ||
await pool.all(); | ||
``` | ||
``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
25
14124
292
2