-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[p-queue] introduce typings (#18303)
- Loading branch information
1 parent
01764b6
commit 29fc3e0
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "extends": "dtslint/dt.json" } |