diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..c9c217c --- /dev/null +++ b/index.d.ts @@ -0,0 +1,115 @@ +/** + * 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. + */ +declare type PProgressCallback = (percentage: number) => void; + +declare type PProgressExecutor = (resolve: (value?: T | PromiseLike) => void, reject: (reason?: any) => void, progress: PProgressCallback) => void; + +declare interface PProgressAllOptions { + /** + * Number of concurrently pending promises + * + * To run the promises in series, set it to 1 + * Default: Infinity, Minimum: 1 + */ + concurrency: number; +} + +/** + * Create a promise that reports progress + */ +declare class PProgress extends Promise { + + /** + * Convenience method to make your promise-returning or async function report progress + * @param input + */ + public static fn(input: any): void; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike, T10 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike, T9 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike, T8 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5, T6, T7, T8]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike, T7 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5, T6, T7]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike, T6 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5, T6]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike, T5 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4, T5]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike, T4 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3, T4]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike, T3 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2, T3]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: [T1 | PromiseLike, T2 | PromiseLike], options?: PProgressAllOptions): PProgress<[T1, T2]>; + + /** + * 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 values An array of Promises or PProgress instances. + * @returns A PProgress instance. + */ + public static all(values: (T | PromiseLike)[], options?: PProgressAllOptions): PProgress; + + /** + * The current progress percentage of the promise as a number between 0 and 1 + */ + public readonly progress: number; + + constructor(executor: PProgressExecutor); + + /** + * Accepts a function that gets instance.progress as an argument and is called for every progress event + * @param callback Function to call every time a progress is reported. Value passed will be number between 0 and 1 + */ + public onProgress(callback: (progress: number) => void): void; +} + +export default PProgress; diff --git a/package.json b/package.json index a8638f8..735abf0 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.1", "description": "Create a promise that reports progress", "license": "MIT", + "typings": "index.d.ts", "repository": "sindresorhus/p-progress", "author": { "name": "Sindre Sorhus",