mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-23 12:56:46 +08:00
Add the package p-progress
This commit is contained in:
175
types/p-progress/index.d.ts
vendored
Normal file
175
types/p-progress/index.d.ts
vendored
Normal file
@@ -0,0 +1,175 @@
|
||||
// Type definitions for p-progress 0.2
|
||||
// Project: https://github.com/sindresorhus/p-progress#readme
|
||||
// Definitions by: icopp <https://github.com/icopp>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.1
|
||||
|
||||
declare namespace pProgress {
|
||||
interface Options {
|
||||
/**
|
||||
* Number of concurrently pending promises.
|
||||
*
|
||||
* To run the promises in series, set it to 1.
|
||||
*
|
||||
* When this option is set, the first argument must be an array of
|
||||
* promise-returning functions.
|
||||
*
|
||||
* Minimum 1.
|
||||
*
|
||||
* @default {Infinity}
|
||||
*/
|
||||
concurrency?: number;
|
||||
}
|
||||
}
|
||||
|
||||
type PromiseFactory<T> = () => Promise<T> | PromiseLike<T>;
|
||||
|
||||
declare class PProgress<T> extends Promise<T> {
|
||||
/**
|
||||
* The current progress percentage of the promise as a number between 0 and
|
||||
* 1.
|
||||
*/
|
||||
progress: number;
|
||||
|
||||
/**
|
||||
* Convenience method to run multiple promises and get a total progress of
|
||||
* all of them. It counts normal promises with progress `0` when pending and
|
||||
* progress `1` when resolved. For PProgress type promises, it listens to
|
||||
* their onProgress() method for more fine grained progress reporting. You
|
||||
* can mix and match normal promises and PProgress promises.
|
||||
*
|
||||
* @param promises Array of promises or promise-returning functions.
|
||||
* @throws {TypeError}
|
||||
*/
|
||||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>,
|
||||
PromiseFactory<T6>,
|
||||
PromiseFactory<T7>,
|
||||
PromiseFactory<T8>,
|
||||
PromiseFactory<T9>,
|
||||
PromiseFactory<T10>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
|
||||
static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>,
|
||||
PromiseFactory<T6>,
|
||||
PromiseFactory<T7>,
|
||||
PromiseFactory<T8>,
|
||||
PromiseFactory<T9>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
|
||||
static all<T1, T2, T3, T4, T5, T6, T7, T8>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>,
|
||||
PromiseFactory<T6>,
|
||||
PromiseFactory<T7>,
|
||||
PromiseFactory<T8>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8]>;
|
||||
static all<T1, T2, T3, T4, T5, T6, T7>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>,
|
||||
PromiseFactory<T6>,
|
||||
PromiseFactory<T7>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5, T6, T7]>;
|
||||
static all<T1, T2, T3, T4, T5, T6>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>,
|
||||
PromiseFactory<T6>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5, T6]>;
|
||||
static all<T1, T2, T3, T4, T5>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>,
|
||||
PromiseFactory<T5>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4, T5]>;
|
||||
static all<T1, T2, T3, T4>(
|
||||
promises: [
|
||||
PromiseFactory<T1>,
|
||||
PromiseFactory<T2>,
|
||||
PromiseFactory<T3>,
|
||||
PromiseFactory<T4>
|
||||
],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3, T4]>;
|
||||
static all<T1, T2, T3>(
|
||||
promises: [PromiseFactory<T1>, PromiseFactory<T2>, PromiseFactory<T3>],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2, T3]>;
|
||||
static all<T1, T2>(
|
||||
promises: [PromiseFactory<T1>, PromiseFactory<T2>],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1, T2]>;
|
||||
static all<T1>(
|
||||
promises: [PromiseFactory<T1>],
|
||||
options?: pProgress.Options
|
||||
): PProgress<[T1]>;
|
||||
static all<TAll>(
|
||||
promises: Array<PromiseFactory<TAll>>,
|
||||
options?: pProgress.Options
|
||||
): PProgress<TAll>;
|
||||
|
||||
/**
|
||||
* Same as the Promise constructor, but with an appended `progress`
|
||||
* parameter in `executor`.
|
||||
*
|
||||
* @param progress
|
||||
* Call this with progress updates. It expects a number between 0 and 1.
|
||||
*
|
||||
* Multiple calls with the same number will result in only one
|
||||
* `onProgress()` event.
|
||||
*
|
||||
* Progress percentage `1` is reported for you when the promise resolves.
|
||||
* If you set it yourself, it will simply be ignored.
|
||||
*/
|
||||
constructor(
|
||||
executor: (
|
||||
resolve: (value?: T | PromiseLike<T>) => void,
|
||||
reject: (reason?: any) => void,
|
||||
progress: (progress: number) => void
|
||||
) => void
|
||||
);
|
||||
|
||||
/**
|
||||
* Accepts a function that gets `instance.progress` as an argument and is
|
||||
* called for every progress event.
|
||||
*
|
||||
* @throws {TypeError}
|
||||
*/
|
||||
onProgress(cb: (progress: number) => void): void;
|
||||
}
|
||||
|
||||
export = PProgress;
|
||||
31
types/p-progress/p-progress-tests.ts
Normal file
31
types/p-progress/p-progress-tests.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import PProgress = require("p-progress");
|
||||
|
||||
new PProgress(async (resolve, reject, progress) => {
|
||||
try {
|
||||
await Promise.resolve("sindresorhus.com");
|
||||
progress(0.5);
|
||||
|
||||
await Promise.resolve("ava.li");
|
||||
progress(1);
|
||||
|
||||
resolve();
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}).onProgress(progress => {
|
||||
progress;
|
||||
});
|
||||
|
||||
PProgress.all(
|
||||
[
|
||||
() => Promise.resolve("sindresorhus.com"),
|
||||
() => Promise.resolve("ava.li"),
|
||||
() => Promise.resolve(),
|
||||
() => Promise.resolve(1)
|
||||
],
|
||||
{ concurrency: 2 }
|
||||
).then(result => {
|
||||
const str: string = result[0];
|
||||
const str2: string = result[1];
|
||||
const num: number = result[3];
|
||||
});
|
||||
23
types/p-progress/tsconfig.json
Normal file
23
types/p-progress/tsconfig.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strictFunctionTypes": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"p-progress-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/p-progress/tslint.json
Normal file
1
types/p-progress/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user