From 22de65e5058ba1accf1f658aeb6ce43b046a84c6 Mon Sep 17 00:00:00 2001 From: yoannchb-pro <71560747+yoannchb-pro@users.noreply.github.com> Date: Wed, 9 Nov 2022 15:52:02 -0500 Subject: [PATCH] v1.0.0 --- README.md | 66 +++++++++++++++++++++---- dist/index.d.ts | 27 ++++++++-- dist/index.js | 68 +++++++++++++++++++------- dist/index.js.map | 2 +- index.ts | 92 ++++++++++++++++++++++++++--------- test/eventlistener/test.html | 36 ++++++++++++-- test/node/test-2.js | 16 ++++++ test/node/test-concurrency.js | 13 +++++ test/node/test.js | 17 +++---- test/ts/test.ts | 2 +- 10 files changed, 268 insertions(+), 71 deletions(-) create mode 100644 test/node/test-2.js create mode 100644 test/node/test-concurrency.js diff --git a/README.md b/README.md index 7f54eec..283705b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,5 @@ # Enqueu -> NOTE: The project documentation still in development and the package is not published yet - Promise queue for concurrency control ## Installation @@ -18,27 +16,75 @@ or ## Example +In this case you will get "hey, my name is yoann, how are you ?, bye" and without Enqueu "my name is yoann, bye, how are you ?, hey". + ```js import Enqueu from "enqueu"; const queu = new Enqueu(); -queu.add(() => fetch("/home")); -queu.add(() => fetch("/about")); -queu.add(() => fetch("/enqueu")); +queu + .add(() => new Promise((r) => setTimeout(r, 3000))) + .then((_) => console.log("hey")); + +queu + .add(() => new Promise((r) => setTimeout(r, 2000))) + .then((_) => console.log("how are you ?")); + +queu + .add(() => new Promise((r) => setTimeout(r, 1000))) + .then((_) => console.log("bye")); + +queu + .add(() => new Promise((r) => setTimeout(r, 500)), { priority: 1 }) //from 1 to Infinity + .then((_) => console.log("my name is Yoann")); ``` -With event listener +### With event listener + +For example in this case if you click 4 times at once on the button you will get "10, 5, 2.5". +But without Enqueu you will get "1.25, 1.25, 1.25, 1.25". ```js -const queu = new Enqueu(3); //the maximum size of the queu is 3 (other will be throwed off) +const queu = new Enqueu({ maxSize: 3 }); //the maximum size of the queu is 3 (other will be throwed off) + +let count = 20; document.querySelector("button").addEventListener( "click", queu.createFn(function () { - return fetch("/add-article").then(() => console.log("added")); + count = count / 2; + return new Promise((r) => + setTimeout(function () { + r(count); + }, count * 1000) + ); }) ); - -//if we click 5 times we will get added, added, added (each function we will wait the other one finished) ``` + +## Enqueu + +### Constructor + +- `maxSize` Max size of the queu +- `maxConcurrency` Max promise as the same time + +### Attibutes + +- `isPaused` Return a boolean to see if the queu is paused or not +- `pending` Return the number of pending functions +- `queu` Return the queu + +### Methods + +- `add(fn: Function, options: Options = {}): Function` Add the function to the queu +- `createFn(fn: Function, options: Options = {}): Function` Create a function that will add "fn" to the queu on call (useful for addEventListener for example) +- `pause()` Pause the queu +- `start()` Start the queu after a pause +- `onEmpty(fn: Function)` Call the function passed as argument when the queu is empty +- `onQueuElementExecuted(fn: Function)` Call the function passed as argument when the a new queu function is started +- `onQueuElementFinishExecution(fn: Function)` Call the function passed as argument when the a new queu function which started is finished +- `clear()` Clear the queu +- `remove(fn: Function)` Remove a function from the queu +- `removeFromIndex(index: number)` Remove a specified indexed element in the queu diff --git a/dist/index.d.ts b/dist/index.d.ts index 50f7f76..204d90f 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2,22 +2,39 @@ type Queu = { fn: Function; arguments: any; priority: number; + resolve: Function; + reject: Function; + started?: boolean; }; type Options = Partial<{ priority: number; - execute: boolean; }>; -declare class Enqueu { +type OptionsConstructor = Partial<{ maxSize: number; + maxConcurrency: number; +}>; +declare class Enqueu { private _queu; private _paused; private _emptyFunction; private _executedFunction; private _executionFinishFunction; - constructor(maxSize?: number); + private options; + constructor(options?: OptionsConstructor); get isPaused(): boolean; get pending(): number; get queu(): Queu[]; + /** + * Return important elements of queu element + * @param queuElement + * @returns + */ + /** + * Return important elements of queu element + * @param queuElement + * @returns + */ + private computeQueuElement; /** * Start the next element from the queu */ @@ -35,14 +52,14 @@ declare class Enqueu { * @param fn * @param Options */ - add(fn: Function, options?: Options): void; + add(fn: Function, options?: Options): Promise; /** * Create a function that register other function in the queu */ /** * Create a function that register other function in the queu */ - createFn(fn: Function, options?: Options): () => Promise; + createFn(fn: Function, options?: Options): () => Promise; /** * Remove element from the queu * @param fn diff --git a/dist/index.js b/dist/index.js index b339fde..bdd6f11 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30,10 +30,11 @@ } class Enqueu { - constructor(maxSize = Infinity) { - this.maxSize = maxSize; + constructor(options = {}) { this._queu = []; this._paused = false; + this.options = {}; + Object.assign(this.options, { maxConcurrency: 1 }, options); } get isPaused() { return this._paused; @@ -44,18 +45,38 @@ get queu() { return this._queu; } + /** + * Return important elements of queu element + * @param queuElement + * @returns + */ + computeQueuElement(queuElement) { + return { + fn: queuElement.fn, + arguments: queuElement.arguments, + priority: queuElement.priority, + }; + } /** * Start the next element from the queu */ startNextQueuElement() { - return __awaiter(this, void 0, void 0, function* () { - const actual = this._queu[0]; - if (this._executedFunction) - this._executedFunction(actual); - yield actual.fn(...actual.arguments); - this._queu.shift(); //after execution we remove it + const actual = this._queu.find((e) => !e.started); + if (!actual) + return; + actual.started = true; + if (this._executedFunction) + this._executedFunction(this.computeQueuElement(actual)); + actual + .fn(...actual.arguments) + .then(actual.resolve) + .catch(actual.reject) + .finally(() => { + //after execution we remove it + const index = this._queu.findIndex((e) => e === actual); + this._queu.splice(index, 1); if (this._executionFinishFunction) - this._executionFinishFunction(actual); + this._executionFinishFunction(this.computeQueuElement(actual)); if (this._queu.length > 0) this.startNextQueuElement(); else if (this._emptyFunction) @@ -68,8 +89,10 @@ * @param Options */ add(fn, options = {}) { - const queuFn = this.createFn(fn, options); - queuFn(); + return __awaiter(this, void 0, void 0, function* () { + const queuFn = this.createFn(fn, options); + return yield queuFn(); + }); } /** * Create a function that register other function in the queu @@ -77,16 +100,25 @@ createFn(fn, options = {}) { const obj = this; //no function arrow because we need access to "arguments" return function () { - var _a; - return __awaiter(this, arguments, void 0, function* () { - if (obj._queu.length === obj.maxSize) + return new Promise((resolve, reject) => { + var _a; + if (obj._queu.length === obj.options.maxSize) return; //if max size - obj._queu.push({ fn, arguments, priority: (_a = options.priority) !== null && _a !== void 0 ? _a : 0 }); // adding to the queu + // adding to the queu + const queuElement = { + fn, + arguments, + priority: (_a = options.priority) !== null && _a !== void 0 ? _a : Infinity, + resolve, + reject, + }; + obj._queu.push(queuElement); + //sorting by priority if (options.priority) - obj._queu.sort((a, b) => a.priority - b.priority); //sorting by priority + obj._queu.sort((a, b) => a.priority - b.priority); //starting the function - if ((obj._queu.length === 1 && !obj._paused) || options.execute) { - yield obj.startNextQueuElement(); + if (obj._queu.length <= obj.options.maxConcurrency && !obj._paused) { + obj.startNextQueuElement(); } }); }; diff --git a/dist/index.js.map b/dist/index.js.map index dd9a45d..1b60e56 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sources":["../../../../node_modules/tslib/tslib.es6.js","../index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","type Queu = {\r\n fn: Function;\r\n arguments: any;\r\n priority: number;\r\n};\r\n\r\ntype Options = Partial<{\r\n priority: number;\r\n execute: boolean;\r\n}>;\r\n\r\nclass Enqueu {\r\n private _queu: Queu[] = [];\r\n private _paused = false;\r\n\r\n private _emptyFunction: Function;\r\n private _executedFunction: Function;\r\n private _executionFinishFunction: Function;\r\n\r\n constructor(public maxSize: number = Infinity) {}\r\n\r\n get isPaused() {\r\n return this._paused;\r\n }\r\n\r\n get pending() {\r\n return this._queu.length;\r\n }\r\n\r\n get queu() {\r\n return this._queu;\r\n }\r\n\r\n /**\r\n * Start the next element from the queu\r\n */\r\n private async startNextQueuElement() {\r\n const actual = this._queu[0];\r\n\r\n if (this._executedFunction) this._executedFunction(actual);\r\n\r\n await actual.fn(...actual.arguments);\r\n\r\n this._queu.shift(); //after execution we remove it\r\n\r\n if (this._executionFinishFunction) this._executionFinishFunction(actual);\r\n\r\n if (this._queu.length > 0) this.startNextQueuElement();\r\n else if (this._emptyFunction) this._emptyFunction();\r\n }\r\n\r\n /**\r\n * Same as createFn but directly add the function to the queu\r\n * @param fn\r\n * @param Options\r\n */\r\n public add(fn: Function, options: Options = {}) {\r\n const queuFn = this.createFn(fn, options);\r\n queuFn();\r\n }\r\n\r\n /**\r\n * Create a function that register other function in the queu\r\n */\r\n public createFn(fn: Function, options: Options = {}) {\r\n const obj = this; //no function arrow because we need access to \"arguments\"\r\n\r\n return async function () {\r\n if (obj._queu.length === obj.maxSize) return; //if max size\r\n\r\n obj._queu.push({ fn, arguments, priority: options.priority ?? 0 }); // adding to the queu\r\n if (options.priority) obj._queu.sort((a, b) => a.priority - b.priority); //sorting by priority\r\n\r\n //starting the function\r\n if ((obj._queu.length === 1 && !obj._paused) || options.execute) {\r\n await obj.startNextQueuElement();\r\n }\r\n };\r\n }\r\n\r\n /**\r\n * Remove element from the queu\r\n * @param fn\r\n * @returns\r\n */\r\n public remove(fn: Function) {\r\n const index = this._queu.findIndex((e) => e.fn === fn);\r\n if (index === -1) return false;\r\n this.removeFromIndex(index);\r\n return true;\r\n }\r\n\r\n /**\r\n * Remove element from queu with the index\r\n * @param index\r\n */\r\n public removeFromIndex(index: number) {\r\n this._queu.splice(index, 1);\r\n }\r\n\r\n /**\r\n * Clear the actual queu\r\n */\r\n public clear() {\r\n this._queu = [];\r\n }\r\n\r\n /**\r\n * Pause the queu\r\n */\r\n public pause() {\r\n this._paused = true;\r\n }\r\n\r\n /**\r\n * Start after pause\r\n */\r\n public start() {\r\n this._paused = false;\r\n if (this._queu.length > 0) this.startNextQueuElement();\r\n }\r\n\r\n /**\r\n * When the queu is empty\r\n * @param fn\r\n */\r\n public onEmpty(fn: Function) {\r\n this._emptyFunction = fn;\r\n }\r\n\r\n /**\r\n * When a function is executed\r\n * @param fn\r\n */\r\n public onQueuElementExecuted(fn: Function) {\r\n this._executedFunction = fn;\r\n }\r\n\r\n /**\r\n * When a function have finish to be executed\r\n * @param fn\r\n */\r\n public onQueuElementFinishExecution(fn: Function) {\r\n this._executionFinishFunction = fn;\r\n }\r\n}\r\n\r\nexport default Enqueu;\r\n"],"names":[],"mappings":";;;;;;IAAA;IACA;AACA;IACA;IACA;AACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;AAuDA;IACO,SAAS,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE;IAC7D,IAAI,SAAS,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK,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AAK,GAAA;IACV,QAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACrB,QAAA,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;gBAAE,IAAI,CAAC,oBAAoB,EAAE,CAAC;SACxD;IAED;;;IAGG;IACI,IAAA,OAAO,CAAC,EAAY,EAAA;IACzB,QAAA,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC;SAC1B;IAED;;;IAGG;IACI,IAAA,qBAAqB,CAAC,EAAY,EAAA;IACvC,QAAA,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;SAC7B;IAED;;;IAGG;IACI,IAAA,4BAA4B,CAAC,EAAY,EAAA;IAC9C,QAAA,IAAI,CAAC,wBAAwB,GAAG,EAAE,CAAC;SACpC;IACF;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.js","sources":["../../../../node_modules/tslib/tslib.es6.js","../index.ts"],"sourcesContent":["/******************************************************************************\r\nCopyright (c) Microsoft Corporation.\r\n\r\nPermission to use, copy, modify, and/or distribute this software for any\r\npurpose with or without fee is hereby granted.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\r\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\r\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\r\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\r\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\r\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\r\nPERFORMANCE OF THIS SOFTWARE.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = function(d, b) {\r\n extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\r\n return extendStatics(d, b);\r\n};\r\n\r\nexport function __extends(d, b) {\r\n if (typeof b !== \"function\" && b !== null)\r\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = function() {\r\n __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n }\r\n return __assign.apply(this, arguments);\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\r\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\r\n t[p[i]] = s[p[i]];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\r\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [op[0] & 2, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n var desc = Object.getOwnPropertyDescriptor(m, k);\r\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\r\n desc = { enumerable: true, get: function() { return m[k]; } };\r\n }\r\n Object.defineProperty(o, k2, desc);\r\n}) : (function(o, m, k, k2) {\r\n if (k2 === undefined) k2 = k;\r\n o[k2] = m[k];\r\n});\r\n\r\nexport function __exportStar(m, o) {\r\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\r\n}\r\n\r\nexport function __values(o) {\r\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\r\n if (m) return m.call(o);\r\n if (o && typeof o.length === \"number\") return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\n/** @deprecated */\r\nexport function __spreadArrays() {\r\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\r\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\r\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\r\n r[k] = a[j];\r\n return r;\r\n}\r\n\r\nexport function __spreadArray(to, from, pack) {\r\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\r\n if (ar || !(i in from)) {\r\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\r\n ar[i] = from[i];\r\n }\r\n }\r\n return to.concat(ar || Array.prototype.slice.call(from));\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; } : f; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator], i;\r\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\r\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\r\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nvar __setModuleDefault = Object.create ? (function(o, v) {\r\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\r\n}) : function(o, v) {\r\n o[\"default\"] = v;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (k !== \"default\" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);\r\n __setModuleDefault(result, mod);\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n\r\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\r\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\r\n}\r\n\r\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\r\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\r\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\r\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\r\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\r\n}\r\n\r\nexport function __classPrivateFieldIn(state, receiver) {\r\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\r\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\r\n}\r\n","type Queu = {\r\n fn: Function;\r\n arguments: any;\r\n priority: number;\r\n resolve: Function;\r\n reject: Function;\r\n started?: boolean;\r\n};\r\n\r\ntype Options = Partial<{\r\n priority: number;\r\n}>;\r\n\r\ntype OptionsConstructor = Partial<{\r\n maxSize: number;\r\n maxConcurrency: number;\r\n}>;\r\n\r\nclass Enqueu {\r\n private _queu: Queu[] = [];\r\n private _paused = false;\r\n\r\n private _emptyFunction: Function;\r\n private _executedFunction: Function;\r\n private _executionFinishFunction: Function;\r\n\r\n private options: OptionsConstructor = {};\r\n\r\n constructor(options: OptionsConstructor = {}) {\r\n Object.assign(this.options, { maxConcurrency: 1 }, options);\r\n }\r\n\r\n get isPaused() {\r\n return this._paused;\r\n }\r\n\r\n get pending() {\r\n return this._queu.length;\r\n }\r\n\r\n get queu() {\r\n return this._queu;\r\n }\r\n\r\n /**\r\n * Return important elements of queu element\r\n * @param queuElement\r\n * @returns\r\n */\r\n private computeQueuElement(queuElement: Queu) {\r\n return {\r\n fn: queuElement.fn,\r\n arguments: queuElement.arguments,\r\n priority: queuElement.priority,\r\n };\r\n }\r\n\r\n /**\r\n * Start the next element from the queu\r\n */\r\n private startNextQueuElement() {\r\n const actual = this._queu.find((e) => !e.started);\r\n\r\n if (!actual) return;\r\n\r\n actual.started = true;\r\n\r\n if (this._executedFunction)\r\n this._executedFunction(this.computeQueuElement(actual));\r\n\r\n actual\r\n .fn(...actual.arguments)\r\n .then(actual.resolve)\r\n .catch(actual.reject)\r\n .finally(() => {\r\n //after execution we remove it\r\n const index = this._queu.findIndex((e) => e === actual);\r\n this._queu.splice(index, 1);\r\n\r\n if (this._executionFinishFunction)\r\n this._executionFinishFunction(this.computeQueuElement(actual));\r\n\r\n if (this._queu.length > 0) this.startNextQueuElement();\r\n else if (this._emptyFunction) this._emptyFunction();\r\n });\r\n }\r\n\r\n /**\r\n * Same as createFn but directly add the function to the queu\r\n * @param fn\r\n * @param Options\r\n */\r\n public async add(fn: Function, options: Options = {}) {\r\n const queuFn = this.createFn(fn, options);\r\n return await queuFn();\r\n }\r\n\r\n /**\r\n * Create a function that register other function in the queu\r\n */\r\n public createFn(fn: Function, options: Options = {}) {\r\n const obj = this; //no function arrow because we need access to \"arguments\"\r\n\r\n return function () {\r\n return new Promise((resolve, reject) => {\r\n if (obj._queu.length === obj.options.maxSize) return; //if max size\r\n\r\n // adding to the queu\r\n const queuElement = {\r\n fn,\r\n arguments,\r\n priority: options.priority ?? Infinity,\r\n resolve,\r\n reject,\r\n };\r\n obj._queu.push(queuElement);\r\n\r\n //sorting by priority\r\n if (options.priority) obj._queu.sort((a, b) => a.priority - b.priority);\r\n\r\n //starting the function\r\n if (obj._queu.length <= obj.options.maxConcurrency && !obj._paused) {\r\n obj.startNextQueuElement();\r\n }\r\n });\r\n };\r\n }\r\n\r\n /**\r\n * Remove element from the queu\r\n * @param fn\r\n * @returns\r\n */\r\n public remove(fn: Function) {\r\n const index = this._queu.findIndex((e) => e.fn === fn);\r\n if (index === -1) return false;\r\n this.removeFromIndex(index);\r\n return true;\r\n }\r\n\r\n /**\r\n * Remove element from queu with the index\r\n * @param index\r\n */\r\n public removeFromIndex(index: number) {\r\n this._queu.splice(index, 1);\r\n }\r\n\r\n /**\r\n * Clear the actual queu\r\n */\r\n public clear() {\r\n this._queu = [];\r\n }\r\n\r\n /**\r\n * Pause the queu\r\n */\r\n public pause() {\r\n this._paused = true;\r\n }\r\n\r\n /**\r\n * Start after pause\r\n */\r\n public start() {\r\n this._paused = false;\r\n if (this._queu.length > 0) this.startNextQueuElement();\r\n }\r\n\r\n /**\r\n * When the queu is empty\r\n * @param fn\r\n */\r\n public onEmpty(fn: Function) {\r\n this._emptyFunction = fn;\r\n }\r\n\r\n /**\r\n * When a function is executed\r\n * @param fn\r\n */\r\n public onQueuElementExecuted(fn: Function) {\r\n this._executedFunction = fn;\r\n }\r\n\r\n /**\r\n * When a function have finish to be executed\r\n * @param fn\r\n */\r\n public onQueuElementFinishExecution(fn: Function) {\r\n this._executionFinishFunction = fn;\r\n }\r\n}\r\n\r\nexport default Enqueu;\r\n"],"names":[],"mappings":";;;;;;IAAA;IACA;AACA;IACA;IACA;AACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;AAuDA;IACO,SAAS,SAAS,CAAC,OAAO,EAAE,UAAU,EAAE,CAAC,EAAE,SAAS,EAAE;IAC7D,IAAI,SAAS,KAAK,CAAC,KAAK,EAAE,EAAE,OAAO,KAAK,YAAY,CAAC,GAAG,KAAK,GAAG,IAAI,CAAC,CAAC,UAAU,OAAO,EAAE,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE;IAChH,IAAI,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,EAAE,UAAU,OAAO,EAAE,MAAM,EAAE;IAC/D,QAAQ,SAAS,SAAS,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE;IACnG,QAAQ,SAAS,QAAQ,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC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eAAe,CAAC,KAAa,EAAA;YAClC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;SAC7B;IAED;;IAEG;QACI,KAAK,GAAA;IACV,QAAA,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;SACjB;IAED;;IAEG;QACI,KAAK,GAAA;IACV,QAAA,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;SACrB;IAED;;IAEG;QACI,KAAK,GAAA;IACV,QAAA,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC;IACrB,QAAA,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;gBAAE,IAAI,CAAC,oBAAoB,EAAE,CAAC;SACxD;IAED;;;IAGG;IACI,IAAA,OAAO,CAAC,EAAY,EAAA;IACzB,QAAA,IAAI,CAAC,cAAc,GAAG,EAAE,CAAC;SAC1B;IAED;;;IAGG;IACI,IAAA,qBAAqB,CAAC,EAAY,EAAA;IACvC,QAAA,IAAI,CAAC,iBAAiB,GAAG,EAAE,CAAC;SAC7B;IAED;;;IAGG;IACI,IAAA,4BAA4B,CAAC,EAAY,EAAA;IAC9C,QAAA,IAAI,CAAC,wBAAwB,GAAG,EAAE,CAAC;SACpC;IACF;;;;;;;;"} \ No newline at end of file diff --git a/index.ts b/index.ts index 9415422..f459d30 100644 --- a/index.ts +++ b/index.ts @@ -2,11 +2,18 @@ type Queu = { fn: Function; arguments: any; priority: number; + resolve: Function; + reject: Function; + started?: boolean; }; type Options = Partial<{ priority: number; - execute: boolean; +}>; + +type OptionsConstructor = Partial<{ + maxSize: number; + maxConcurrency: number; }>; class Enqueu { @@ -17,7 +24,11 @@ class Enqueu { private _executedFunction: Function; private _executionFinishFunction: Function; - constructor(public maxSize: number = Infinity) {} + private options: OptionsConstructor = {}; + + constructor(options: OptionsConstructor = {}) { + Object.assign(this.options, { maxConcurrency: 1 }, options); + } get isPaused() { return this._paused; @@ -31,22 +42,47 @@ class Enqueu { return this._queu; } + /** + * Return important elements of queu element + * @param queuElement + * @returns + */ + private computeQueuElement(queuElement: Queu) { + return { + fn: queuElement.fn, + arguments: queuElement.arguments, + priority: queuElement.priority, + }; + } + /** * Start the next element from the queu */ - private async startNextQueuElement() { - const actual = this._queu[0]; + private startNextQueuElement() { + const actual = this._queu.find((e) => !e.started); - if (this._executedFunction) this._executedFunction(actual); + if (!actual) return; - await actual.fn(...actual.arguments); + actual.started = true; - this._queu.shift(); //after execution we remove it + if (this._executedFunction) + this._executedFunction(this.computeQueuElement(actual)); - if (this._executionFinishFunction) this._executionFinishFunction(actual); + actual + .fn(...actual.arguments) + .then(actual.resolve) + .catch(actual.reject) + .finally(() => { + //after execution we remove it + const index = this._queu.findIndex((e) => e === actual); + this._queu.splice(index, 1); - if (this._queu.length > 0) this.startNextQueuElement(); - else if (this._emptyFunction) this._emptyFunction(); + if (this._executionFinishFunction) + this._executionFinishFunction(this.computeQueuElement(actual)); + + if (this._queu.length > 0) this.startNextQueuElement(); + else if (this._emptyFunction) this._emptyFunction(); + }); } /** @@ -54,9 +90,9 @@ class Enqueu { * @param fn * @param Options */ - public add(fn: Function, options: Options = {}) { + public async add(fn: Function, options: Options = {}) { const queuFn = this.createFn(fn, options); - queuFn(); + return await queuFn(); } /** @@ -65,16 +101,28 @@ class Enqueu { public createFn(fn: Function, options: Options = {}) { const obj = this; //no function arrow because we need access to "arguments" - return async function () { - if (obj._queu.length === obj.maxSize) return; //if max size - - obj._queu.push({ fn, arguments, priority: options.priority ?? 0 }); // adding to the queu - if (options.priority) obj._queu.sort((a, b) => a.priority - b.priority); //sorting by priority - - //starting the function - if ((obj._queu.length === 1 && !obj._paused) || options.execute) { - await obj.startNextQueuElement(); - } + return function () { + return new Promise((resolve, reject) => { + if (obj._queu.length === obj.options.maxSize) return; //if max size + + // adding to the queu + const queuElement = { + fn, + arguments, + priority: options.priority ?? Infinity, + resolve, + reject, + }; + obj._queu.push(queuElement); + + //sorting by priority + if (options.priority) obj._queu.sort((a, b) => a.priority - b.priority); + + //starting the function + if (obj._queu.length <= obj.options.maxConcurrency && !obj._paused) { + obj.startNextQueuElement(); + } + }); }; } diff --git a/test/eventlistener/test.html b/test/eventlistener/test.html index c713758..95b9641 100644 --- a/test/eventlistener/test.html +++ b/test/eventlistener/test.html @@ -9,13 +9,39 @@ + \ No newline at end of file diff --git a/test/node/test-2.js b/test/node/test-2.js new file mode 100644 index 0000000..4062d7d --- /dev/null +++ b/test/node/test-2.js @@ -0,0 +1,16 @@ +const Enqueu = require("../../dist/index"); + +const queu = new Enqueu(); + +queu + .add(() => new Promise((r) => setTimeout(r, 6000))) + .then((_) => console.log("hey")); +queu + .add(() => new Promise((r) => setTimeout(r, 5000))) + .then((_) => console.log("how are you ?")); +queu + .add(() => new Promise((r) => setTimeout(r, 3000))) + .then((_) => console.log("bye")); +queu + .add(() => new Promise((r) => setTimeout(r, 500)), { priority: 1 }) //from 1 to Infinity + .then((_) => console.log("my name is Yoann")); diff --git a/test/node/test-concurrency.js b/test/node/test-concurrency.js new file mode 100644 index 0000000..a6c89c1 --- /dev/null +++ b/test/node/test-concurrency.js @@ -0,0 +1,13 @@ +const Enqueu = require("../../dist/index"); + +const queu = new Enqueu({ maxConcurrency: 2 }); + +queu + .add(() => new Promise((r) => setTimeout(r, 6000))) + .then((_) => console.log("hey")); +queu + .add(() => new Promise((r) => setTimeout(r, 3000))) + .then((_) => console.log("how are you ?")); +queu + .add(() => new Promise((r) => setTimeout(r, 2000))) + .then((_) => console.log("bye")); diff --git a/test/node/test.js b/test/node/test.js index 5163b86..a53631e 100644 --- a/test/node/test.js +++ b/test/node/test.js @@ -4,29 +4,28 @@ function wait(t) { return new Promise((_) => setTimeout(_, t)); } -const queu = new Enqueu(3); -let id = 0; +const queu = new Enqueu({ maxSize: 3 }); queu.pause(); queu.add(function () { - console.log(++id); - return wait(2000); + console.log(1); + return wait(3000); }); queu.add(function () { - console.log(++id); + console.log(2); return wait(2000); }); queu.add(function () { - console.log(++id); - return wait(2000); + console.log(3); + return wait(1000); }); queu.add(function () { - console.log(++id); - return wait(2000); + console.log(4); + return wait(1); }); queu.onQueuElementExecuted(function () { diff --git a/test/ts/test.ts b/test/ts/test.ts index f2cfd4a..51b07fb 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -4,7 +4,7 @@ function wait(t: number) { return new Promise((_) => setTimeout(_, t)); } -const queu = new Enqueu(3); +const queu = new Enqueu({ maxSize: 3 }); let id = 0; queu.pause();