diff --git a/spec/operators/throttleTime-spec.ts b/spec/operators/throttleTime-spec.ts index 4e65054ae8..899635f15f 100644 --- a/spec/operators/throttleTime-spec.ts +++ b/spec/operators/throttleTime-spec.ts @@ -141,11 +141,11 @@ describe('throttleTime operator', () => { }); describe('throttleTime(fn, { leading: true, trailing: true })', () => { - asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should immediately emit the first and last values in each time window', () => { - const e1 = hot('-a-xy-----b--x--cxxx--|'); - const e1subs = '^ !'; + asDiagram('throttleTime(fn, { leading: true, trailing: true })')('should emit only one value in given time window', () => { + const e1 = hot('-a-xy-----b--x--cxxx---|'); + const e1subs = '^ !'; const t = time( '----| '); - const expected = '-a---y----b---x-c---x-|'; + const expected = '-a---y----b---x---x---x|'; const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: true, trailing: true })); @@ -153,6 +153,15 @@ describe('throttleTime operator', () => { expectSubscriptions(e1.subscriptions).toBe(e1subs); }); + it('should handle a busy producer emitting a regular repeating sequence', () => { + const e1 = hot('abcdeabcdeabcdeabcdea|'); + const subs = '^ !'; + const expected = 'a----a----a----a----a|'; + + expectObservable(e1.pipe(throttleTime(50, rxTestScheduler, { leading: true, trailing: true }))).toBe(expected); + expectSubscriptions(e1.subscriptions).toBe(subs); + }); + it('should emit the value if only a single one is given', () => { const e1 = hot('-a--------------------|'); const t = time('----| '); @@ -166,10 +175,10 @@ describe('throttleTime operator', () => { describe('throttleTime(fn, { leading: false, trailing: true })', () => { asDiagram('throttleTime(fn, { leading: false, trailing: true })')('should immediately emit the last value in each time window', () => { - const e1 = hot('-a-xy-----b--x--cxxx--|'); - const e1subs = '^ !'; + const e1 = hot('-a-xy-----b--x--cxxx---|'); + const e1subs = '^ !'; const t = time( '----| '); - const expected = '-----y--------x-----x-|'; + const expected = '-----y--------x---x---x|'; const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: false, trailing: true })); @@ -178,10 +187,10 @@ describe('throttleTime operator', () => { }); it('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 e1 = hot('-a-xy-----b--x-cxx|'); + const e1subs = '^ !'; + const t = time('----| '); + const expected = '-----y--------x---(x|)'; const result = e1.pipe(throttleTime(t, rxTestScheduler, { leading: false, trailing: true })); diff --git a/src/internal/operators/throttleTime.ts b/src/internal/operators/throttleTime.ts index baed7785c2..21c9c9e675 100644 --- a/src/internal/operators/throttleTime.ts +++ b/src/internal/operators/throttleTime.ts @@ -153,13 +153,16 @@ class ThrottleTimeSubscriber extends Subscriber { const throttled = this.throttled; if (throttled) { if (this.trailing && this._hasTrailingValue) { + this.add(this.throttled = this.scheduler.schedule>(dispatchNext, this.duration, { subscriber: this })); this.destination.next(this._trailingValue); this._trailingValue = null; this._hasTrailingValue = false; } throttled.unsubscribe(); this.remove(throttled); - this.throttled = null; + if (throttled === this.throttled) { + this.throttled = null; + } } } }