3b6ab021f9186570ac39338b0ddf42130fc337de6f39f0ee67c9324f285f79ca9061379d7af93eefcc42afd988757c00d80c29c89fd797e275bd85c90c4e09 2.0 KB

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