mergeAll.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { mergeMap } from './mergeMap';
  2. import { identity } from '../util/identity';
  3. /**
  4. * Converts a higher-order Observable into a first-order Observable which
  5. * concurrently delivers all values that are emitted on the inner Observables.
  6. *
  7. * <span class="informal">Flattens an Observable-of-Observables.</span>
  8. *
  9. * <img src="./img/mergeAll.png" width="100%">
  10. *
  11. * `mergeAll` subscribes to an Observable that emits Observables, also known as
  12. * a higher-order Observable. Each time it observes one of these emitted inner
  13. * Observables, it subscribes to that and delivers all the values from the
  14. * inner Observable on the output Observable. The output Observable only
  15. * completes once all inner Observables have completed. Any error delivered by
  16. * a inner Observable will be immediately emitted on the output Observable.
  17. *
  18. * @example <caption>Spawn a new interval Observable for each click event, and blend their outputs as one Observable</caption>
  19. * var clicks = Rx.Observable.fromEvent(document, 'click');
  20. * var higherOrder = clicks.map((ev) => Rx.Observable.interval(1000));
  21. * var firstOrder = higherOrder.mergeAll();
  22. * firstOrder.subscribe(x => console.log(x));
  23. *
  24. * @example <caption>Count from 0 to 9 every second for each click, but only allow 2 concurrent timers</caption>
  25. * var clicks = Rx.Observable.fromEvent(document, 'click');
  26. * var higherOrder = clicks.map((ev) => Rx.Observable.interval(1000).take(10));
  27. * var firstOrder = higherOrder.mergeAll(2);
  28. * firstOrder.subscribe(x => console.log(x));
  29. *
  30. * @see {@link combineAll}
  31. * @see {@link concatAll}
  32. * @see {@link exhaust}
  33. * @see {@link merge}
  34. * @see {@link mergeMap}
  35. * @see {@link mergeMapTo}
  36. * @see {@link mergeScan}
  37. * @see {@link switch}
  38. * @see {@link zipAll}
  39. *
  40. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of inner
  41. * Observables being subscribed to concurrently.
  42. * @return {Observable} An Observable that emits values coming from all the
  43. * inner Observables emitted by the source Observable.
  44. * @method mergeAll
  45. * @owner Observable
  46. */
  47. export function mergeAll(concurrent = Number.POSITIVE_INFINITY) {
  48. return mergeMap(identity, null, concurrent);
  49. }
  50. //# sourceMappingURL=mergeAll.js.map