import { Observable, ObservableInput } from '../Observable'; import { Scheduler } from '../Scheduler'; import { isScheduler } from '../util/isScheduler'; import { isArray } from '../util/isArray'; import { ArrayObservable } from './ArrayObservable'; import { CombineLatestOperator } from '../operator/combineLatest'; /* tslint:disable:max-line-length */ export function combineLatest(v1: ObservableInput, v2: ObservableInput, scheduler?: Scheduler): Observable<[T, T2]>; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, scheduler?: Scheduler): Observable<[T, T2, T3]>; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, scheduler?: Scheduler): Observable<[T, T2, T3, T4]>; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, scheduler?: Scheduler): Observable<[T, T2, T3, T4, T5]>; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput, scheduler?: Scheduler): Observable<[T, T2, T3, T4, T5, T6]>; export function combineLatest(v1: ObservableInput, project: (v1: T) => R, scheduler?: Scheduler): Observable; export function combineLatest(v1: ObservableInput, v2: ObservableInput, project: (v1: T, v2: T2) => R, scheduler?: Scheduler): Observable; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, project: (v1: T, v2: T2, v3: T3) => R, scheduler?: Scheduler): Observable; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4) => R, scheduler?: Scheduler): Observable; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5) => R, scheduler?: Scheduler): Observable; export function combineLatest(v1: ObservableInput, v2: ObservableInput, v3: ObservableInput, v4: ObservableInput, v5: ObservableInput, v6: ObservableInput, project: (v1: T, v2: T2, v3: T3, v4: T4, v5: T5, v6: T6) => R, scheduler?: Scheduler): Observable; export function combineLatest(array: ObservableInput[], scheduler?: Scheduler): Observable; export function combineLatest(array: ObservableInput[], scheduler?: Scheduler): Observable; export function combineLatest(array: ObservableInput[], project: (...values: Array) => R, scheduler?: Scheduler): Observable; export function combineLatest(array: ObservableInput[], project: (...values: Array) => R, scheduler?: Scheduler): Observable; export function combineLatest(...observables: Array | Scheduler>): Observable; export function combineLatest(...observables: Array | ((...values: Array) => R) | Scheduler>): Observable; export function combineLatest(...observables: Array | ((...values: Array) => R) | Scheduler>): Observable; /* tslint:enable:max-line-length */ /** * Combines multiple Observables to create an Observable whose values are * calculated from the latest values of each of its input Observables. * * Whenever any input Observable emits a value, it * computes a formula using the latest values from all the inputs, then emits * the output of that formula. * * * * `combineLatest` combines the values from all the Observables passed as * arguments. This is done by subscribing to each Observable, in order, and * collecting an array of each of the most recent values any time any of the * input Observables emits, then either taking that array and passing it as * arguments to an optional `project` function and emitting the return value of * that, or just emitting the array of recent values directly if there is no * `project` function. * * @example Dynamically calculate the Body-Mass Index from an Observable of weight and one for height * var weight = Rx.Observable.of(70, 72, 76, 79, 75); * var height = Rx.Observable.of(1.76, 1.77, 1.78); * var bmi = Rx.Observable.combineLatest(weight, height, (w, h) => w / (h * h)); * bmi.subscribe(x => console.log('BMI is ' + x)); * * // With output to console: * // BMI is 24.212293388429753 * // BMI is 23.93948099205209 * // BMI is 23.671253629592222 * * @see {@link combineAll} * @see {@link merge} * @see {@link withLatestFrom} * * @param {Observable} observable1 An input Observable to combine with the * source Observable. * @param {Observable} observable2 An input Observable to combine with the * source Observable. More than one input Observables may be given as argument. * @param {function} [project] An optional function to project the values from * the combined latest values into a new value on the output Observable. * @param {Scheduler} [scheduler=null] The Scheduler to use for subscribing to * each input Observable. * @return {Observable} An Observable of projected values from the most recent * values from each input Observable, or an array of the most recent values from * each input Observable. * @static true * @name combineLatest * @owner Observable */ export function combineLatest(...observables: Array | Array> | (((...values: Array) => R)) | Scheduler>): Observable { let project: (...values: Array) => R = null; let scheduler: Scheduler = null; if (isScheduler(observables[observables.length - 1])) { scheduler = observables.pop(); } if (typeof observables[observables.length - 1] === 'function') { project = <(...values: Array) => R>observables.pop(); } // if the first and only other argument besides the resultSelector is an array // assume it's been called with `combineLatest([obs1, obs2, obs3], project)` if (observables.length === 1 && isArray(observables[0])) { observables = >>observables[0]; } return new ArrayObservable(observables, scheduler).lift(new CombineLatestOperator(project)); }