combineLatest.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. }
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var isScheduler_1 = require("../util/isScheduler");
  17. var isArray_1 = require("../util/isArray");
  18. var OuterSubscriber_1 = require("../OuterSubscriber");
  19. var subscribeToResult_1 = require("../util/subscribeToResult");
  20. var fromArray_1 = require("./fromArray");
  21. var NONE = {};
  22. function combineLatest() {
  23. var observables = [];
  24. for (var _i = 0; _i < arguments.length; _i++) {
  25. observables[_i] = arguments[_i];
  26. }
  27. var resultSelector = null;
  28. var scheduler = null;
  29. if (isScheduler_1.isScheduler(observables[observables.length - 1])) {
  30. scheduler = observables.pop();
  31. }
  32. if (typeof observables[observables.length - 1] === 'function') {
  33. resultSelector = observables.pop();
  34. }
  35. if (observables.length === 1 && isArray_1.isArray(observables[0])) {
  36. observables = observables[0];
  37. }
  38. return fromArray_1.fromArray(observables, scheduler).lift(new CombineLatestOperator(resultSelector));
  39. }
  40. exports.combineLatest = combineLatest;
  41. var CombineLatestOperator = (function () {
  42. function CombineLatestOperator(resultSelector) {
  43. this.resultSelector = resultSelector;
  44. }
  45. CombineLatestOperator.prototype.call = function (subscriber, source) {
  46. return source.subscribe(new CombineLatestSubscriber(subscriber, this.resultSelector));
  47. };
  48. return CombineLatestOperator;
  49. }());
  50. exports.CombineLatestOperator = CombineLatestOperator;
  51. var CombineLatestSubscriber = (function (_super) {
  52. __extends(CombineLatestSubscriber, _super);
  53. function CombineLatestSubscriber(destination, resultSelector) {
  54. var _this = _super.call(this, destination) || this;
  55. _this.resultSelector = resultSelector;
  56. _this.active = 0;
  57. _this.values = [];
  58. _this.observables = [];
  59. return _this;
  60. }
  61. CombineLatestSubscriber.prototype._next = function (observable) {
  62. this.values.push(NONE);
  63. this.observables.push(observable);
  64. };
  65. CombineLatestSubscriber.prototype._complete = function () {
  66. var observables = this.observables;
  67. var len = observables.length;
  68. if (len === 0) {
  69. this.destination.complete();
  70. }
  71. else {
  72. this.active = len;
  73. this.toRespond = len;
  74. for (var i = 0; i < len; i++) {
  75. var observable = observables[i];
  76. this.add(subscribeToResult_1.subscribeToResult(this, observable, observable, i));
  77. }
  78. }
  79. };
  80. CombineLatestSubscriber.prototype.notifyComplete = function (unused) {
  81. if ((this.active -= 1) === 0) {
  82. this.destination.complete();
  83. }
  84. };
  85. CombineLatestSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
  86. var values = this.values;
  87. var oldVal = values[outerIndex];
  88. var toRespond = !this.toRespond
  89. ? 0
  90. : oldVal === NONE ? --this.toRespond : this.toRespond;
  91. values[outerIndex] = innerValue;
  92. if (toRespond === 0) {
  93. if (this.resultSelector) {
  94. this._tryResultSelector(values);
  95. }
  96. else {
  97. this.destination.next(values.slice());
  98. }
  99. }
  100. };
  101. CombineLatestSubscriber.prototype._tryResultSelector = function (values) {
  102. var result;
  103. try {
  104. result = this.resultSelector.apply(this, values);
  105. }
  106. catch (err) {
  107. this.destination.error(err);
  108. return;
  109. }
  110. this.destination.next(result);
  111. };
  112. return CombineLatestSubscriber;
  113. }(OuterSubscriber_1.OuterSubscriber));
  114. exports.CombineLatestSubscriber = CombineLatestSubscriber;
  115. //# sourceMappingURL=combineLatest.js.map