From bd2d6b130d3426325c2c7217a0559511d774b4e5 Mon Sep 17 00:00:00 2001 From: Matthias Kunnen Date: Thu, 14 Feb 2019 03:18:42 +0100 Subject: [PATCH] fix(throttleTime): fix double emission with leading and trailing enabled Fix an issue with throttleTime emitting both leading and trailing values in the same time window. Double emission problem: source: a123b12-c-23d-2-ef--- expected: a---b---c---d---e---f actual: a---b1---c2---2-e---f Closes #2466 and #2727. Follows #2749 and #2864. BREAKING CHANGE: throttleTime no longer emits both leading and trailing values in the same time window. --- src/internal/operators/throttleTime.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/internal/operators/throttleTime.ts b/src/internal/operators/throttleTime.ts index baed7785c2..7f23be8fe8 100644 --- a/src/internal/operators/throttleTime.ts +++ b/src/internal/operators/throttleTime.ts @@ -130,7 +130,7 @@ class ThrottleTimeSubscriber extends Subscriber { this._hasTrailingValue = true; } } else { - this.add(this.throttled = this.scheduler.schedule>(dispatchNext, this.duration, { subscriber: this })); + this.throttle(); if (this.leading) { this.destination.next(value); } else if (this.trailing) { @@ -151,17 +151,27 @@ class ThrottleTimeSubscriber extends Subscriber { clearThrottle() { const throttled = this.throttled; + if (throttled) { + throttled.unsubscribe(); + this.remove(throttled); + this.throttled = null; + if (this.trailing && this._hasTrailingValue) { this.destination.next(this._trailingValue); this._trailingValue = null; this._hasTrailingValue = false; + + if (this.leading && this.trailing) { + this.throttle(); + } } - throttled.unsubscribe(); - this.remove(throttled); - this.throttled = null; } } + + throttle() { + this.add(this.throttled = this.scheduler.schedule>(dispatchNext, this.duration, { subscriber: this })); + } } interface DispatchArg {