Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions spec/operators/throttle-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from 'chai';
import * as Rx from '../../src/Rx';
import marbleTestingSignature = require('../helpers/marble-testing'); // tslint:disable-line:no-require-imports

declare const type;
declare const { asDiagram };
declare const hot: typeof marbleTestingSignature.hot;
declare const cold: typeof marbleTestingSignature.cold;
Expand Down Expand Up @@ -338,6 +339,22 @@ describe('Observable.prototype.throttle', () => {
);
});

type('should support selectors of the same type', () => {
/* tslint:disable:no-unused-variable */
let o: Rx.Observable<number>;
let s: Rx.Observable<number>;
let r: Rx.Observable<number> = o.throttle((n) => s);
/* tslint:enable:no-unused-variable */
});

type('should support selectors of a different type', () => {
/* tslint:disable:no-unused-variable */
let o: Rx.Observable<number>;
let s: Rx.Observable<string>;
let r: Rx.Observable<number> = o.throttle((n) => s);
/* tslint:enable:no-unused-variable */
});

describe('throttle(fn, { leading: true, trailing: true })', () => {
asDiagram('throttle(fn, { leading: true, trailing: true })')('should immediately emit the first value in each time window', () => {
const e1 = hot('-a-xy-----b--x--cxxx--|');
Expand Down
6 changes: 3 additions & 3 deletions src/internal/operators/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ export const defaultThrottleConfig: ThrottleConfig = {
* @method throttle
* @owner Observable
*/
export function throttle<T>(durationSelector: (value: T) => SubscribableOrPromise<number>,
export function throttle<T>(durationSelector: (value: T) => SubscribableOrPromise<any>,
config: ThrottleConfig = defaultThrottleConfig): MonoTypeOperatorFunction<T> {
return (source: Observable<T>) => source.lift(new ThrottleOperator(durationSelector, config.leading, config.trailing));
}

class ThrottleOperator<T> implements Operator<T, T> {
constructor(private durationSelector: (value: T) => SubscribableOrPromise<number>,
constructor(private durationSelector: (value: T) => SubscribableOrPromise<any>,
private leading: boolean,
private trailing: boolean) {
}
Expand All @@ -88,7 +88,7 @@ class ThrottleSubscriber<T, R> extends OuterSubscriber<T, R> {
private _hasTrailingValue = false;

constructor(protected destination: Subscriber<T>,
private durationSelector: (value: T) => SubscribableOrPromise<number>,
private durationSelector: (value: T) => SubscribableOrPromise<any>,
private _leading: boolean,
private _trailing: boolean) {
super(destination);
Expand Down
2 changes: 1 addition & 1 deletion src/internal/patching/operator/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { throttle as higherOrder, ThrottleConfig, defaultThrottleConfig } from '
* @owner Observable
*/
export function throttle<T>(this: Observable<T>,
durationSelector: (value: T) => SubscribableOrPromise<number>,
durationSelector: (value: T) => SubscribableOrPromise<any>,
config: ThrottleConfig = defaultThrottleConfig): Observable<T> {
return higherOrder(durationSelector, config)(this);
}