a zip code crypto-currency system good for red ONLY

combineLatest.js 6.2KB

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