Skip to content

Commit

Permalink
fix(throttleTime): fix double emission with leading and trailing enabled
Browse files Browse the repository at this point in the history
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 ReactiveX#2466 and ReactiveX#2727.
Follows ReactiveX#2749 and ReactiveX#2864.

BREAKING CHANGE: throttleTime no longer emits both leading and trailing
values in the same time window.
  • Loading branch information
MatthiasKunnen committed Apr 23, 2019
1 parent 76e43f3 commit 7dbbb96
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/internal/operators/throttleTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> {
this._hasTrailingValue = true;
}
} else {
this.add(this.throttled = this.scheduler.schedule<DispatchArg<T>>(dispatchNext, this.duration, { subscriber: this }));
this.throttle();
if (this.leading) {
this.destination.next(value);
} else if (this.trailing) {
Expand All @@ -151,17 +151,27 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> {

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<DispatchArg<T>>(dispatchNext, this.duration, { subscriber: this }));
}
}

interface DispatchArg<T> {
Expand Down

0 comments on commit 7dbbb96

Please sign in to comment.