Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 3, 2021
1 parent 81c597b commit fba496b
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 207 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
matrix:
node-version:
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const allProgressPromise = PProgress.all([
delay(103),
progressPromise(),
delay(55),
delay(209)
delay(209),
]);

allProgressPromise.onProgress(console.log);
Expand Down
117 changes: 59 additions & 58 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: Use the built-in type when TS 4.5 is out.
type Awaited<ValueType> = ValueType extends undefined ? ValueType : ValueType extends PromiseLike<infer ResolveValueType> ? ResolveValueType : ValueType;

// https://github.com/microsoft/TypeScript/blob/582e404a1041ce95d22939b73f0b4d95be77c6ec/lib/lib.es2020.promise.d.ts#L21-L31
Expand All @@ -11,7 +12,7 @@ export type PromiseSettledResult<ResolveValueType> = {

export interface Options {
/**
Number of concurrently pending promises. Minimum: `1`.
The number of concurrently pending promises. Minimum: `1`.
To run the promises in series, set it to `1`.
Expand All @@ -27,65 +28,11 @@ export type PromiseFactory<ValueType> = () => PromiseLike<ValueType>;
export type ProgressNotifier = (progress: number) => void;

// @ts-expect-error `Promise.all` currently uses an incompatible combinatorics-based type definition (https://github.com/microsoft/TypeScript/issues/39788)
export class PProgress<ValueType> extends Promise<ValueType> {
/**
The current progress percentage of the promise as a number between 0 and 1.
*/
readonly progress: number;

/**
Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).
@param executor - Same as the `Promise` constructor but with an appended `progress` parameter in `executor`.
@example
```
import {PProgress} from 'p-progress';
const progressPromise = new PProgress((resolve, reject, progress) => {
const job = new Job();
job.on('data', data => {
progress(data.length / job.totalSize);
});
job.on('finish', resolve);
job.on('error', reject);
});
progressPromise.onProgress(progress => {
console.log(`${progress * 100}%`);
//=> 9%
//=> 23%
//=> 59%
//=> 75%
//=> 100%
});
await progressPromise;
```
*/
constructor(
/**
@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.
Calling with a number lower than previously will be ignored.
Progress percentage `1` is reported for you when the promise resolves. If you set it yourself, it will simply be ignored.
*/
executor: (
resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
reject: (reason?: unknown) => void,
progress: ProgressNotifier
) => void
);

export class PProgress<ValueType> extends Promise<ValueType> { // eslint-disable-line @typescript-eslint/naming-convention
/**
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, similar to [p-all](https://github.com/sindresorhus/p-all).
@param promises - Promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).
@example
```
Expand Down Expand Up @@ -145,7 +92,7 @@ export class PProgress<ValueType> extends Promise<ValueType> {
/**
Like [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) but also exposes the total progress of all of the promises like `PProgress.all`.
@param promises - Array of promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).
@param promises - Promises or promise-returning functions, similar to [p-all](https://github.com/sindresorhus/p-all).
@example
```
Expand Down Expand Up @@ -211,6 +158,60 @@ export class PProgress<ValueType> extends Promise<ValueType> {
options?: Options
): PProgress<Iterable<PromiseSettledResult<ReturnValue>>>;

/**
The current progress percentage of the promise as a number between 0 and 1.
*/
readonly progress: number;

/**
Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise).
@param executor - Same as the `Promise` constructor but with an appended `progress` parameter in `executor`.
@example
```
import {PProgress} from 'p-progress';
const progressPromise = new PProgress((resolve, reject, progress) => {
const job = new Job();
job.on('data', data => {
progress(data.length / job.totalSize);
});
job.on('finish', resolve);
job.on('error', reject);
});
progressPromise.onProgress(progress => {
console.log(`${progress * 100}%`);
//=> 9%
//=> 23%
//=> 59%
//=> 75%
//=> 100%
});
await progressPromise;
```
*/
constructor(
/**
@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.
Calling with a number lower than previously will be ignored.
Progress percentage `1` is reported for you when the promise resolves. If you set it yourself, it will simply be ignored.
*/
executor: (
resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
reject: (reason?: unknown) => void,
progress: ProgressNotifier
) => void
);

/**
Accepts a function that gets `instance.progress` as an argument and is called for every progress event.
*/
Expand Down
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export class PProgress extends Promise {
static all(promises, options) {
return pProgress(async progress => {
if (
options && typeof options.concurrency === 'number' &&
!(promises.every(promise => typeof promise === 'function'))
options && typeof options.concurrency === 'number'
&& !(promises.every(promise => typeof promise === 'function'))
) {
throw new TypeError('When `options.concurrency` is set, the first argument must be an Array of Promise-returning functions');
}
Expand Down Expand Up @@ -51,8 +51,8 @@ export class PProgress extends Promise {
static allSettled(promises, {concurrency} = {}) {
return pProgress(async progress => {
if (
typeof concurrency === 'number' &&
!(promises.every(promise => typeof promise === 'function'))
typeof concurrency === 'number'
&& !(promises.every(promise => typeof promise === 'function'))
) {
throw new TypeError('When `options.concurrency` is set, the first argument must be an Array of Promise-returning functions');
}
Expand All @@ -78,12 +78,12 @@ export class PProgress extends Promise {
try {
return {
status: 'fulfilled',
value: await promise
value: await promise,
};
} catch (error) {
return {
status: 'rejected',
reason: error
reason: error,
};
} finally {
progressMap.set(promise, 1);
Expand All @@ -92,7 +92,7 @@ export class PProgress extends Promise {
};

return pTimes(promises.length, mapper, {
concurrency
concurrency,
});
});
}
Expand Down Expand Up @@ -133,7 +133,7 @@ export class PProgress extends Promise {
if (progress !== 1) {
setProgress(progress);
}
}
},
);
});

Expand All @@ -156,7 +156,6 @@ export class PProgress extends Promise {
}

then(onFulfilled, onRejected) {
// eslint-disable-next-line promise/prefer-await-to-then
const child = super.then(onFulfilled, onRejected);
this._listeners.add(progress => {
child._setProgress(progress);
Expand Down
Loading

0 comments on commit fba496b

Please sign in to comment.