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