Skip to content

Commit

Permalink
fix(throttle): now properly trailing throttles for individual values
Browse files Browse the repository at this point in the history
fixes #2859
  • Loading branch information
benlesh committed Sep 26, 2017
1 parent 2e576dc commit ccca46c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
11 changes: 11 additions & 0 deletions spec/operators/throttle-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
declare const expectObservable: typeof marbleTestingSignature.expectObservable;
declare const expectSubscriptions: typeof marbleTestingSignature.expectSubscriptions;
declare const time: typeof marbleTestingSignature.time;
declare const rxTestScheduler: typeof marbleTestingSignature.rxTestScheduler;

const Observable = Rx.Observable;

Expand Down Expand Up @@ -372,5 +374,14 @@ describe('Observable.prototype.throttle', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
expectSubscriptions(e2.subscriptions).toBe(e2subs);
});

it('should work for individual values', () => {
const s1 = cold('---------x---------x--------x------|');
const n = cold('----(n|)');
const exp = '-------------x---------x--------x--|';

expectObservable(s1.throttle(() => n, { trailing: true }))
.toBe(exp);
});
});
});
15 changes: 5 additions & 10 deletions src/operators/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,17 @@ class ThrottleSubscriber<T, R> extends OuterSubscriber<T, R> {
}

protected _next(value: T): void {
if (this.throttled) {
if (this._trailing) {
this._hasTrailingValue = true;
this._trailingValue = value;
}
} else {
if (this._trailing) {
this._hasTrailingValue = true;
this._trailingValue = value;
}
if (!this.throttled) {
const duration = this.tryDurationSelector(value);
if (duration) {
this.add(this.throttled = subscribeToResult(this, duration));
}
if (this._leading) {
this.destination.next(value);
if (this._trailing) {
this._hasTrailingValue = true;
this._trailingValue = value;
}
}
}
}
Expand Down

0 comments on commit ccca46c

Please sign in to comment.