Debian Codegraph @

node/rxjs/src/internal/operators/switchMap.ts

raw
1 import { Subscriber } from '../Subscriber';
2 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
3 import { innerFrom } from '../observable/innerFrom';
4 import { operate } from '../util/lift';
5 import { createOperatorSubscriber } from './OperatorSubscriber';
6
7 /* tslint:disable:max-line-length */
8 export function switchMap<T, O extends ObservableInput<any>>(
9 project: (value: T, index: number) => O
10 ): OperatorFunction<T, ObservedValueOf<O>>;
11 /** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */
12 export function switchMap<T, O extends ObservableInput<any>>(
13 project: (value: T, index: number) => O,
14 resultSelector: undefined
15 ): OperatorFunction<T, ObservedValueOf<O>>;
16 /** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */
17 export function switchMap<T, R, O extends ObservableInput<any>>(
18 project: (value: T, index: number) => O,
19 resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
20 ): OperatorFunction<T, R>;
21 /* tslint:enable:max-line-length */
22
23 /**
24 * Projects each source value to an Observable which is merged in the output
25 * Observable, emitting values only from the most recently projected Observable.
26 *
27 * <span class="informal">Maps each value to an Observable, then flattens all of
28 * these inner Observables using {@link switchAll}.</span>
29 *
30 * ![](switchMap.png)
31 *
32 * Returns an Observable that emits items based on applying a function that you
33 * supply to each item emitted by the source Observable, where that function
34 * returns an (so-called "inner") Observable. Each time it observes one of these
35 * inner Observables, the output Observable begins emitting the items emitted by
36 * that inner Observable. When a new inner Observable is emitted, `switchMap`
37 * stops emitting items from the earlier-emitted inner Observable and begins
38 * emitting items from the new one. It continues to behave like this for
39 * subsequent inner Observables.
40 *
41 * ## Example
42 *
43 * Generate new Observable according to source Observable values
44 *
45 * ```ts
46 * import { of, switchMap } from 'rxjs';
47 *
48 * const switched = of(1, 2, 3).pipe(switchMap(x => of(x, x ** 2, x ** 3)));
49 * switched.subscribe(x => console.log(x));
50 * // outputs
51 * // 1
52 * // 1
53 * // 1
54 * // 2
55 * // 4
56 * // 8
57 * // 3
58 * // 9
59 * // 27
60 * ```
61 *
62 * Restart an interval Observable on every click event
63 *
64 * ```ts
65 * import { fromEvent, switchMap, interval } from 'rxjs';
66 *
67 * const clicks = fromEvent(document, 'click');
68 * const result = clicks.pipe(switchMap(() => interval(1000)));
69 * result.subscribe(x => console.log(x));
70 * ```
71 *
72 * @see {@link concatMap}
73 * @see {@link exhaustMap}
74 * @see {@link mergeMap}
75 * @see {@link switchAll}
76 * @see {@link switchMapTo}
77 *
78 * @param project A function that, when applied to an item emitted by the source
79 * Observable, returns an Observable.
80 * @return A function that returns an Observable that emits the result of
81 * applying the projection function (and the optional deprecated
82 * `resultSelector`) to each item emitted by the source Observable and taking
83 * only the values from the most recently projected inner Observable.
84 */
85 export function switchMap<T, R, O extends ObservableInput<any>>(
86 project: (value: T, index: number) => O,
87 resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
88 ): OperatorFunction<T, ObservedValueOf<O> | R> {
89 return operate((source, subscriber) => {
90 let innerSubscriber: Subscriber<ObservedValueOf<O>> | null = null;
91 let index = 0;
92 // Whether or not the source subscription has completed
93 let isComplete = false;
94
95 // We only complete the result if the source is complete AND we don't have an active inner subscription.
96 // This is called both when the source completes and when the inners complete.
97 const checkComplete = () => isComplete && !innerSubscriber && subscriber.complete();
98
99 source.subscribe(
100 createOperatorSubscriber(
101 subscriber,
102 (value) => {
103 // Cancel the previous inner subscription if there was one
104 innerSubscriber?.unsubscribe();
105 let innerIndex = 0;
106 const outerIndex = index++;
107 // Start the next inner subscription
108 innerFrom(project(value, outerIndex)).subscribe(
109 (innerSubscriber = createOperatorSubscriber(
110 subscriber,
111 // When we get a new inner value, next it through. Note that this is
112 // handling the deprecate result selector here. This is because with this architecture
113 // it ends up being smaller than using the map operator.
114 (innerValue) => subscriber.next(resultSelector ? resultSelector(value, innerValue, outerIndex, innerIndex++) : innerValue),
115 () => {
116 // The inner has completed. Null out the inner subscriber to
117 // free up memory and to signal that we have no inner subscription
118 // currently.
119 innerSubscriber = null!;
120 checkComplete();
121 }
122 ))
123 );
124 },
125 () => {
126 isComplete = true;
127 checkComplete();
128 }
129 )
130 );
131 });
132 }