| 1 |
import { concatMap } from './concatMap'; |
| 2 |
import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types'; |
| 3 |
import { isFunction } from '../util/isFunction'; |
| 4 |
|
| 5 |
/** @deprecated Will be removed in v9. Use {@link concatMap} instead: `concatMap(() => result)` */ |
| 6 |
export function concatMapTo<O extends ObservableInput<unknown>>(observable: O): OperatorFunction<unknown, ObservedValueOf<O>>; |
| 7 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 8 |
export function concatMapTo<O extends ObservableInput<unknown>>( |
| 9 |
observable: O, |
| 10 |
resultSelector: undefined |
| 11 |
): OperatorFunction<unknown, ObservedValueOf<O>>; |
| 12 |
/** @deprecated The `resultSelector` parameter will be removed in v8. Use an inner `map` instead. Details: https://rxjs.dev/deprecations/resultSelector */ |
| 13 |
export function concatMapTo<T, R, O extends ObservableInput<unknown>>( |
| 14 |
observable: O, |
| 15 |
resultSelector: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 16 |
): OperatorFunction<T, R>; |
| 17 |
|
| 18 |
/** |
| 19 |
* Projects each source value to the same Observable which is merged multiple |
| 20 |
* times in a serialized fashion on the output Observable. |
| 21 |
* |
| 22 |
* <span class="informal">It's like {@link concatMap}, but maps each value |
| 23 |
* always to the same inner Observable.</span> |
| 24 |
* |
| 25 |
*  |
| 26 |
* |
| 27 |
* Maps each source value to the given Observable `innerObservable` regardless |
| 28 |
* of the source value, and then flattens those resulting Observables into one |
| 29 |
* single Observable, which is the output Observable. Each new `innerObservable` |
| 30 |
* instance emitted on the output Observable is concatenated with the previous |
| 31 |
* `innerObservable` instance. |
| 32 |
* |
| 33 |
* __Warning:__ if source values arrive endlessly and faster than their |
| 34 |
* corresponding inner Observables can complete, it will result in memory issues |
| 35 |
* as inner Observables amass in an unbounded buffer waiting for their turn to |
| 36 |
* be subscribed to. |
| 37 |
* |
| 38 |
* Note: `concatMapTo` is equivalent to `mergeMapTo` with concurrency parameter |
| 39 |
* set to `1`. |
| 40 |
* |
| 41 |
* ## Example |
| 42 |
* |
| 43 |
* For each click event, tick every second from 0 to 3, with no concurrency |
| 44 |
* |
| 45 |
* ```ts |
| 46 |
* import { fromEvent, concatMapTo, interval, take } from 'rxjs'; |
| 47 |
* |
| 48 |
* const clicks = fromEvent(document, 'click'); |
| 49 |
* const result = clicks.pipe( |
| 50 |
* concatMapTo(interval(1000).pipe(take(4))) |
| 51 |
* ); |
| 52 |
* result.subscribe(x => console.log(x)); |
| 53 |
* |
| 54 |
* // Results in the following: |
| 55 |
* // (results are not concurrent) |
| 56 |
* // For every click on the "document" it will emit values 0 to 3 spaced |
| 57 |
* // on a 1000ms interval |
| 58 |
* // one click = 1000ms-> 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3 |
| 59 |
* ``` |
| 60 |
* |
| 61 |
* @see {@link concat} |
| 62 |
* @see {@link concatAll} |
| 63 |
* @see {@link concatMap} |
| 64 |
* @see {@link mergeMapTo} |
| 65 |
* @see {@link switchMapTo} |
| 66 |
* |
| 67 |
* @param innerObservable An `ObservableInput` to replace each value from the |
| 68 |
* source Observable. |
| 69 |
* @return A function that returns an Observable of values merged together by |
| 70 |
* joining the passed Observable with itself, one after the other, for each |
| 71 |
* value emitted from the source. |
| 72 |
* @deprecated Will be removed in v9. Use {@link concatMap} instead: `concatMap(() => result)` |
| 73 |
*/ |
| 74 |
export function concatMapTo<T, R, O extends ObservableInput<unknown>>( |
| 75 |
innerObservable: O, |
| 76 |
resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 77 |
): OperatorFunction<T, ObservedValueOf<O> | R> { |
| 78 |
return isFunction(resultSelector) ? concatMap(() => innerObservable, resultSelector) : concatMap(() => innerObservable); |
| 79 |
} |