combineLatest.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var rxjs_1 = require("rxjs");
  4. var internal_compatibility_1 = require("rxjs/internal-compatibility");
  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 this Observable with values from
  17. * Observables passed as arguments. This is done by subscribing to each
  18. * Observable, in order, and collecting an array of each of the most recent
  19. * values any time any of the input Observables emits, then either taking that
  20. * array and passing it as arguments to an optional `project` function and
  21. * emitting the return value of that, or just emitting the array of recent
  22. * values directly if there is no `project` function.
  23. *
  24. * @example <caption>Dynamically calculate the Body-Mass Index from an Observable of weight and one for height</caption>
  25. * var weight = Rx.Observable.of(70, 72, 76, 79, 75);
  26. * var height = Rx.Observable.of(1.76, 1.77, 1.78);
  27. * var bmi = weight.combineLatest(height, (w, h) => w / (h * h));
  28. * bmi.subscribe(x => console.log('BMI is ' + x));
  29. *
  30. * // With output to console:
  31. * // BMI is 24.212293388429753
  32. * // BMI is 23.93948099205209
  33. * // BMI is 23.671253629592222
  34. *
  35. * @see {@link combineAll}
  36. * @see {@link merge}
  37. * @see {@link withLatestFrom}
  38. *
  39. * @param {ObservableInput} other An input Observable to combine with the source
  40. * Observable. More than one input Observables may be given as argument.
  41. * @param {function} [project] An optional function to project the values from
  42. * the combined latest values into a new value on the output Observable.
  43. * @return {Observable} An Observable of projected values from the most recent
  44. * values from each input Observable, or an array of the most recent values from
  45. * each input Observable.
  46. * @method combineLatest
  47. * @owner Observable
  48. */
  49. function combineLatest() {
  50. var observables = [];
  51. for (var _i = 0; _i < arguments.length; _i++) {
  52. observables[_i] = arguments[_i];
  53. }
  54. var project = null;
  55. if (typeof observables[observables.length - 1] === 'function') {
  56. project = observables.pop();
  57. }
  58. // if the first and only other argument besides the resultSelector is an array
  59. // assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
  60. if (observables.length === 1 && internal_compatibility_1.isArray(observables[0])) {
  61. observables = observables[0].slice();
  62. }
  63. return this.lift.call(rxjs_1.of.apply(void 0, [this].concat(observables)), new internal_compatibility_1.CombineLatestOperator(project));
  64. }
  65. exports.combineLatest = combineLatest;
  66. //# sourceMappingURL=combineLatest.js.map