Skip to content

Commit

Permalink
fix(throttleTime): emit throttled values when complete if trailing=tr…
Browse files Browse the repository at this point in the history
…ue (#3559)

Treat complete as a trailing edge and emit the last throttled value when complete before the throttle time is up.

closes #3351
  • Loading branch information
yozo1984 authored and benlesh committed Apr 13, 2018
1 parent c365405 commit 3e846f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/operators/throttleTime-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,17 @@ describe('Observable.prototype.throttleTime', () => {
expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should emit the last throttled value when complete', () => {
const e1 = hot('-a-xy-----b--x--cxx|');
const e1subs = '^ !';
const t = time('----| ');
const expected = '-----y--------x----(x|)';

const result = e1.throttleTime(t, rxTestScheduler, { leading: false, trailing: true });

expectObservable(result).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});
});
9 changes: 9 additions & 0 deletions src/internal/operators/throttleTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ class ThrottleTimeSubscriber<T> extends Subscriber<T> {
}
}

protected _complete() {
if (this._hasTrailingValue) {
this.destination.next(this._trailingValue);
this.destination.complete();
} else {
this.destination.complete();
}
}

clearThrottle() {
const throttled = this.throttled;
if (throttled) {
Expand Down

0 comments on commit 3e846f2

Please sign in to comment.