1 import { concatMap } from './concatMap';
2 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
4 /* tslint:disable:max-line-length */
5 export function concatMapTo<T, O extends ObservableInput<any>>(observable: O): OperatorFunction<T, ObservedValueOf<O>>;
7 export function concatMapTo<T, O extends ObservableInput<any>>(observable: O, resultSelector: undefined): OperatorFunction<T, ObservedValueOf<O>>;
9 export function concatMapTo<T, R, O extends ObservableInput<any>>(observable: O, resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
10 /* tslint:enable:max-line-length */
13 * Projects each source value to the same Observable which is merged multiple
14 * times in a serialized fashion on the output Observable.
16 * <span class="informal">It's like {@link concatMap}, but maps each value
17 * always to the same inner Observable.</span>
19 * ![](concatMapTo.png)
21 * Maps each source value to the given Observable `innerObservable` regardless
22 * of the source value, and then flattens those resulting Observables into one
23 * single Observable, which is the output Observable. Each new `innerObservable`
24 * instance emitted on the output Observable is concatenated with the previous
25 * `innerObservable` instance.
27 * __Warning:__ if source values arrive endlessly and faster than their
28 * corresponding inner Observables can complete, it will result in memory issues
29 * as inner Observables amass in an unbounded buffer waiting for their turn to
32 * Note: `concatMapTo` is equivalent to `mergeMapTo` with concurrency parameter
36 * For each click event, tick every second from 0 to 3, with no concurrency
38 * import { fromEvent, interval } from 'rxjs';
39 * import { concatMapTo, take } from 'rxjs/operators';
41 * const clicks = fromEvent(document, 'click');
42 * const result = clicks.pipe(
43 * concatMapTo(interval(1000).pipe(take(4))),
45 * result.subscribe(x => console.log(x));
47 * // Results in the following:
48 * // (results are not concurrent)
49 * // For every click on the "document" it will emit values 0 to 3 spaced
50 * // on a 1000ms interval
51 * // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3
55 * @see {@link concatAll}
56 * @see {@link concatMap}
57 * @see {@link mergeMapTo}
58 * @see {@link switchMapTo}
60 * @param {ObservableInput} innerObservable An Observable to replace each value from
61 * the source Observable.
62 * @return {Observable} An observable of values merged together by joining the
63 * passed observable with itself, one after the other, for each value emitted
68 export function concatMapTo<T, R, O extends ObservableInput<any>>(
70 resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R
71 ): OperatorFunction<T, ObservedValueOf<O>|R> {
72 return concatMap(() => innerObservable, resultSelector);