1 import { Operator } from '../Operator';
2 import { Observable } from '../Observable';
3 import { Subscriber } from '../Subscriber';
4 import { Subscription } from '../Subscription';
5 import { OuterSubscriber } from '../OuterSubscriber';
6 import { InnerSubscriber } from '../InnerSubscriber';
7 import { subscribeToResult } from '../util/subscribeToResult';
8 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
9 import { map } from './map';
10 import { from } from '../observable/from';
12 /* tslint:disable:max-line-length */
13 export function switchMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): OperatorFunction<T, ObservedValueOf<O>>;
14 /** @deprecated resultSelector is no longer supported, use inner map instead */
15 export function switchMap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
16 /** @deprecated resultSelector is no longer supported, use inner map instead */
17 export function switchMap<T, R, O extends ObservableInput<any>>(project: (value: T, index: number) => O, resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
18 /* tslint:enable:max-line-length */
21 * Projects each source value to an Observable which is merged in the output
22 * Observable, emitting values only from the most recently projected Observable.
24 * <span class="informal">Maps each value to an Observable, then flattens all of
25 * these inner Observables.</span>
29 * Returns an Observable that emits items based on applying a function that you
30 * supply to each item emitted by the source Observable, where that function
31 * returns an (so-called "inner") Observable. Each time it observes one of these
32 * inner Observables, the output Observable begins emitting the items emitted by
33 * that inner Observable. When a new inner Observable is emitted, `switchMap`
34 * stops emitting items from the earlier-emitted inner Observable and begins
35 * emitting items from the new one. It continues to behave like this for
36 * subsequent inner Observables.
39 * Generate new Observable according to source Observable values
41 * import { of } from 'rxjs';
42 * import { switchMap } from 'rxjs/operators';
44 * const switched = of(1, 2, 3).pipe(switchMap((x: number) => of(x, x ** 2, x ** 3)));
45 * switched.subscribe(x => console.log(x));
56 * Rerun an interval Observable on every click event
58 * import { fromEvent, interval } from 'rxjs';
59 * import { switchMap } from 'rxjs/operators';
61 * const clicks = fromEvent(document, 'click');
62 * const result = clicks.pipe(switchMap((ev) => interval(1000)));
63 * result.subscribe(x => console.log(x));
66 * @see {@link concatMap}
67 * @see {@link exhaustMap}
68 * @see {@link mergeMap}
69 * @see {@link switchAll}
70 * @see {@link switchMapTo}
72 * @param {function(value: T, ?index: number): ObservableInput} project A function
73 * that, when applied to an item emitted by the source Observable, returns an
75 * @return {Observable} An Observable that emits the result of applying the
76 * projection function (and the optional deprecated `resultSelector`) to each item
77 * emitted by the source Observable and taking only the values from the most recently
78 * projected inner Observable.
82 export function switchMap<T, R, O extends ObservableInput<any>>(
83 project: (value: T, index: number) => O,
84 resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R,
85 ): OperatorFunction<T, ObservedValueOf<O>|R> {
86 if (typeof resultSelector === 'function') {
87 return (source: Observable<T>) => source.pipe(
88 switchMap((a, i) => from(project(a, i)).pipe(
89 map((b, ii) => resultSelector(a, b, i, ii))
93 return (source: Observable<T>) => source.lift(new SwitchMapOperator(project));
96 class SwitchMapOperator<T, R> implements Operator<T, R> {
97 constructor(private project: (value: T, index: number) => ObservableInput<R>) {
100 call(subscriber: Subscriber<R>, source: any): any {
101 return source.subscribe(new SwitchMapSubscriber(subscriber, this.project));
106 * We need this JSDoc comment for affecting ESDoc.
110 class SwitchMapSubscriber<T, R> extends OuterSubscriber<T, R> {
111 private index: number = 0;
112 private innerSubscription: Subscription;
114 constructor(destination: Subscriber<R>,
115 private project: (value: T, index: number) => ObservableInput<R>) {
119 protected _next(value: T) {
120 let result: ObservableInput<R>;
121 const index = this.index++;
123 result = this.project(value, index);
125 this.destination.error(error);
128 this._innerSub(result, value, index);
131 private _innerSub(result: ObservableInput<R>, value: T, index: number) {
132 const innerSubscription = this.innerSubscription;
133 if (innerSubscription) {
134 innerSubscription.unsubscribe();
136 const innerSubscriber = new InnerSubscriber(this, value, index);
137 const destination = this.destination as Subscription;
138 destination.add(innerSubscriber);
139 this.innerSubscription = subscribeToResult(this, result, undefined, undefined, innerSubscriber);
140 // The returned subscription will usually be the subscriber that was
141 // passed. However, interop subscribers will be wrapped and for
142 // unsubscriptions to chain correctly, the wrapper needs to be added, too.
143 if (this.innerSubscription !== innerSubscriber) {
144 destination.add(this.innerSubscription);
148 protected _complete(): void {
149 const {innerSubscription} = this;
150 if (!innerSubscription || innerSubscription.closed) {
156 protected _unsubscribe() {
157 this.innerSubscription = null;
160 notifyComplete(innerSub: Subscription): void {
161 const destination = this.destination as Subscription;
162 destination.remove(innerSub);
163 this.innerSubscription = null;
164 if (this.isStopped) {
169 notifyNext(outerValue: T, innerValue: R,
170 outerIndex: number, innerIndex: number,
171 innerSub: InnerSubscriber<T, R>): void {
172 this.destination.next(innerValue);