concat.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. var isScheduler_1 = require('../util/isScheduler');
  3. var of_1 = require('./of');
  4. var from_1 = require('./from');
  5. var concatAll_1 = require('../operators/concatAll');
  6. /* tslint:enable:max-line-length */
  7. /**
  8. * Creates an output Observable which sequentially emits all values from given
  9. * Observable and then moves on to the next.
  10. *
  11. * <span class="informal">Concatenates multiple Observables together by
  12. * sequentially emitting their values, one Observable after the other.</span>
  13. *
  14. * <img src="./img/concat.png" width="100%">
  15. *
  16. * `concat` joins multiple Observables together, by subscribing to them one at a time and
  17. * merging their results into the output Observable. You can pass either an array of
  18. * Observables, or put them directly as arguments. Passing an empty array will result
  19. * in Observable that completes immediately.
  20. *
  21. * `concat` will subscribe to first input Observable and emit all its values, without
  22. * changing or affecting them in any way. When that Observable completes, it will
  23. * subscribe to then next Observable passed and, again, emit its values. This will be
  24. * repeated, until the operator runs out of Observables. When last input Observable completes,
  25. * `concat` will complete as well. At any given moment only one Observable passed to operator
  26. * emits values. If you would like to emit values from passed Observables concurrently, check out
  27. * {@link merge} instead, especially with optional `concurrent` parameter. As a matter of fact,
  28. * `concat` is an equivalent of `merge` operator with `concurrent` parameter set to `1`.
  29. *
  30. * Note that if some input Observable never completes, `concat` will also never complete
  31. * and Observables following the one that did not complete will never be subscribed. On the other
  32. * hand, if some Observable simply completes immediately after it is subscribed, it will be
  33. * invisible for `concat`, which will just move on to the next Observable.
  34. *
  35. * If any Observable in chain errors, instead of passing control to the next Observable,
  36. * `concat` will error immediately as well. Observables that would be subscribed after
  37. * the one that emitted error, never will.
  38. *
  39. * If you pass to `concat` the same Observable many times, its stream of values
  40. * will be "replayed" on every subscription, which means you can repeat given Observable
  41. * as many times as you like. If passing the same Observable to `concat` 1000 times becomes tedious,
  42. * you can always use {@link repeat}.
  43. *
  44. * @example <caption>Concatenate a timer counting from 0 to 3 with a synchronous sequence from 1 to 10</caption>
  45. * var timer = Rx.Observable.interval(1000).take(4);
  46. * var sequence = Rx.Observable.range(1, 10);
  47. * var result = Rx.Observable.concat(timer, sequence);
  48. * result.subscribe(x => console.log(x));
  49. *
  50. * // results in:
  51. * // 0 -1000ms-> 1 -1000ms-> 2 -1000ms-> 3 -immediate-> 1 ... 10
  52. *
  53. *
  54. * @example <caption>Concatenate an array of 3 Observables</caption>
  55. * var timer1 = Rx.Observable.interval(1000).take(10);
  56. * var timer2 = Rx.Observable.interval(2000).take(6);
  57. * var timer3 = Rx.Observable.interval(500).take(10);
  58. * var result = Rx.Observable.concat([timer1, timer2, timer3]); // note that array is passed
  59. * result.subscribe(x => console.log(x));
  60. *
  61. * // results in the following:
  62. * // (Prints to console sequentially)
  63. * // -1000ms-> 0 -1000ms-> 1 -1000ms-> ... 9
  64. * // -2000ms-> 0 -2000ms-> 1 -2000ms-> ... 5
  65. * // -500ms-> 0 -500ms-> 1 -500ms-> ... 9
  66. *
  67. *
  68. * @example <caption>Concatenate the same Observable to repeat it</caption>
  69. * const timer = Rx.Observable.interval(1000).take(2);
  70. *
  71. * Rx.Observable.concat(timer, timer) // concating the same Observable!
  72. * .subscribe(
  73. * value => console.log(value),
  74. * err => {},
  75. * () => console.log('...and it is done!')
  76. * );
  77. *
  78. * // Logs:
  79. * // 0 after 1s
  80. * // 1 after 2s
  81. * // 0 after 3s
  82. * // 1 after 4s
  83. * // "...and it is done!" also after 4s
  84. *
  85. * @see {@link concatAll}
  86. * @see {@link concatMap}
  87. * @see {@link concatMapTo}
  88. *
  89. * @param {ObservableInput} input1 An input Observable to concatenate with others.
  90. * @param {ObservableInput} input2 An input Observable to concatenate with others.
  91. * More than one input Observables may be given as argument.
  92. * @param {Scheduler} [scheduler=null] An optional IScheduler to schedule each
  93. * Observable subscription on.
  94. * @return {Observable} All values of each passed Observable merged into a
  95. * single Observable, in order, in serial fashion.
  96. * @static true
  97. * @name concat
  98. * @owner Observable
  99. */
  100. function concat() {
  101. var observables = [];
  102. for (var _i = 0; _i < arguments.length; _i++) {
  103. observables[_i - 0] = arguments[_i];
  104. }
  105. if (observables.length === 1 || (observables.length === 2 && isScheduler_1.isScheduler(observables[1]))) {
  106. return from_1.from(observables[0]);
  107. }
  108. return concatAll_1.concatAll()(of_1.of.apply(void 0, observables));
  109. }
  110. exports.concat = concat;
  111. //# sourceMappingURL=concat.js.map