queue-promise
Advanced tools
Comparing version 1.3.32 to 1.3.33
@@ -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.emit("start"),this.stopped=!1,this.started=!0,this.intervalId=setInterval(this.dequeue.bind(this),this.options.interval))}stop(){this.emit("stop"),this.stopped=!0,this.started=!1,clearInterval(this.intervalId)}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,"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; |
{ | ||
"name": "queue-promise", | ||
"version": "1.3.32", | ||
"version": "1.3.33", | ||
"keywords": [ | ||
@@ -31,11 +31,11 @@ "queue", | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.3", | ||
"@babel/core": "^7.8.3", | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.9.0", | ||
"@babel/plugin-proposal-async-generator-functions": "^7.8.3", | ||
"@babel/plugin-proposal-class-properties": "^7.8.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.8.3", | ||
"@babel/preset-env": "^7.8.3", | ||
"@babel/preset-flow": "^7.8.3", | ||
"@babel/register": "^7.8.3", | ||
"babel-eslint": "^10.0.3", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.9.0", | ||
"@babel/preset-env": "^7.9.0", | ||
"@babel/preset-flow": "^7.9.0", | ||
"@babel/register": "^7.9.0", | ||
"babel-eslint": "^10.1.0", | ||
"babel-plugin-add-module-exports": "^1.0.2", | ||
@@ -45,6 +45,6 @@ "babel-preset-minify": "0.5.1", | ||
"eslint": "^6.8.0", | ||
"eslint-config-prettier": "^6.9.0", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-plugin-flowtype": "^4.6.0", | ||
"eslint-plugin-import": "^2.20.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-flowtype": "^4.7.0", | ||
"eslint-plugin-import": "^2.20.1", | ||
"eslint-plugin-node": "^11.0.0", | ||
@@ -54,6 +54,6 @@ "eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"flow-bin": "^0.116.1", | ||
"mocha": "^7.0.1", | ||
"prettier": "^1.19.1", | ||
"rimraf": "^3.0.0" | ||
"flow-bin": "^0.121.0", | ||
"mocha": "^7.1.1", | ||
"prettier": "^2.0.2", | ||
"rimraf": "^3.0.2" | ||
}, | ||
@@ -60,0 +60,0 @@ "scripts": { |
<div align="center"> | ||
<h1>queue-promise</h1> | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/Bartozzz/queue-promise.svg)](https://greenkeeper.io/) | ||
[![Build Status](https://img.shields.io/travis/Bartozzz/queue-promise.svg)](https://travis-ci.org/Bartozzz/queue-promise/) | ||
[![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 version](https://img.shields.io/npm/v/queue-promise.svg)](https://www.npmjs.com/package/queue-promise) | ||
[![npm dependency Status](https://david-dm.org/Bartozzz/queue-promise.svg)](https://www.npmjs.com/package/queue-promise) | ||
[![npm downloads](https://img.shields.io/npm/dt/queue-promise.svg)](https://www.npmjs.com/package/queue-promise) | ||
<br> | ||
`queue-promise` is a small, dependency-free library for promise-based queues. It will resolve enqueued tasks concurrently at a given speed. When a task is being resolved or rejected, an event will be emitted. | ||
`queue-promise` is a small, dependency-free library for promise-based queues. It will execute enqueued tasks concurrently at a given speed. When a task is being resolved or rejected, an event is emitted. | ||
@@ -26,7 +27,4 @@ </div> | ||
const queue = new Queue({ | ||
// How many tasks should be executed in parallel (defaults to `5`): | ||
concurrent: 1, | ||
// How often should new tasks be executed (in ms – defaults to `500`): | ||
interval: 2000, | ||
// If should resolve new tasks automatically when added (defaults to `true`): | ||
start: true | ||
@@ -58,3 +56,3 @@ }); | ||
| `interval` | `500` | How often should new tasks be executed (in ms) | | ||
| `start` | `true` | Whether it should automatically resolve new tasks as soon as they are added | | ||
| `start` | `true` | Whether it should automatically execute new tasks as soon as they are added | | ||
@@ -72,7 +70,12 @@ #### **public** `.enqueue(tasks)`/`.add(tasks)` | ||
queue.enqueue(getRepos("userA")); | ||
queue.enqueue(getRepos("userB")); | ||
queue.enqueue(() => { | ||
return getRepos("userA"); | ||
}); | ||
queue.enqueue(async () => { | ||
await getRepos("userB"); | ||
}); | ||
// …equivalent to: | ||
queue.enqueue([getRepos("userA"), getRepos("userB")]); | ||
queue.enqueue([() => getRepos("userA"), () => getRepos("userB")]); | ||
``` | ||
@@ -82,3 +85,3 @@ | ||
Manually resolves _n_ concurrent (based od `options.concurrent`) promises from the queue. Uses global [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Is called automatically if `options.start` is set to `true`. Emits `resolve` and `reject` events. | ||
Executes _n_ concurrent (based od `options.concurrent`) promises from the queue. Uses global [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Is called automatically if `options.start` is set to `true`. Emits `resolve` and `reject` events. | ||
@@ -88,10 +91,10 @@ **Example:** | ||
```javascript | ||
queue.enqueue(getRepos("userA")); | ||
queue.enqueue(getRepos("userB")); | ||
queue.enqueue(() => getRepos("userA")); | ||
queue.enqueue(() => getRepos("userB")); | ||
// If "concurrent" is set to 1, only one promise is resolved on dequeue: | ||
// If "concurrent" is set to 1, only one promise is executed on dequeue: | ||
const userA = await queue.dequeue(); | ||
const userB = await queue.dequeue(); | ||
// If "concurrent" is set to 2, two promises are resolved concurrently: | ||
// If "concurrent" is set to 2, two promises are executed concurrently: | ||
const [userA, userB] = await queue.dequeue(); | ||
@@ -115,2 +118,6 @@ ``` | ||
**Note:** | ||
`dequeue`, `resolve` and `reject` events are emitted per task. This means that even if `concurrent` is set to `2`, `2` events will be emitted. | ||
#### **public** `.start()` | ||
@@ -121,6 +128,6 @@ | ||
```javascript | ||
queue.enqueue(getRepos("userA")); | ||
queue.enqueue(getRepos("userB")); | ||
queue.enqueue(getRepos("userC")); | ||
queue.enqueue(getRepos("userD")); | ||
queue.enqueue(() => getRepos("userA")); | ||
queue.enqueue(() => getRepos("userB")); | ||
queue.enqueue(() => getRepos("userC")); | ||
queue.enqueue(() => getRepos("userD")); | ||
queue.start(); | ||
@@ -135,3 +142,3 @@ | ||
Forces the queue to stop. New tasks will not be resolved automatically even if `options.start` was set to `true`. Emits `stop` event. | ||
Forces the queue to stop. New tasks will not be executed automatically even if `options.start` was set to `true`. Emits `stop` event. | ||
@@ -144,7 +151,7 @@ #### **public** `.clear()` | ||
Whether the queue has been started or not. | ||
Whether the queue is running. | ||
#### **public** `.stopped` | ||
Whether the queue has been forced to stop. | ||
Whether the queue has been forced to stop by calling `Queue.stop`. | ||
@@ -151,0 +158,0 @@ #### **public** `.isEmpty` |
10902
158