Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw if a progress is less than last value #9

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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