Skip to content

Commit

Permalink
Do not throw if a progress is less than last value (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Allen authored Feb 21, 2020
1 parent a24163f commit 33535c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ declare class PProgress<ValueType> extends Promise<ValueType> {
/**
@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.
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.
*/
Expand Down
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ class PProgress extends Promise {
// We wait for the next microtask tick so `super` is called before we use `this`
await Promise.resolve();

if (progress === this._progress) {
// Note: we don't really have guarantees over
// the order in which async operations are evaluated,
// so if we get an out-of-order progress, we'll just discard it.
if (progress <= this._progress) {
return;
}

if (progress < this._progress) {
throw new Error('The progress percentage can\'t be lower than the last progress event');
}

this._progress = progress;

for (const listener of this._listeners) {
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Type: `Function`

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.
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.

Expand Down

0 comments on commit 33535c8

Please sign in to comment.