import { Operator } from '../Operator'; import { Observable, ObservableInput } from '../Observable'; import { Subscriber } from '../Subscriber'; import { Subscription } from '../Subscription'; import { OuterSubscriber } from '../OuterSubscriber'; import { InnerSubscriber } from '../InnerSubscriber'; import { subscribeToResult } from '../util/subscribeToResult'; import { OperatorFunction } from '../interfaces'; /* tslint:disable:max-line-length */ export function switchMap(project: (value: T, index: number) => ObservableInput): OperatorFunction; export function switchMap(project: (value: T, index: number) => ObservableInput, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction; /* tslint:enable:max-line-length */ /** * Projects each source value to an Observable which is merged in the output * Observable, emitting values only from the most recently projected Observable. * * Maps each value to an Observable, then flattens all of * these inner Observables using {@link switch}. * * * * Returns an Observable that emits items based on applying a function that you * supply to each item emitted by the source Observable, where that function * returns an (so-called "inner") Observable. Each time it observes one of these * inner Observables, the output Observable begins emitting the items emitted by * that inner Observable. When a new inner Observable is emitted, `switchMap` * stops emitting items from the earlier-emitted inner Observable and begins * emitting items from the new one. It continues to behave like this for * subsequent inner Observables. * * @example Rerun an interval Observable on every click event * var clicks = Rx.Observable.fromEvent(document, 'click'); * var result = clicks.switchMap((ev) => Rx.Observable.interval(1000)); * result.subscribe(x => console.log(x)); * * @see {@link concatMap} * @see {@link exhaustMap} * @see {@link mergeMap} * @see {@link switch} * @see {@link switchMapTo} * * @param {function(value: T, ?index: number): ObservableInput} project A function * that, when applied to an item emitted by the source Observable, returns an * Observable. * @param {function(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): any} [resultSelector] * A function to produce the value on the output Observable based on the values * and the indices of the source (outer) emission and the inner Observable * emission. The arguments passed to this function are: * - `outerValue`: the value that came from the source * - `innerValue`: the value that came from the projected Observable * - `outerIndex`: the "index" of the value that came from the source * - `innerIndex`: the "index" of the value from the projected Observable * @return {Observable} An Observable that emits the result of applying the * projection function (and the optional `resultSelector`) to each item emitted * by the source Observable and taking only the values from the most recently * projected inner Observable. * @method switchMap * @owner Observable */ export function switchMap( project: (value: T, index: number) => ObservableInput, resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R ): OperatorFunction { return function switchMapOperatorFunction(source: Observable): Observable { return source.lift(new SwitchMapOperator(project, resultSelector)); }; } class SwitchMapOperator implements Operator { constructor(private project: (value: T, index: number) => ObservableInput, private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) { } call(subscriber: Subscriber, source: any): any { return source.subscribe(new SwitchMapSubscriber(subscriber, this.project, this.resultSelector)); } } /** * We need this JSDoc comment for affecting ESDoc. * @ignore * @extends {Ignored} */ class SwitchMapSubscriber extends OuterSubscriber { private index: number = 0; private innerSubscription: Subscription; constructor(destination: Subscriber, private project: (value: T, index: number) => ObservableInput, private resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R) { super(destination); } protected _next(value: T) { let result: ObservableInput; const index = this.index++; try { result = this.project(value, index); } catch (error) { this.destination.error(error); return; } this._innerSub(result, value, index); } private _innerSub(result: ObservableInput, value: T, index: number) { const innerSubscription = this.innerSubscription; if (innerSubscription) { innerSubscription.unsubscribe(); } this.add(this.innerSubscription = subscribeToResult(this, result, value, index)); } protected _complete(): void { const {innerSubscription} = this; if (!innerSubscription || innerSubscription.closed) { super._complete(); } } /** @deprecated internal use only */ _unsubscribe() { this.innerSubscription = null; } notifyComplete(innerSub: Subscription): void { this.remove(innerSub); this.innerSubscription = null; if (this.isStopped) { super._complete(); } } notifyNext(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number, innerSub: InnerSubscriber): void { if (this.resultSelector) { this._tryNotifyNext(outerValue, innerValue, outerIndex, innerIndex); } else { this.destination.next(innerValue); } } private _tryNotifyNext(outerValue: T, innerValue: I, outerIndex: number, innerIndex: number): void { let result: R; try { result = this.resultSelector(outerValue, innerValue, outerIndex, innerIndex); } catch (err) { this.destination.error(err); return; } this.destination.next(result); } }