concat.js 4.6 KB

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