queue-promise
Advanced tools
Comparing version 1.3.33 to 2.0.0
@@ -1,1 +0,1 @@ | ||
"use strict";var _events=_interopRequireDefault(require("events"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}class Queue extends _events.default{constructor(a={}){super(),_defineProperty(this,"tasks",new Map),_defineProperty(this,"uniqueId",0),_defineProperty(this,"intervalId",void 0),_defineProperty(this,"currentlyHandled",0),_defineProperty(this,"options",{concurrent:5,interval:500,start:!0}),_defineProperty(this,"started",!1),_defineProperty(this,"stopped",!1),this.options=_objectSpread({},this.options,{},a),this.options.interval=parseInt(this.options.interval,10),this.options.concurrent=parseInt(this.options.concurrent,10),a.concurrency&&(this.options.concurrent=parseInt(a.concurrency,10))}start(){this.started||this.isEmpty||(this.stopped=!1,this.started=!0,this.intervalId=setInterval(this.dequeue.bind(this),this.options.interval),this.emit("start"))}stop(){this.stopped=!0,this.started=!1,clearInterval(this.intervalId),this.emit("stop")}finalize(){this.currentlyHandled-=1,0===this.currentlyHandled&&this.isEmpty&&(this.emit("end"),this.stop(),this.stopped=!1)}async dequeue(){const a=[];this.tasks.forEach((b,c)=>{this.currentlyHandled<this.options.concurrent&&(this.currentlyHandled++,this.tasks.delete(c),a.push(Promise.resolve(b()).then(a=>(this.emit("resolve",a),a)).catch(a=>(this.emit("reject",a),a)).finally(()=>{this.emit("dequeue"),this.finalize()})))});const b=await Promise.all(a);return 1===this.options.concurrent?b[0]:b}enqueue(a){if(Array.isArray(a))return void a.map(a=>this.enqueue(a));if("function"!=typeof a)throw new Error(`You must provide a function, not ${typeof a}.`);this.tasks.set(this.uniqueId++,a),this.options.start&&!this.stopped&&this.start()}add(a){this.enqueue(a)}clear(){this.tasks.clear()}get isEmpty(){return 0===this.tasks.size}}exports.default=Queue,module.exports=exports.default; | ||
"use strict";var _events=_interopRequireDefault(require("events"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}class Queue extends _events.default{constructor(a={}){super(),_defineProperty(this,"tasks",new Map),_defineProperty(this,"uniqueId",0),_defineProperty(this,"lastRan",void 0),_defineProperty(this,"timeoutId",void 0),_defineProperty(this,"currentlyHandled",0),_defineProperty(this,"options",{concurrent:5,interval:500,start:!0}),_defineProperty(this,"started",!1),_defineProperty(this,"stopped",!1),this.options=_objectSpread({},this.options,{},a),this.options.interval=parseInt(this.options.interval,10),this.options.concurrent=parseInt(this.options.concurrent,10),a.concurrency&&(this.options.concurrent=parseInt(a.concurrency,10))}start(){this.started||this.isEmpty||(this.stopped=!1,this.started=!0,this.emit("start"),(async()=>{for(;!this.isEmpty&&!this.stopped;)await this.execute()})())}stop(){this.stopped=!0,this.started=!1,clearTimeout(this.timeoutId),this.emit("stop")}finalize(){this.currentlyHandled-=1,0===this.currentlyHandled&&this.isEmpty&&(this.emit("end"),this.stop(),this.stopped=!1)}async execute(){const a=[];this.tasks.forEach((b,c)=>{this.currentlyHandled<this.options.concurrent&&(this.currentlyHandled++,this.tasks.delete(c),a.push(Promise.resolve(b()).then(a=>(this.emit("resolve",a),a)).catch(a=>(this.emit("reject",a),a)).finally(()=>{this.emit("dequeue"),this.finalize()})))});const b=await Promise.all(a);return 1===this.options.concurrent?b[0]:b}dequeue(){const{interval:a}=this.options;return new Promise(b=>{this.lastRan||(this.lastRan=-Date.now()+a),clearTimeout(this.timeoutId),this.timeoutId=setTimeout(()=>{this.lastRan=Date.now(),this.execute().then(b)},a-(Date.now()-this.lastRan))})}enqueue(a){if(Array.isArray(a))return void a.map(a=>this.enqueue(a));if("function"!=typeof a)throw new Error(`You must provide a function, not ${typeof a}.`);this.tasks.set(this.uniqueId++,a),this.options.start&&!this.stopped&&this.start()}add(a){this.enqueue(a)}clear(){this.tasks.clear()}get isEmpty(){return 0===this.tasks.size}}exports.default=Queue,module.exports=exports.default; |
{ | ||
"name": "queue-promise", | ||
"version": "1.3.33", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -22,2 +22,3 @@ "queue", | ||
"main": "dist/index.js", | ||
"types": "dist", | ||
"repository": { | ||
@@ -34,2 +35,3 @@ "type": "git", | ||
"@babel/core": "^7.9.0", | ||
"@babel/node": "^7.8.7", | ||
"@babel/plugin-proposal-async-generator-functions": "^7.8.3", | ||
@@ -41,2 +43,3 @@ "@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/register": "^7.9.0", | ||
"@types/node": "13.9.5", | ||
"babel-eslint": "^10.1.0", | ||
@@ -46,2 +49,3 @@ "babel-plugin-add-module-exports": "^1.0.2", | ||
"chai": "^4.2.0", | ||
"dtslint": "^3.4.0", | ||
"eslint": "^6.8.0", | ||
@@ -57,2 +61,4 @@ "eslint-config-prettier": "^6.10.1", | ||
"flow-bin": "^0.121.0", | ||
"fs-extra": "^9.0.0", | ||
"glob": "^7.1.6", | ||
"mocha": "^7.1.1", | ||
@@ -63,11 +69,17 @@ "prettier": "^2.0.2", | ||
"scripts": { | ||
"test": "npm run prepare && npm run test:eslint && npm run test:flow && npm run test:mocha", | ||
"pretest": "npm run prepare", | ||
"test": "npm run test:eslint && npm run test:flow && npm run test:typescript && npm run test:mocha", | ||
"test:flow": "flow", | ||
"test:typescript": "npx dtslint types", | ||
"test:mocha": "mocha --require @babel/register", | ||
"test:eslint": "eslint --fix src", | ||
"test:eslint": "eslint src", | ||
"defs:flow": "npx babel-node ./.bin/definitions-flow.js", | ||
"defs:typescript": "npx babel-node ./.bin/definitions-typescript.js", | ||
"clean": "rimraf dist", | ||
"prebuild": "npm run clean", | ||
"build": "babel src -d dist", | ||
"postbuild": "npm run defs:flow && npm run defs:typescript", | ||
"watch": "babel src -d dist -w", | ||
"prepare": "npm run clean && npm run build" | ||
"prepare": "npm run build" | ||
} | ||
} |
<div align="center"> | ||
<h1>queue-promise</h1> | ||
[![Default CI/CD](https://github.com/Bartozzz/queue-promise/workflows/Default%20CI/CD/badge.svg)](https://github.com/Bartozzz/queue-promise/actions) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/Bartozzz/queue-promise/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Bartozzz/queue-promise?targetFile=package.json) | ||
[![Default CI/CD](https://github.com/Bartozzz/queue-promise/workflows/Default%20CI/CD/badge.svg)](https://github.com/Bartozzz/queue-promise/actions) | ||
[![npm package size](https://img.badgesize.io/Bartozzz/queue-promise/master/dist/index.js?compression=gzip)](https://www.npmjs.com/package/queue-promise) | ||
[![npm version](https://img.shields.io/npm/v/queue-promise.svg)](https://www.npmjs.com/package/queue-promise) | ||
@@ -39,6 +40,6 @@ [![npm dependency Status](https://david-dm.org/Bartozzz/queue-promise.svg)](https://www.npmjs.com/package/queue-promise) | ||
queue.enqueue(asyncTaskA); // resolved/rejected after 0s | ||
queue.enqueue(asyncTaskB); // resolved/rejected after 2s | ||
queue.enqueue(asyncTaskC); // resolved/rejected after 4s | ||
queue.enqueue(asyncTaskD); // resolved/rejected after 6s | ||
queue.enqueue(asyncTaskA); // resolved/rejected after 0ms | ||
queue.enqueue(asyncTaskB); // resolved/rejected after 2000ms | ||
queue.enqueue(asyncTaskC); // resolved/rejected after 4000ms | ||
queue.enqueue(asyncTaskD); // resolved/rejected after 6000ms | ||
``` | ||
@@ -99,2 +100,6 @@ | ||
**Note:** | ||
`.dequeue()` function throttles (is executed at most once per every `options.interval` milliseconds). | ||
#### **public** `.on(event, callback)` | ||
@@ -101,0 +106,0 @@ |
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Found 1 instance in 1 package
19862
6
24
163
0
30