Comparing version 6.2.1 to 6.3.0
import EventEmitter = require('eventemitter3'); | ||
import { Queue } from './queue'; | ||
import { Queue, RunFunction } from './queue'; | ||
import PriorityQueue from './priority-queue'; | ||
@@ -9,3 +9,3 @@ import { QueueAddOptions, DefaultAddOptions, Options } from './options'; | ||
*/ | ||
export default class PQueue<QueueType extends Queue<EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions> extends EventEmitter<'active'> { | ||
export default class PQueue<QueueType extends Queue<RunFunction, EnqueueOptionsType> = PriorityQueue, EnqueueOptionsType extends QueueAddOptions = DefaultAddOptions> extends EventEmitter<'active'> { | ||
private readonly _carryoverConcurrencyCount; | ||
@@ -83,2 +83,8 @@ private readonly _isIntervalIgnored; | ||
/** | ||
Size of the queue, filtered by the given options. | ||
For example, this can be used to find the number of items remaining in the queue with a specific priority level. | ||
*/ | ||
sizeBy(options: Partial<EnqueueOptionsType>): number; | ||
/** | ||
Number of pending promises. | ||
@@ -85,0 +91,0 @@ */ |
@@ -340,2 +340,10 @@ "use strict"; | ||
/** | ||
Size of the queue, filtered by the given options. | ||
For example, this can be used to find the number of items remaining in the queue with a specific priority level. | ||
*/ | ||
sizeBy(options) { | ||
return this._queue.filter(options).length; | ||
} | ||
/** | ||
Number of pending promises. | ||
@@ -342,0 +350,0 @@ */ |
@@ -1,6 +0,6 @@ | ||
import { Queue } from './queue'; | ||
import { Queue, RunFunction } from './queue'; | ||
export interface QueueAddOptions { | ||
readonly [key: string]: unknown; | ||
} | ||
export interface Options<QueueType extends Queue<QueueOptions>, QueueOptions extends QueueAddOptions> { | ||
export interface Options<QueueType extends Queue<RunFunction, QueueOptions>, QueueOptions extends QueueAddOptions> { | ||
/** | ||
@@ -7,0 +7,0 @@ Concurrency limit. |
@@ -6,7 +6,8 @@ import { Queue, RunFunction } from './queue'; | ||
} | ||
export default class PriorityQueue implements Queue<PriorityQueueOptions> { | ||
export default class PriorityQueue implements Queue<RunFunction, PriorityQueueOptions> { | ||
private readonly _queue; | ||
enqueue(run: RunFunction, options?: Partial<PriorityQueueOptions>): void; | ||
dequeue(): RunFunction | undefined; | ||
filter(options: Partial<PriorityQueueOptions>): RunFunction[]; | ||
get size(): number; | ||
} |
@@ -30,2 +30,5 @@ "use strict"; | ||
} | ||
filter(options) { | ||
return this._queue.filter(element => element.priority === options.priority).map(element => element.run); | ||
} | ||
get size() { | ||
@@ -32,0 +35,0 @@ return this._queue.length; |
export declare type RunFunction = () => Promise<unknown>; | ||
export interface Queue<Options> { | ||
export interface Queue<Element, Options> { | ||
size: number; | ||
dequeue(): RunFunction | undefined; | ||
enqueue(run: RunFunction, options?: Partial<Options>): void; | ||
filter(options: Partial<Options>): Element[]; | ||
dequeue(): Element | undefined; | ||
enqueue(run: Element, options?: Partial<Options>): void; | ||
} |
{ | ||
"name": "p-queue", | ||
"version": "6.2.1", | ||
"version": "6.3.0", | ||
"description": "Promise queue with concurrency control", | ||
@@ -64,3 +64,3 @@ "license": "MIT", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.7.2", | ||
"typescript": "3.7.2", | ||
"xo": "^0.25.3" | ||
@@ -90,3 +90,4 @@ }, | ||
"@typescript-eslint/require-await": "off", | ||
"@typescript-eslint/no-misused-promises": "off" | ||
"@typescript-eslint/no-misused-promises": "off", | ||
"require-atomic-updates": "off" | ||
} | ||
@@ -93,0 +94,0 @@ }, |
@@ -169,2 +169,23 @@ # 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) | ||
#### .sizeBy(options) | ||
Size of the queue, filtered by the given options. | ||
For example, this can be used to find the number of items remaining in the queue with a specific priority level. | ||
```js | ||
const queue = new PQueue(); | ||
queue.add(async () => 'π¦', {priority: 1}); | ||
queue.add(async () => 'π¦', {priority: 0}); | ||
queue.add(async () => 'π¦', {priority: 1}); | ||
console.log(queue.sizeBy({priority: 1})); | ||
//=> 2 | ||
console.log(queue.sizeBy({priority: 0})); | ||
//=> 1 | ||
``` | ||
#### .pending | ||
@@ -288,11 +309,18 @@ | ||
} | ||
enqueue(run, options) { | ||
this._queue.push(run); | ||
} | ||
dequeue() { | ||
return this._queue.shift(); | ||
} | ||
get size() { | ||
return this._queue.length; | ||
} | ||
filter(options) { | ||
return this._queue; | ||
} | ||
} | ||
@@ -299,0 +327,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
31643
598
350