mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 04:50:18 +00:00
[p-queue] introduce typings (#18303)
This commit is contained in:
committed by
Wesley Wigham
parent
01764b65da
commit
29fc3e0735
Vendored
+50
@@ -0,0 +1,50 @@
|
||||
// Type definitions for p-queue 1.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
|
||||
|
||||
export = PQueue;
|
||||
|
||||
declare class PQueue<O extends PQueue.QueueAddOptions = PQueue.DefaultAddOptions> {
|
||||
size: number;
|
||||
pending: number;
|
||||
|
||||
constructor(opts?: PQueue.Options<O>);
|
||||
|
||||
add<T>(fn: PQueue.Task<T>, opts?: O): Promise<T>;
|
||||
|
||||
onEmpty(): Promise<void>;
|
||||
|
||||
clear(): void;
|
||||
}
|
||||
|
||||
declare namespace PQueue {
|
||||
interface QueueAddOptions {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface QueueClassConstructor<O extends QueueAddOptions> {
|
||||
new(): QueueClass<O>;
|
||||
}
|
||||
|
||||
interface QueueClass<O extends QueueAddOptions> {
|
||||
size: number;
|
||||
|
||||
enqueue(run: () => void, options?: O): void;
|
||||
|
||||
dequeue(): (() => void) | undefined;
|
||||
}
|
||||
|
||||
interface Options<O extends QueueAddOptions> {
|
||||
queueClass?: QueueClassConstructor<O>;
|
||||
concurrency?: number;
|
||||
}
|
||||
|
||||
interface DefaultAddOptions {
|
||||
priority?: number;
|
||||
}
|
||||
|
||||
type Task<T> = () => Promise<T>;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import PQueue = require('p-queue');
|
||||
|
||||
const queue = new PQueue({concurrency: 1});
|
||||
|
||||
queue.add(() => Promise.resolve('sindresorhus.com')).then((sindre) => {
|
||||
const str: string = sindre;
|
||||
});
|
||||
|
||||
Promise.resolve((): Promise<string> => Promise.resolve('unicorn'))
|
||||
.then(task => queue.add(task, {priority: 5}))
|
||||
.then(unicorn => {
|
||||
const str: string = unicorn;
|
||||
});
|
||||
|
||||
queue.onEmpty().then(() => {
|
||||
});
|
||||
queue.clear();
|
||||
|
||||
let num: number;
|
||||
num = queue.size;
|
||||
num = queue.pending;
|
||||
|
||||
class QueueClass implements PQueue.QueueClass<{ any: string }> {
|
||||
private queue: Array<() => void>;
|
||||
|
||||
size = 0;
|
||||
|
||||
constructor() {
|
||||
this.queue = [];
|
||||
}
|
||||
|
||||
enqueue(run: () => void, options: { any: string }) {
|
||||
this.queue.push(run);
|
||||
}
|
||||
|
||||
dequeue() {
|
||||
return this.queue.shift();
|
||||
}
|
||||
}
|
||||
|
||||
const queue2 = new PQueue({queueClass: QueueClass});
|
||||
queue2.add(() => Promise.resolve(), {any: 'hi'});
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"p-queue-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user