[6a3a178] | 1 | import { ObservableInput, OperatorFunction } from '../types';
|
---|
| 2 | import { switchMap } from './switchMap';
|
---|
| 3 |
|
---|
| 4 | /* tslint:disable:max-line-length */
|
---|
| 5 | export function switchMapTo<R>(observable: ObservableInput<R>): OperatorFunction<any, R>;
|
---|
| 6 | /** @deprecated resultSelector is no longer supported. Switch to using switchMap with an inner map */
|
---|
| 7 | export function switchMapTo<T, R>(observable: ObservableInput<R>, resultSelector: undefined): OperatorFunction<T, R>;
|
---|
| 8 | /** @deprecated resultSelector is no longer supported. Switch to using switchMap with an inner map */
|
---|
| 9 | export function switchMapTo<T, I, R>(observable: ObservableInput<I>, resultSelector: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R): OperatorFunction<T, R>;
|
---|
| 10 | /* tslint:enable:max-line-length */
|
---|
| 11 |
|
---|
| 12 | /**
|
---|
| 13 | * Projects each source value to the same Observable which is flattened multiple
|
---|
| 14 | * times with {@link switchMap} in the output Observable.
|
---|
| 15 | *
|
---|
| 16 | * <span class="informal">It's like {@link switchMap}, but maps each value
|
---|
| 17 | * always to the same inner Observable.</span>
|
---|
| 18 | *
|
---|
| 19 | * ![](switchMapTo.png)
|
---|
| 20 | *
|
---|
| 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. The output Observables
|
---|
| 24 | * emits values only from the most recently emitted instance of
|
---|
| 25 | * `innerObservable`.
|
---|
| 26 | *
|
---|
| 27 | * ## Example
|
---|
| 28 | * Rerun an interval Observable on every click event
|
---|
| 29 | * ```ts
|
---|
| 30 | * import { fromEvent, interval } from 'rxjs';
|
---|
| 31 | * import { switchMapTo } from 'rxjs/operators';
|
---|
| 32 | *
|
---|
| 33 | * const clicks = fromEvent(document, 'click');
|
---|
| 34 | * const result = clicks.pipe(switchMapTo(interval(1000)));
|
---|
| 35 | * result.subscribe(x => console.log(x));
|
---|
| 36 | * ```
|
---|
| 37 | *
|
---|
| 38 | * @see {@link concatMapTo}
|
---|
| 39 | * @see {@link switchAll}
|
---|
| 40 | * @see {@link switchMap}
|
---|
| 41 | * @see {@link mergeMapTo}
|
---|
| 42 | *
|
---|
| 43 | * @param {ObservableInput} innerObservable An Observable to replace each value from
|
---|
| 44 | * the source Observable.
|
---|
| 45 | * @return {Observable} An Observable that emits items from the given
|
---|
| 46 | * `innerObservable` (and optionally transformed through the deprecated `resultSelector`)
|
---|
| 47 | * every time a value is emitted on the source Observable, and taking only the values
|
---|
| 48 | * from the most recently projected inner Observable.
|
---|
| 49 | * @method switchMapTo
|
---|
| 50 | * @owner Observable
|
---|
| 51 | */
|
---|
| 52 | export function switchMapTo<T, I, R>(
|
---|
| 53 | innerObservable: ObservableInput<I>,
|
---|
| 54 | resultSelector?: (outerValue: T, innerValue: I, outerIndex: number, innerIndex: number) => R
|
---|
| 55 | ): OperatorFunction<T, I|R> {
|
---|
| 56 | return resultSelector ? switchMap(() => innerObservable, resultSelector) : switchMap(() => innerObservable);
|
---|
| 57 | }
|
---|