combineLatest.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** PURE_IMPORTS_START tslib,_util_isScheduler,_util_isArray,_OuterSubscriber,_util_subscribeToResult,_fromArray PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { isScheduler } from '../util/isScheduler';
  4. import { isArray } from '../util/isArray';
  5. import { OuterSubscriber } from '../OuterSubscriber';
  6. import { subscribeToResult } from '../util/subscribeToResult';
  7. import { fromArray } from './fromArray';
  8. var NONE = {};
  9. export function combineLatest() {
  10. var observables = [];
  11. for (var _i = 0; _i < arguments.length; _i++) {
  12. observables[_i] = arguments[_i];
  13. }
  14. var resultSelector = undefined;
  15. var scheduler = undefined;
  16. if (isScheduler(observables[observables.length - 1])) {
  17. scheduler = observables.pop();
  18. }
  19. if (typeof observables[observables.length - 1] === 'function') {
  20. resultSelector = observables.pop();
  21. }
  22. if (observables.length === 1 && isArray(observables[0])) {
  23. observables = observables[0];
  24. }
  25. return fromArray(observables, scheduler).lift(new CombineLatestOperator(resultSelector));
  26. }
  27. var CombineLatestOperator = /*@__PURE__*/ (function () {
  28. function CombineLatestOperator(resultSelector) {
  29. this.resultSelector = resultSelector;
  30. }
  31. CombineLatestOperator.prototype.call = function (subscriber, source) {
  32. return source.subscribe(new CombineLatestSubscriber(subscriber, this.resultSelector));
  33. };
  34. return CombineLatestOperator;
  35. }());
  36. export { CombineLatestOperator };
  37. var CombineLatestSubscriber = /*@__PURE__*/ (function (_super) {
  38. tslib_1.__extends(CombineLatestSubscriber, _super);
  39. function CombineLatestSubscriber(destination, resultSelector) {
  40. var _this = _super.call(this, destination) || this;
  41. _this.resultSelector = resultSelector;
  42. _this.active = 0;
  43. _this.values = [];
  44. _this.observables = [];
  45. return _this;
  46. }
  47. CombineLatestSubscriber.prototype._next = function (observable) {
  48. this.values.push(NONE);
  49. this.observables.push(observable);
  50. };
  51. CombineLatestSubscriber.prototype._complete = function () {
  52. var observables = this.observables;
  53. var len = observables.length;
  54. if (len === 0) {
  55. this.destination.complete();
  56. }
  57. else {
  58. this.active = len;
  59. this.toRespond = len;
  60. for (var i = 0; i < len; i++) {
  61. var observable = observables[i];
  62. this.add(subscribeToResult(this, observable, undefined, i));
  63. }
  64. }
  65. };
  66. CombineLatestSubscriber.prototype.notifyComplete = function (unused) {
  67. if ((this.active -= 1) === 0) {
  68. this.destination.complete();
  69. }
  70. };
  71. CombineLatestSubscriber.prototype.notifyNext = function (_outerValue, innerValue, outerIndex) {
  72. var values = this.values;
  73. var oldVal = values[outerIndex];
  74. var toRespond = !this.toRespond
  75. ? 0
  76. : oldVal === NONE ? --this.toRespond : this.toRespond;
  77. values[outerIndex] = innerValue;
  78. if (toRespond === 0) {
  79. if (this.resultSelector) {
  80. this._tryResultSelector(values);
  81. }
  82. else {
  83. this.destination.next(values.slice());
  84. }
  85. }
  86. };
  87. CombineLatestSubscriber.prototype._tryResultSelector = function (values) {
  88. var result;
  89. try {
  90. result = this.resultSelector.apply(this, values);
  91. }
  92. catch (err) {
  93. this.destination.error(err);
  94. return;
  95. }
  96. this.destination.next(result);
  97. };
  98. return CombineLatestSubscriber;
  99. }(OuterSubscriber));
  100. export { CombineLatestSubscriber };
  101. //# sourceMappingURL=combineLatest.js.map