@onlymisaky/promise-queue
Advanced tools
+53
-11
@@ -5,3 +5,3 @@ | ||
| * author: onlymisaky | ||
| * promise-queue v0.0.1 | ||
| * @onlymisaky/promise-queue v0.0.1 | ||
| * (c) 2021-2021 | ||
@@ -20,8 +20,35 @@ * Released under the ISC license. | ||
| this.limit = 2; | ||
| this.concurrent = 0; | ||
| this.current = 0; | ||
| this.queue = []; | ||
| this.runningQueue = []; | ||
| this.finishQueue = []; | ||
| this.results = []; | ||
| if (options === null || options === void 0 ? void 0 : options.limit) { | ||
| this.limit = options.limit; | ||
| } | ||
| if (options === null || options === void 0 ? void 0 : options.onAllFinish) { | ||
| this.onAllFinish = options.onAllFinish.bind(this); | ||
| } | ||
| } | ||
| Object.defineProperty(PromiseQueue.prototype, "total", { | ||
| get: function () { | ||
| return this.queue.length; | ||
| }, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
| Object.defineProperty(PromiseQueue.prototype, "concurrent", { | ||
| get: function () { | ||
| return this.runningQueue.length; | ||
| }, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
| Object.defineProperty(PromiseQueue.prototype, "finishedCount", { | ||
| get: function () { | ||
| return this.finishQueue.length; | ||
| }, | ||
| enumerable: false, | ||
| configurable: true | ||
| }); | ||
| PromiseQueue.prototype.add = function (promiseFunc) { | ||
@@ -34,18 +61,33 @@ this.queue.push(promiseFunc); | ||
| var _this = this; | ||
| if (this.queue.length === 0 || this.limit === this.concurrent) { | ||
| if (this.finishedCount === this.total || this.limit === this.concurrent || this.current === this.total) { | ||
| return this; | ||
| } | ||
| this.concurrent++; | ||
| var fn = this.queue.shift(); | ||
| if (this.timer) { | ||
| clearTimeout(this.timer); | ||
| this.timer = undefined; | ||
| } | ||
| var fn = this.queue[this.current]; | ||
| this.current++; | ||
| this.runningQueue.push(fn); | ||
| var promise = fn(); | ||
| promise. | ||
| then(function () { | ||
| _this.run(); | ||
| then(function (res) { | ||
| _this.resolveNext(fn, { status: 'fulfilled', value: res }); | ||
| }) | ||
| .catch(function () { | ||
| _this.run(); | ||
| .catch(function (err) { | ||
| _this.resolveNext(fn, { status: 'rejected', reason: err }); | ||
| }); | ||
| }; | ||
| PromiseQueue.prototype.run = function () { | ||
| this.concurrent--; | ||
| PromiseQueue.prototype.resolveNext = function (fn, result) { | ||
| var _this = this; | ||
| this.finishQueue.push(fn); | ||
| var runningIndex = this.runningQueue.indexOf(fn); | ||
| this.runningQueue.splice(runningIndex, 1); | ||
| var queueIndex = this.queue.indexOf(fn); | ||
| this.results[queueIndex] = result; | ||
| this.timer = setTimeout(function () { | ||
| if (_this.finishedCount === _this.total && typeof _this.onAllFinish === 'function') { | ||
| _this.onAllFinish(_this.results); | ||
| } | ||
| }, 0); | ||
| this.next(); | ||
@@ -52,0 +94,0 @@ }; |
@@ -1,10 +0,32 @@ | ||
| import { Options, PromiseFunc } from './typings'; | ||
| export declare type PromiseFunc = () => Promise<any>; | ||
| interface PromiseFulfilledResult<T> { | ||
| status: 'fulfilled'; | ||
| value: T; | ||
| } | ||
| interface PromiseRejectedResult { | ||
| status: 'rejected'; | ||
| reason: any; | ||
| } | ||
| declare type PromiseSettledResult<T = any> = PromiseFulfilledResult<T> | PromiseRejectedResult; | ||
| export interface Options { | ||
| limit?: number; | ||
| onAllFinish: (result: PromiseSettledResult[]) => void; | ||
| } | ||
| export default class PromiseQueue { | ||
| private limit; | ||
| private concurrent; | ||
| private queue; | ||
| constructor(options?: Options); | ||
| add(promiseFunc: PromiseFunc): this; | ||
| private next; | ||
| private run; | ||
| private limit; | ||
| private current; | ||
| private queue; | ||
| private runningQueue; | ||
| private finishQueue; | ||
| private results; | ||
| private timer; | ||
| private onAllFinish?; | ||
| get total(): number; | ||
| get concurrent(): number; | ||
| get finishedCount(): number; | ||
| constructor(options?: Options); | ||
| add(promiseFunc: PromiseFunc): this; | ||
| private next; | ||
| private resolveNext; | ||
| } | ||
| export { }; |
+1
-1
| { | ||
| "name": "@onlymisaky/promise-queue", | ||
| "version": "0.0.1", | ||
| "version": "0.0.2", | ||
| "description": "promise queue", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
4828
100.67%124
106.67%