Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Commit

Permalink
fix(typings): relax throttle selector type (ReactiveX#3205)
Browse files Browse the repository at this point in the history
* test(throttle): add typings tests

* fix(typings): relax throttle selector type

Closes ReactiveX#3204
  • Loading branch information
cartant authored and benlesh committed Jan 23, 2018
1 parent dc41a5e commit e83fda7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
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);
}

0 comments on commit e83fda7

Please sign in to comment.