merge.js 3.8KB

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