Front end of the Slack clone application.

combineLatest.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var isScheduler_1 = require('../util/isScheduler');
  3. var isArray_1 = require('../util/isArray');
  4. var ArrayObservable_1 = require('./ArrayObservable');
  5. var combineLatest_1 = require('../operators/combineLatest');
  6. /* tslint:enable:max-line-length */
  7. /**
  8. * Combines multiple Observables to create an Observable whose values are
  9. * calculated from the latest values of each of its input Observables.
  10. *
  11. * <span class="informal">Whenever any input Observable emits a value, it
  12. * computes a formula using the latest values from all the inputs, then emits
  13. * the output of that formula.</span>
  14. *
  15. * <img src="./img/combineLatest.png" width="100%">
  16. *
  17. * `combineLatest` combines the values from all the Observables passed as
  18. * arguments. This is done by subscribing to each Observable in order and,
  19. * whenever any Observable emits, collecting an array of the most recent
  20. * values from each Observable. So if you pass `n` Observables to operator,
  21. * returned Observable will always emit an array of `n` values, in order
  22. * corresponding to order of passed Observables (value from the first Observable
  23. * on the first place and so on).
  24. *
  25. * Static version of `combineLatest` accepts either an array of Observables
  26. * or each Observable can be put directly as an argument. Note that array of
  27. * Observables is good choice, if you don't know beforehand how many Observables
  28. * you will combine. Passing empty array will result in Observable that
  29. * completes immediately.
  30. *
  31. * To ensure output array has always the same length, `combineLatest` will
  32. * actually wait for all input Observables to emit at least once,
  33. * before it starts emitting results. This means if some Observable emits
  34. * values before other Observables started emitting, all that values but last
  35. * will be lost. On the other hand, is some Observable does not emit value but
  36. * completes, resulting Observable will complete at the same moment without
  37. * emitting anything, since it will be now impossible to include value from
  38. * completed Observable in resulting array. Also, if some input Observable does
  39. * not emit any value and never completes, `combineLatest` will also never emit
  40. * and never complete, since, again, it will wait for all streams to emit some
  41. * value.
  42. *
  43. * If at least one Observable was passed to `combineLatest` and all passed Observables
  44. * emitted something, resulting Observable will complete when all combined
  45. * streams complete. So even if some Observable completes, result of
  46. * `combineLatest` will still emit values when other Observables do. In case
  47. * of completed Observable, its value from now on will always be the last
  48. * emitted value. On the other hand, if any Observable errors, `combineLatest`
  49. * will error immediately as well, and all other Observables will be unsubscribed.
  50. *
  51. * `combineLatest` accepts as optional parameter `project` function, which takes
  52. * as arguments all values that would normally be emitted by resulting Observable.
  53. * `project` can return any kind of value, which will be then emitted by Observable
  54. * instead of default array. Note that `project` does not take as argument that array
  55. * of values, but values themselves. That means default `project` can be imagined
  56. * as function that takes all its arguments and puts them into an array.
  57. *
  58. *
  59. * @example <caption>Combine two timer Observables</caption>
  60. * const firstTimer = Rx.Observable.timer(0, 1000); // emit 0, 1, 2... after every second, starting from now
  61. * const secondTimer = Rx.Observable.timer(500, 1000); // emit 0, 1, 2... after every second, starting 0,5s from now
  62. * const combinedTimers = Rx.Observable.combineLatest(firstTimer, secondTimer);
  63. * combinedTimers.subscribe(value => console.log(value));
  64. * // Logs
  65. * // [0, 0] after 0.5s
  66. * // [1, 0] after 1s
  67. * // [1, 1] after 1.5s
  68. * // [2, 1] after 2s
  69. *
  70. *
  71. * @example <caption>Combine an array of Observables</caption>
  72. * const observables = [1, 5, 10].map(
  73. * n => Rx.Observable.of(n).delay(n * 1000).startWith(0) // emit 0 and then emit n after n seconds
  74. * );
  75. * const combined = Rx.Observable.combineLatest(observables);
  76. * combined.subscribe(value => console.log(value));
  77. * // Logs
  78. * // [0, 0, 0] immediately
  79. * // [1, 0, 0] after 1s
  80. * // [1, 5, 0] after 5s
  81. * // [1, 5, 10] after 10s
  82. *
  83. *
  84. * @example <caption>Use project function to dynamically calculate the Body-Mass Index</caption>
  85. * var weight = Rx.Observable.of(70, 72, 76, 79, 75);
  86. * var height = Rx.Observable.of(1.76, 1.77, 1.78);
  87. * var bmi = Rx.Observable.combineLatest(weight, height, (w, h) => w / (h * h));
  88. * bmi.subscribe(x => console.log('BMI is ' + x));
  89. *
  90. * // With output to console:
  91. * // BMI is 24.212293388429753
  92. * // BMI is 23.93948099205209
  93. * // BMI is 23.671253629592222
  94. *
  95. *
  96. * @see {@link combineAll}
  97. * @see {@link merge}
  98. * @see {@link withLatestFrom}
  99. *
  100. * @param {ObservableInput} observable1 An input Observable to combine with other Observables.
  101. * @param {ObservableInput} observable2 An input Observable to combine with other Observables.
  102. * More than one input Observables may be given as arguments
  103. * or an array of Observables may be given as the first argument.
  104. * @param {function} [project] An optional function to project the values from
  105. * the combined latest values into a new value on the output Observable.
  106. * @param {Scheduler} [scheduler=null] The IScheduler to use for subscribing to
  107. * each input Observable.
  108. * @return {Observable} An Observable of projected values from the most recent
  109. * values from each input Observable, or an array of the most recent values from
  110. * each input Observable.
  111. * @static true
  112. * @name combineLatest
  113. * @owner Observable
  114. */
  115. function combineLatest() {
  116. var observables = [];
  117. for (var _i = 0; _i < arguments.length; _i++) {
  118. observables[_i - 0] = arguments[_i];
  119. }
  120. var project = null;
  121. var scheduler = null;
  122. if (isScheduler_1.isScheduler(observables[observables.length - 1])) {
  123. scheduler = observables.pop();
  124. }
  125. if (typeof observables[observables.length - 1] === 'function') {
  126. project = observables.pop();
  127. }
  128. // if the first and only other argument besides the resultSelector is an array
  129. // assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
  130. if (observables.length === 1 && isArray_1.isArray(observables[0])) {
  131. observables = observables[0];
  132. }
  133. return new ArrayObservable_1.ArrayObservable(observables, scheduler).lift(new combineLatest_1.CombineLatestOperator(project));
  134. }
  135. exports.combineLatest = combineLatest;
  136. //# sourceMappingURL=combineLatest.js.map