From ad0452f5fc6b885661e057ef9baf6f0777550d09 Mon Sep 17 00:00:00 2001 From: Zak Date: Thu, 12 Apr 2018 16:31:24 -0400 Subject: [PATCH] fix(throttleTime): emit throttled values when complete if trailing=true Treat complete as a trailing edge and emit the last throttled value when complete before the throttle time is up. closes #3351 --- spec/operators/throttleTime-spec.ts | 12 ++++++++++++ src/internal/operators/throttleTime.ts | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/spec/operators/throttleTime-spec.ts b/spec/operators/throttleTime-spec.ts index 824b49d69d..ee3033353d 100644 --- a/spec/operators/throttleTime-spec.ts +++ b/spec/operators/throttleTime-spec.ts @@ -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); + }); }); }); diff --git a/src/internal/operators/throttleTime.ts b/src/internal/operators/throttleTime.ts index f64189c474..f842ab0324 100644 --- a/src/internal/operators/throttleTime.ts +++ b/src/internal/operators/throttleTime.ts @@ -97,6 +97,15 @@ class ThrottleTimeSubscriber extends Subscriber { } } + protected _complete() { + if (this._hasTrailingValue) { + this.destination.next(this._trailingValue); + this.destination.complete(); + } else { + this.destination.complete(); + } + } + clearThrottle() { const throttled = this.throttled; if (throttled) {