merge.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { Observable } from '../Observable';
  2. import { ArrayObservable } from './ArrayObservable';
  3. import { isScheduler } from '../util/isScheduler';
  4. import { mergeAll } from '../operators/mergeAll';
  5. /* tslint:enable:max-line-length */
  6. /**
  7. * Creates an output Observable which concurrently emits all values from every
  8. * given input Observable.
  9. *
  10. * <span class="informal">Flattens multiple Observables together by blending
  11. * their values into one Observable.</span>
  12. *
  13. * <img src="./img/merge.png" width="100%">
  14. *
  15. * `merge` subscribes to each given input Observable (as arguments), and simply
  16. * forwards (without doing any transformation) all the values from all the input
  17. * Observables to the output Observable. The output Observable only completes
  18. * once all input Observables have completed. Any error delivered by an input
  19. * Observable will be immediately emitted on the output Observable.
  20. *
  21. * @example <caption>Merge together two Observables: 1s interval and clicks</caption>
  22. * var clicks = Rx.Observable.fromEvent(document, 'click');
  23. * var timer = Rx.Observable.interval(1000);
  24. * var clicksOrTimer = Rx.Observable.merge(clicks, timer);
  25. * clicksOrTimer.subscribe(x => console.log(x));
  26. *
  27. * // Results in the following:
  28. * // timer will emit ascending values, one every second(1000ms) to console
  29. * // clicks logs MouseEvents to console everytime the "document" is clicked
  30. * // Since the two streams are merged you see these happening
  31. * // as they occur.
  32. *
  33. * @example <caption>Merge together 3 Observables, but only 2 run concurrently</caption>
  34. * var timer1 = Rx.Observable.interval(1000).take(10);
  35. * var timer2 = Rx.Observable.interval(2000).take(6);
  36. * var timer3 = Rx.Observable.interval(500).take(10);
  37. * var concurrent = 2; // the argument
  38. * var merged = Rx.Observable.merge(timer1, timer2, timer3, concurrent);
  39. * merged.subscribe(x => console.log(x));
  40. *
  41. * // Results in the following:
  42. * // - First timer1 and timer2 will run concurrently
  43. * // - timer1 will emit a value every 1000ms for 10 iterations
  44. * // - timer2 will emit a value every 2000ms for 6 iterations
  45. * // - after timer1 hits it's max iteration, timer2 will
  46. * // continue, and timer3 will start to run concurrently with timer2
  47. * // - when timer2 hits it's max iteration it terminates, and
  48. * // timer3 will continue to emit a value every 500ms until it is complete
  49. *
  50. * @see {@link mergeAll}
  51. * @see {@link mergeMap}
  52. * @see {@link mergeMapTo}
  53. * @see {@link mergeScan}
  54. *
  55. * @param {...ObservableInput} observables Input Observables to merge together.
  56. * @param {number} [concurrent=Number.POSITIVE_INFINITY] Maximum number of input
  57. * Observables being subscribed to concurrently.
  58. * @param {Scheduler} [scheduler=null] The IScheduler to use for managing
  59. * concurrency of input Observables.
  60. * @return {Observable} an Observable that emits items that are the result of
  61. * every input Observable.
  62. * @static true
  63. * @name merge
  64. * @owner Observable
  65. */
  66. export function merge(...observables) {
  67. let concurrent = Number.POSITIVE_INFINITY;
  68. let scheduler = null;
  69. let last = observables[observables.length - 1];
  70. if (isScheduler(last)) {
  71. scheduler = observables.pop();
  72. if (observables.length > 1 && typeof observables[observables.length - 1] === 'number') {
  73. concurrent = observables.pop();
  74. }
  75. }
  76. else if (typeof last === 'number') {
  77. concurrent = observables.pop();
  78. }
  79. if (scheduler === null && observables.length === 1 && observables[0] instanceof Observable) {
  80. return observables[0];
  81. }
  82. return mergeAll(concurrent)(new ArrayObservable(observables, scheduler));
  83. }
  84. //# sourceMappingURL=merge.js.map