From 59c723ea2a239d5ad14df26f0bb5be88e4fa37bb Mon Sep 17 00:00:00 2001 From: Asad Saeeduddin Date: Tue, 19 Jan 2016 19:21:35 -0500 Subject: [PATCH] fix(typings): update withLatestFrom to conform to Operator type signature --- src/operator/withLatestFrom.ts | 40 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/operator/withLatestFrom.ts b/src/operator/withLatestFrom.ts index 196dcfd612d..0fbf025c636 100644 --- a/src/operator/withLatestFrom.ts +++ b/src/operator/withLatestFrom.ts @@ -5,6 +5,9 @@ import {tryCatch} from '../util/tryCatch'; import {errorObject} from '../util/errorObject'; import {OuterSubscriber} from '../OuterSubscriber'; import {subscribeToResult} from '../util/subscribeToResult'; +import {isFunction} from '../util/isFunction'; + +export type Projection = (...values: U[]) => R; /** * @param {Observable} observables the observables to get the latest values from. @@ -24,32 +27,39 @@ import {subscribeToResult} from '../util/subscribeToResult'; * result: ---([a,d,x])---------([b,e,y])--------([c,f,z])---| * ``` */ -export function withLatestFrom(...args: Array | ((...values: Array) => R)>): Observable { - let project: any; - if (typeof args[args.length - 1] === 'function') { - project = args.pop(); +export function withLatestFrom(...args: (Observable | Projection)[]): Observable<(T | U)[]> | Observable { + let _this: Observable = this; + + let project: Projection; + let observables: Observable[]; + + let last = args[args.length - 1]; + observables = []>args; + if (isFunction(last)) { + project = last; + args.pop(); } - const observables = []>args; - return this.lift(new WithLatestFromOperator(observables, project)); + // TODO: The way this is implemented is not type safe. Need to fix implementation + return _this.lift(new WithLatestFromOperator(observables, project)); } -class WithLatestFromOperator implements Operator { - constructor(private observables: Observable[], - private project?: (...values: any[]) => Observable) { +class WithLatestFromOperator implements Operator { + constructor(private observables: Observable[], + private project?: Projection) { } - call(subscriber: Subscriber): Subscriber { - return new WithLatestFromSubscriber(subscriber, this.observables, this.project); + call(subscriber: Subscriber<(T | U)[] | R>): Subscriber { + return new WithLatestFromSubscriber(subscriber, this.observables, this.project); } } -class WithLatestFromSubscriber extends OuterSubscriber { +class WithLatestFromSubscriber extends OuterSubscriber { private values: any[]; private toRespond: number[] = []; - constructor(destination: Subscriber, - private observables: Observable[], - private project?: (...values: any[]) => Observable) { + constructor(destination: Subscriber<(T | U)[] | R>, + private observables: Observable[], + private project?: Projection) { super(destination); const len = observables.length; this.values = new Array(len);