Merge pull request #33382 from BendingBender/p-queue

[p-queue] Update types to v3.1
This commit is contained in:
Jesse Trinity
2019-02-28 10:36:47 -08:00
committed by GitHub
2 changed files with 99 additions and 45 deletions
+78 -24
View File
@@ -1,45 +1,56 @@
// Type definitions for p-queue 3.0
// Type definitions for p-queue 3.1
// Project: https://github.com/sindresorhus/p-queue#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Evan Shortiss <https://github.com/evanshortiss>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 2.4
/// <reference types="node" />
import { EventEmitter } from 'events';
export = PQueue;
declare class PQueue<O extends PQueue.QueueAddOptions = PQueue.DefaultAddOptions> {
/**
* Promise queue with concurrency control.
*/
declare class PQueue<
TEnqueueOptions extends PQueue.QueueAddOptions = PQueue.DefaultAddOptions
> extends EventEmitter {
/**
* Size of the queue.
*/
size: number;
readonly size: number;
/**
* Number of pending promises.
*/
pending: number;
readonly pending: number;
/**
* Whether the queue is currently paused.
*/
isPaused: boolean;
readonly isPaused: boolean;
constructor(opts?: PQueue.Options<O>);
constructor(opts?: PQueue.Options<TEnqueueOptions>);
/**
* Returns the promise returned by calling fn.
* Adds a sync or async task to the queue. Always returns a promise.
* @param fn Promise-returning/async function.
* @param opts
*/
add<T>(fn: PQueue.Task<T>, opts?: O): Promise<T>;
add<T>(fn: PQueue.Task<T>, opts?: TEnqueueOptions): Promise<T>;
/**
* Same as .add(), but accepts an array of async functions and
* returns a promise that resolves when all async functions are resolved.
* Same as `.add()`, but accepts an array of sync or async functions
* and returns a promise that resolves when all functions are resolved.
* @param fn Array of Promise-returning/async functions.
*/
addAll<TAll>(fns: Array<PQueue.Task<TAll>>, opts?: O): Promise<TAll[]>;
addAll<TAll>(fns: Array<PQueue.Task<TAll>>, opts?: TEnqueueOptions): Promise<TAll[]>;
/**
* Returns a promise that settles when the queue becomes empty.
*
* Can be called multiple times. Useful if you for example add
* additional items at a later time.
*/
@@ -47,17 +58,18 @@ declare class PQueue<O extends PQueue.QueueAddOptions = PQueue.DefaultAddOptions
/**
* Returns a promise that settles when the queue becomes empty, and all
* promises have completed; queue.size === 0 && queue.pending === 0.
* The difference with .onEmpty is that .onIdle guarantees that all work
* from the queue has finished. .onEmpty merely signals that the queue is
* promises have completed; `queue.size === 0 && queue.pending === 0`.
*
* The difference with `.onEmpty` is that `.onIdle` guarantees that all work
* from the queue has finished. `.onEmpty` merely signals that the queue is
* empty, but it could mean that some promises haven't completed yet.
*/
onIdle(): Promise<void>;
/**
* Start (or resume) executing enqueued tasks within concurrency limit.
* No need to call this if queue is not paused (via options.autoStart = false
* or by .pause() method.)
* No need to call this if queue is not paused
* (via `options.autoStart = false` or by `.pause()` method.)
*/
start(): void;
@@ -70,6 +82,20 @@ declare class PQueue<O extends PQueue.QueueAddOptions = PQueue.DefaultAddOptions
* Put queue execution on hold.
*/
pause(): void;
addListener(event: 'active', listener: () => void): this;
on(event: 'active', listener: () => void): this;
once(event: 'active', listener: () => void): this;
prependListener(event: 'active', listener: () => void): this;
prependOnceListener(event: 'active', listener: () => void): this;
removeListener(event: 'active', listener: () => void): this;
off(event: 'active', listener: () => void): this;
removeAllListeners(event?: 'active'): this;
listeners(event: 'active'): Array<() => void>;
rawListeners(event: 'active'): Array<() => void>;
emit(event: 'active'): boolean;
eventNames(): Array<'active'>;
listenerCount(type: 'active'): number;
}
declare namespace PQueue {
@@ -77,30 +103,58 @@ declare namespace PQueue {
[key: string]: any;
}
interface QueueClassConstructor<O extends QueueAddOptions> {
new(): QueueClass<O>;
interface QueueClassConstructor<TEnqueueOptions extends QueueAddOptions> {
new (): QueueClass<TEnqueueOptions>;
}
interface QueueClass<O extends QueueAddOptions> {
interface QueueClass<TEnqueueOptions extends QueueAddOptions> {
size: number;
enqueue(run: () => void, options?: O): void;
enqueue(run: () => void, options?: TEnqueueOptions): void;
dequeue(): (() => void) | undefined;
}
interface Options<O extends QueueAddOptions> {
interface Options<TEnqueueOptions extends QueueAddOptions> {
/**
* Concurrency limit. Minimum: `1`.
* @default Infinity
*/
concurrency?: number;
/**
* Whether queue tasks within concurrency limit, are auto-executed as soon as they're added.
* @default true
*/
autoStart?: boolean;
queueClass?: QueueClassConstructor<O>;
/**
* Class with a `enqueue` and `dequeue` method, and a `size` getter. See the
* [Custom QueueClass](https://github.com/sindresorhus/p-queue#custom-queueclass) section.
*/
queueClass?: QueueClassConstructor<TEnqueueOptions>;
/**
* The max number of runs in the given interval of time. Minimum: `1`.
* @default Infinity
*/
intervalCap?: number;
/**
* The length of time in milliseconds before the interval count resets. Must be finite. Minimum: `0`.
* @default 0
*/
interval?: number;
/**
* Whether the task must finish in the given interval or will be carried over into the next interval count.
* @default false
*/
carryoverConcurrencyCount?: boolean;
}
interface DefaultAddOptions {
/**
* Priority of operation. Operations with greater priority will be scheduled first.
* @default 0
*/
priority?: number;
}
type Task<T> = () => Promise<T>;
type Task<T> = (() => PromiseLike<T>) | (() => T);
}
+21 -21
View File
@@ -1,31 +1,31 @@
import PQueue = require('p-queue');
const queue = new PQueue({concurrency: 1});
const queue = new PQueue({ concurrency: 1 });
new PQueue({ autoStart: false });
new PQueue({ intervalCap: 1 });
new PQueue({ interval: 0 });
new PQueue({ carryoverConcurrencyCount: true });
queue.add(() => Promise.resolve('sindresorhus.com')).then((sindre) => {
const str: string = sindre;
});
queue.add(() => Promise.resolve('sindresorhus.com')); // $ExpectType Promise<string>
queue.add(() => 'sindresorhus.com'); // $ExpectType Promise<string>
queue.add(() => 'sindresorhus.com', { priority: 1 }); // $ExpectType Promise<string>
queue.addAll([() => Promise.resolve('oh'), () => Promise.resolve('hi')]).then(r => {
r.indexOf('h');
});
queue.addAll([() => Promise.resolve('oh'), () => 'hi']); // $ExpectType Promise<string[]>
queue.addAll<string | number>([() => Promise.resolve('oh'), () => 1]); // $ExpectType Promise<(string | number)[]>
queue.addAll([() => Promise.resolve('oh'), () => 'hi'], { priority: 1 }); // $ExpectType Promise<string[]>
Promise.resolve((): Promise<string> => Promise.resolve('unicorn'))
.then(task => queue.add(task, {priority: 5}))
.then(unicorn => {
const str: string = unicorn;
});
queue.onEmpty().then(() => {});
queue.onIdle().then(() => {});
queue.onEmpty(); // $ExpectType Promise<void>
queue.onIdle(); // $ExpectType Promise<void>
queue.start();
queue.pause();
queue.clear();
let num: number;
num = queue.size;
num = queue.pending;
const paused = queue.isPaused;
queue.size; // $ExpectType number
queue.size = 1; // $ExpectError
queue.pending; // $ExpectType number
queue.pending = 5; // $ExpectError
queue.isPaused; // $ExpectType boolean
queue.isPaused = true; // $ExpectError
class QueueClass implements PQueue.QueueClass<{ any: string }> {
private readonly queue: Array<() => void>;
@@ -45,5 +45,5 @@ class QueueClass implements PQueue.QueueClass<{ any: string }> {
}
}
const queue2 = new PQueue({queueClass: QueueClass});
queue2.add(() => Promise.resolve(), {any: 'hi'});
const queue2 = new PQueue({ queueClass: QueueClass });
queue2.add(() => Promise.resolve(), { any: 'hi' });