| 1 |
import { switchMap } from './switchMap'; |
| 2 |
import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types'; |
| 3 |
import { isFunction } from '../util/isFunction'; |
| 4 |
|
| 5 |
/** @deprecated Will be removed in v9. Use {@link switchMap} instead: `switchMap(() => result)` */ |
| 6 |
export function switchMapTo<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 switchMapTo<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 switchMapTo<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 flattened multiple |
| 20 |
* times with {@link switchMap} in the output Observable. |
| 21 |
* |
| 22 |
* <span class="informal">It's like {@link switchMap}, 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. The output Observables |
| 30 |
* emits values only from the most recently emitted instance of |
| 31 |
* `innerObservable`. |
| 32 |
* |
| 33 |
* ## Example |
| 34 |
* |
| 35 |
* Restart an interval Observable on every click event |
| 36 |
* |
| 37 |
* ```ts |
| 38 |
* import { fromEvent, switchMapTo, interval } from 'rxjs'; |
| 39 |
* |
| 40 |
* const clicks = fromEvent(document, 'click'); |
| 41 |
* const result = clicks.pipe(switchMapTo(interval(1000))); |
| 42 |
* result.subscribe(x => console.log(x)); |
| 43 |
* ``` |
| 44 |
* |
| 45 |
* @see {@link concatMapTo} |
| 46 |
* @see {@link switchAll} |
| 47 |
* @see {@link switchMap} |
| 48 |
* @see {@link mergeMapTo} |
| 49 |
* |
| 50 |
* @param innerObservable An `ObservableInput` to replace each value from the |
| 51 |
* source Observable. |
| 52 |
* @return A function that returns an Observable that emits items from the |
| 53 |
* given `innerObservable` (and optionally transformed through the deprecated |
| 54 |
* `resultSelector`) every time a value is emitted on the source Observable, |
| 55 |
* and taking only the values from the most recently projected inner |
| 56 |
* Observable. |
| 57 |
* @deprecated Will be removed in v9. Use {@link switchMap} instead: `switchMap(() => result)` |
| 58 |
*/ |
| 59 |
export function switchMapTo<T, R, O extends ObservableInput<unknown>>( |
| 60 |
innerObservable: O, |
| 61 |
resultSelector?: (outerValue: T, innerValue: ObservedValueOf<O>, outerIndex: number, innerIndex: number) => R |
| 62 |
): OperatorFunction<T, ObservedValueOf<O> | R> { |
| 63 |
return isFunction(resultSelector) ? switchMap(() => innerObservable, resultSelector) : switchMap(() => innerObservable); |
| 64 |
} |