| 1 |
import { mergeMap } from './mergeMap'; |
| 2 |
import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types'; |
| 3 |
import { isFunction } from '../util/isFunction'; |
| 4 |
|
| 5 |
/* tslint:disable:max-line-length */ |
| 6 |
export function concatMap<T, O extends ObservableInput<any>>( |
| 7 |
project: (value: T, index: number) => O |
| 8 |
): OperatorFunction<T, ObservedValueOf<O>>; |
| 9 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 10 |
export function concatMap<T, O extends ObservableInput<any>>( |
| 11 |
project: (value: T, index: number) => O, |
| 12 |
resultSelector: undefined |
| 13 |
): OperatorFunction<T, ObservedValueOf<O>>; |
| 14 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 15 |
export function concatMap<T, R, O extends ObservableInput<any>>( |
| 16 |
project: (value: T, index: number) => O, |
| 17 |
resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 18 |
): OperatorFunction<T, R>; |
| 19 |
/* tslint:enable:max-line-length */ |
| 20 |
|
| 21 |
/** |
| 22 |
* Projects each source value to an Observable which is merged in the output |
| 23 |
* Observable, in a serialized fashion waiting for each one to complete before |
| 24 |
* merging the next. |
| 25 |
* |
| 26 |
* <span class="informal">Maps each value to an Observable, then flattens all of |
| 27 |
* these inner Observables using {@link concatAll}.</span> |
| 28 |
* |
| 29 |
*  |
| 30 |
* |
| 31 |
* Returns an Observable that emits items based on applying a function that you |
| 32 |
* supply to each item emitted by the source Observable, where that function |
| 33 |
* returns an (so-called "inner") Observable. Each new inner Observable is |
| 34 |
* concatenated with the previous inner Observable. |
| 35 |
* |
| 36 |
* __Warning:__ if source values arrive endlessly and faster than their |
| 37 |
* corresponding inner Observables can complete, it will result in memory issues |
| 38 |
* as inner Observables amass in an unbounded buffer waiting for their turn to |
| 39 |
* be subscribed to. |
| 40 |
* |
| 41 |
* Note: `concatMap` is equivalent to `mergeMap` with concurrency parameter set |
| 42 |
* to `1`. |
| 43 |
* |
| 44 |
* ## Example |
| 45 |
* |
| 46 |
* For each click event, tick every second from 0 to 3, with no concurrency |
| 47 |
* |
| 48 |
* ```ts |
| 49 |
* import { fromEvent, concatMap, interval, take } from 'rxjs'; |
| 50 |
* |
| 51 |
* const clicks = fromEvent(document, 'click'); |
| 52 |
* const result = clicks.pipe( |
| 53 |
* concatMap(ev => interval(1000).pipe(take(4))) |
| 54 |
* ); |
| 55 |
* result.subscribe(x => console.log(x)); |
| 56 |
* |
| 57 |
* // Results in the following: |
| 58 |
* // (results are not concurrent) |
| 59 |
* // For every click on the "document" it will emit values 0 to 3 spaced |
| 60 |
* // on a 1000ms interval |
| 61 |
* // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3 |
| 62 |
* ``` |
| 63 |
* |
| 64 |
* @see {@link concat} |
| 65 |
* @see {@link concatAll} |
| 66 |
* @see {@link concatMapTo} |
| 67 |
* @see {@link exhaustMap} |
| 68 |
* @see {@link mergeMap} |
| 69 |
* @see {@link switchMap} |
| 70 |
* |
| 71 |
* @param project A function that, when applied to an item emitted by the source |
| 72 |
* Observable, returns an Observable. |
| 73 |
* @return A function that returns an Observable that emits the result of |
| 74 |
* applying the projection function (and the optional deprecated |
| 75 |
* `resultSelector`) to each item emitted by the source Observable and taking |
| 76 |
* values from each projected inner Observable sequentially. |
| 77 |
*/ |
| 78 |
export function concatMap<T, R, O extends ObservableInput<any>>( |
| 79 |
project: (value: T, index: number) => O, |
| 80 |
resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 81 |
): OperatorFunction<T, ObservedValueOf<O> | R> { |
| 82 |
return isFunction(resultSelector) ? mergeMap(project, resultSelector, 1) : mergeMap(project, 1); |
| 83 |
} |