[6a3a178] | 1 | import {OperatorFunction, ObservableInput} from '../types';
|
---|
| 2 | import { switchMap } from './switchMap';
|
---|
| 3 | import { identity } from '../util/identity';
|
---|
| 4 |
|
---|
| 5 | export function switchAll<T>(): OperatorFunction<ObservableInput<T>, T>;
|
---|
| 6 | export function switchAll<R>(): OperatorFunction<any, R>;
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Converts a higher-order Observable into a first-order Observable
|
---|
| 10 | * producing values only from the most recent observable sequence
|
---|
| 11 | *
|
---|
| 12 | * <span class="informal">Flattens an Observable-of-Observables.</span>
|
---|
| 13 | *
|
---|
| 14 | * ![](switchAll.png)
|
---|
| 15 | *
|
---|
| 16 | * `switchAll` subscribes to a source that is an observable of observables, also known as a
|
---|
| 17 | * "higher-order observable" (or `Observable<Observable<T>>`). It subscribes to the most recently
|
---|
| 18 | * provided "inner observable" emitted by the source, unsubscribing from any previously subscribed
|
---|
| 19 | * to inner observable, such that only the most recent inner observable may be subscribed to at
|
---|
| 20 | * any point in time. The resulting observable returned by `switchAll` will only complete if the
|
---|
| 21 | * source observable completes, *and* any currently subscribed to inner observable also has completed,
|
---|
| 22 | * if there are any.
|
---|
| 23 | *
|
---|
| 24 | * ## Examples
|
---|
| 25 | * Spawn a new interval observable for each click event, but for every new
|
---|
| 26 | * click, cancel the previous interval and subscribe to the new one.
|
---|
| 27 | *
|
---|
| 28 | * ```ts
|
---|
| 29 | * import { fromEvent, interval } from 'rxjs';
|
---|
| 30 | * import { switchAll, map, tap } from 'rxjs/operators';
|
---|
| 31 | *
|
---|
| 32 | * const clicks = fromEvent(document, 'click').pipe(tap(() => console.log('click')));
|
---|
| 33 | * const source = clicks.pipe(map((ev) => interval(1000)));
|
---|
| 34 | *
|
---|
| 35 | * source.pipe(
|
---|
| 36 | * switchAll()
|
---|
| 37 | * ).subscribe(x => console.log(x));
|
---|
| 38 | *
|
---|
| 39 | * // Output
|
---|
| 40 | * // click
|
---|
| 41 | * // 1
|
---|
| 42 | * // 2
|
---|
| 43 | * // 3
|
---|
| 44 | * // 4
|
---|
| 45 | * // ...
|
---|
| 46 | * // click
|
---|
| 47 | * // 1
|
---|
| 48 | * // 2
|
---|
| 49 | * // 3
|
---|
| 50 | * // ...
|
---|
| 51 | * // click
|
---|
| 52 | * // ...
|
---|
| 53 | * ```
|
---|
| 54 | *
|
---|
| 55 | * @see {@link combineAll}
|
---|
| 56 | * @see {@link concatAll}
|
---|
| 57 | * @see {@link exhaust}
|
---|
| 58 | * @see {@link switchMap}
|
---|
| 59 | * @see {@link switchMapTo}
|
---|
| 60 | * @see {@link mergeAll}
|
---|
| 61 | */
|
---|
| 62 |
|
---|
| 63 | export function switchAll<T>(): OperatorFunction<ObservableInput<T>, T> {
|
---|
| 64 | return switchMap(identity);
|
---|
| 65 | }
|
---|