queue-promise
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -13,7 +13,13 @@ // Minimum TypeScript Version: 3.0 | ||
interface State { | ||
IDLE: 0; | ||
RUNNING: 1; | ||
STOPPED: 2; | ||
} | ||
class Queue extends EventEmitter { | ||
readonly options: QueueOptions; | ||
readonly started: boolean; | ||
readonly stopped: boolean; | ||
readonly state: State; | ||
readonly isEmpty: boolean; | ||
readonly shouldRun: boolean; | ||
@@ -20,0 +26,0 @@ constructor(options?: QueueOptions); |
@@ -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,"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.dequeue()})())}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; | ||
"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}const State={IDLE:0,RUNNING:1,STOPPED:2};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,"state",State.IDLE),_defineProperty(this,"options",{concurrent:5,interval:500,start:!0}),this.options=_objectSpread(_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.state===State.RUNNING||this.isEmpty||(this.state=State.RUNNING,this.emit("start"),(async()=>{for(;this.shouldRun;)await this.dequeue()})())}stop(){clearTimeout(this.timeoutId),this.state=State.STOPPED,this.emit("stop")}finalize(){this.currentlyHandled-=1,0===this.currentlyHandled&&this.isEmpty&&(this.stop(),this.state=State.IDLE,this.emit("end"))}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.uniqueId=(this.uniqueId+1)%Number.MAX_SAFE_INTEGER,this.tasks.set(this.uniqueId,a),this.options.start&&this.state!==State.STOPPED&&this.start()}add(a){this.enqueue(a)}clear(){this.tasks.clear()}get isEmpty(){return 0===this.tasks.size}get shouldRun(){return!this.isEmpty&&this.state!==State.STOPPED}}exports.default=Queue,module.exports=exports.default; |
{ | ||
"name": "queue-promise", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"keywords": [ | ||
@@ -41,12 +41,17 @@ "queue", | ||
"@babel/register": "^7.9.0", | ||
"@types/node": "13.9.5", | ||
"@commitlint/cli": "^9.0.1", | ||
"@commitlint/config-conventional": "^9.0.1", | ||
"@saithodev/semantic-release-backmerge": "1.1.1", | ||
"@semantic-release/changelog": "5.0.1", | ||
"@semantic-release/git": "9.0.0", | ||
"@types/node": "14.0.14", | ||
"babel-eslint": "^10.1.0", | ||
"babel-plugin-add-module-exports": "^1.0.2", | ||
"babel-preset-minify": "0.5.1", | ||
"babel-preset-minify": "^0.5.1", | ||
"chai": "^4.2.0", | ||
"dtslint": "^3.4.0", | ||
"eslint": "^6.8.0", | ||
"eslint": "^7.2.0", | ||
"eslint-config-prettier": "^6.10.1", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-flowtype": "^4.7.0", | ||
"eslint-plugin-flowtype": "^5.1.3", | ||
"eslint-plugin-import": "^2.20.1", | ||
@@ -57,25 +62,34 @@ "eslint-plugin-node": "^11.0.0", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"flow-bin": "^0.121.0", | ||
"flow-bin": "^0.128.0", | ||
"fs-extra": "^9.0.0", | ||
"glob": "^7.1.6", | ||
"mocha": "^7.1.1", | ||
"husky": "^4.2.5", | ||
"mocha": "^8.0.1", | ||
"prettier": "^2.0.2", | ||
"rimraf": "^3.0.2" | ||
"rimraf": "^3.0.2", | ||
"semantic-release": "17.1.1" | ||
}, | ||
"scripts": { | ||
"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": "npm run test:lint && npm run test:flow && npm run test:typescript && npm run test:unit", | ||
"test:flow": "npx flow", | ||
"test:typescript": "npx dtslint types", | ||
"test:mocha": "mocha --require @babel/register", | ||
"test:eslint": "eslint src", | ||
"test:unit": "npx mocha --require @babel/register", | ||
"test:lint": "npx eslint src", | ||
"defs:flow": "npx babel-node ./.bin/definitions-flow.js", | ||
"defs:typescript": "npx babel-node ./.bin/definitions-typescript.js", | ||
"clean": "rimraf dist", | ||
"clean": "npx rimraf dist", | ||
"prebuild": "npm run clean", | ||
"build": "babel src -d dist", | ||
"build": "npx babel src -d dist", | ||
"postbuild": "npm run defs:flow && npm run defs:typescript", | ||
"watch": "babel src -d dist -w", | ||
"prepare": "npm run build" | ||
"watch": "npx babel src -d dist -w", | ||
"prepare": "npm run build", | ||
"prerelease": "npm run build", | ||
"release": "git add dist/* && standard-version -a" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS" | ||
} | ||
} | ||
} |
@@ -24,2 +24,4 @@ <div align="center"> | ||
### With automatic start | ||
```javascript | ||
@@ -30,4 +32,3 @@ import Queue from "queue-promise"; | ||
concurrent: 1, | ||
interval: 2000, | ||
start: true | ||
interval: 2000 | ||
}); | ||
@@ -45,5 +46,27 @@ | ||
queue.enqueue(asyncTaskC); // resolved/rejected after 4000ms | ||
queue.enqueue(asyncTaskD); // resolved/rejected after 6000ms | ||
``` | ||
### Without automatic start | ||
```javascript | ||
import Queue from "queue-promise"; | ||
const queue = new Queue({ | ||
concurrent: 1, | ||
interval: 2000, | ||
start: false, | ||
}); | ||
queue.enqueue(asyncTaskA); | ||
queue.enqueue(asyncTaskB); | ||
queue.enqueue(asyncTaskC); | ||
while (queue.shouldRun) { | ||
// 1st iteration after 2000ms | ||
// 2nd iteration after 4000ms | ||
// 3rd iteration after 6000ms | ||
const data = await queue.dequeue(); | ||
} | ||
``` | ||
## API | ||
@@ -161,2 +184,6 @@ | ||
#### **public** `.shouldRun` | ||
Checks whether the queue is not empty and not stopped. | ||
## Tests | ||
@@ -167,1 +194,45 @@ | ||
``` | ||
## Contributing | ||
### Development | ||
We have prepared multiple commands to help you develop `queue-promise` on your own. You will need a local copy of [Node.js](https://nodejs.org/en/) installed on your machine. Then, install project dependencies using the following command: | ||
```bash | ||
$ npm install | ||
``` | ||
#### Usage | ||
```bash | ||
$ npm run <command> | ||
``` | ||
#### List of commands | ||
| Command | Description | | ||
| :---------------- | :------------------------------------------------------- | | ||
| `test` | Run all `test:*` commands described below. | | ||
| `test:flow` | Test Flow types. | | ||
| `test:typescript` | Test TypeScript types. | | ||
| `test:unit` | Run unit tests. | | ||
| `test:lint` | Run linter tests. | | ||
| `defs:flow` | Build Flow type definitions. | | ||
| `defs:typescript` | Build TypeScript type definitions. | | ||
| `clean` | Clean `dist` directory. | | ||
| `build` | Build package and generate type definitions. | | ||
| `watch` | Build package in watch mode. | | ||
| `release` | Bump package version and generate a `CHANGELOG.md` file. | | ||
### License | ||
`queue-promise` was created and developed by [Bartosz Łaniewski](https://github.com/Bartozzz). The full list of contributors can be found [here](https://github.com/Bartozzz/queue-promise/graphs/contributors). The package is [MIT licensed](https://github.com/Bartozzz/queue-promise/blob/master/LICENSE). | ||
### Bug reporting | ||
[![Github Open Issues](https://img.shields.io/github/issues-raw/Bartozzz/queue-promise.svg)](https://github.com/Bartozzz/queue-promise/issues) | ||
[![Github Closed Issues](https://img.shields.io/github/issues-closed-raw/Bartozzz/queue-promise.svg)](https://github.com/Bartozzz/queue-promise/issues?q=is%3Aissue+is%3Aclosed) | ||
[![Github Pull Requests](https://img.shields.io/github/issues-pr-raw/Bartozzz/queue-promise.svg)](https://github.com/Bartozzz/queue-promise/pulls) | ||
**We want contributing to `queue-promise` to be fun, enjoyable, and educational for anyone, and everyone.** Changes and improvements are more than welcome! Feel free to fork and open a pull request. We use [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) for commit messages. If you have found any issues, please [report them here](https://github.com/Bartozzz/queue-promise/new) - they are being tracked on [GitHub Issues](https://github.com/Bartozzz/queue-promise/issues). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
28595
7
29
234
37