From 33535c8f8addec7093a3727cef0b57c4a665b595 Mon Sep 17 00:00:00 2001 From: Tim Allen Date: Fri, 21 Feb 2020 22:45:54 +1100 Subject: [PATCH] Do not throw if a progress is less than last value (#9) --- index.d.ts | 3 ++- index.js | 9 ++++----- readme.md | 3 ++- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 3d8c435..81dd26a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -762,7 +762,8 @@ declare class PProgress extends Promise { /** @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. */ diff --git a/index.js b/index.js index 3b42437..d5a5223 100644 --- a/index.js +++ b/index.js @@ -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) { diff --git a/readme.md b/readme.md index ba2db46..59b4d54 100644 --- a/readme.md +++ b/readme.md @@ -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.