Comparing version 3.0.0 to 3.1.0
'use strict'; | ||
const EventEmitter = require('events'); | ||
// Port of lower_bound from http://en.cppreference.com/w/cpp/algorithm/lower_bound | ||
@@ -54,4 +56,6 @@ // Used to compute insertion index to keep queue sorted after insertion | ||
class PQueue { | ||
class PQueue extends EventEmitter { | ||
constructor(options) { | ||
super(); | ||
options = Object.assign({ | ||
@@ -74,3 +78,3 @@ carryoverConcurrencyCount: false, | ||
if (!(typeof options.interval === 'number' && Number.isFinite(options.interval) && options.interval >= 0)) { | ||
if (!(Number.isFinite(options.interval) && options.interval >= 0)) { | ||
throw new TypeError(`Expected \`interval\` to be a finite number >= 0, got \`${options.interval}\` (${typeof options.interval})`); | ||
@@ -164,2 +168,3 @@ } | ||
if (this._doesIntervalAllowAnother && this._doesConcurrentAllowAnother) { | ||
this.emit('active'); | ||
this.queue.dequeue()(); | ||
@@ -166,0 +171,0 @@ if (canInitializeInterval) { |
{ | ||
"name": "p-queue", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Promise queue with concurrency control", | ||
@@ -11,3 +11,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava", | ||
"test": "xo && nyc ava", | ||
"bench": "node bench.js" | ||
@@ -43,4 +43,6 @@ }, | ||
"benchmark": "^2.1.2", | ||
"codecov": "^3.1.0", | ||
"delay": "^3.1.0", | ||
"in-range": "^1.0.0", | ||
"nyc": "^13.0.1", | ||
"random-int": "^1.0.0", | ||
@@ -47,0 +49,0 @@ "time-span": "^2.0.0", |
@@ -1,2 +0,2 @@ | ||
# p-queue [![Build Status](https://travis-ci.org/sindresorhus/p-queue.svg?branch=master)](https://travis-ci.org/sindresorhus/p-queue) | ||
# p-queue [![Build Status](https://travis-ci.org/sindresorhus/p-queue.svg?branch=master)](https://travis-ci.org/sindresorhus/p-queue) [![codecov](https://codecov.io/gh/sindresorhus/p-queue/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/p-queue) | ||
@@ -43,3 +43,3 @@ > Promise queue with concurrency control | ||
Returns a new `queue` instance. | ||
Returns a new `queue` instance, which is an [`EventEmitter`](https://nodejs.org/api/events.html) subclass. | ||
@@ -159,2 +159,28 @@ #### options | ||
## Events | ||
#### active | ||
Emitted as each item is processed in the queue for the purpose of tracking progress. | ||
```js | ||
const delay = require('delay'); | ||
const PQueue = require('p-queue'); | ||
const queue = new PQueue({concurrency: 2}); | ||
let count = 0; | ||
queue.on('active', () => { | ||
console.log(`Working on item #${++count}. Size: ${queue.size} Pending: ${queue.pending}`); | ||
}); | ||
queue.add(() => Promise.resolve()); | ||
queue.add(() => delay(2000)); | ||
queue.add(() => Promise.resolve()); | ||
queue.add(() => Promise.resolve()); | ||
queue.add(() => delay(500)); | ||
``` | ||
## Advanced example | ||
@@ -161,0 +187,0 @@ |
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
14807
235
279
9