es6/operator/combineLatest.js
import { ArrayObservable } from '../observable/ArrayObservable';
import { isArray } from '../util/isArray';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
var none = {};
/* tslint:disable:max-line-length */
export function combineLatest() {
var observables = [];
for (var _i = 0; _i < arguments.length; _i++) {
observables[_i - 0] = arguments[_i];
}
var project = null;
if (typeof observables[observables.length - 1] === 'function') {
project = observables.pop();
}
// if the first and only other argument besides the resultSelector is an array
// assume it's been called with `combineLatest([obs1, obs2, obs3], project)`
if (observables.length === 1 && isArray(observables[0])) {
observables = observables[0];
}
observables.unshift(this);
return this.lift.call(new ArrayObservable(observables), new CombineLatestOperator(project));
}
export var CombineLatestOperator = (function () {
function CombineLatestOperator(project) {
this.project = project;
}
CombineLatestOperator.prototype.call = function (subscriber, source) {
return source._subscribe(new CombineLatestSubscriber(subscriber, this.project));
};
return CombineLatestOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export var CombineLatestSubscriber = (function (_super) {
__extends(CombineLatestSubscriber, _super);
function CombineLatestSubscriber(destination, project) {
_super.call(this, destination);
this.project = project;
this.active = 0;
this.values = [];
this.observables = [];
}
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(this, observable, observable, i));
}
}
};
CombineLatestSubscriber.prototype.notifyComplete = function (unused) {
if ((this.active -= 1) === 0) {
this.destination.complete();
}
};
CombineLatestSubscriber.prototype.notifyNext = function (outerValue, innerValue, outerIndex, innerIndex, innerSub) {
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.project) {
this._tryProject(values);
}
else {
this.destination.next(values.slice());
}
}
};
CombineLatestSubscriber.prototype._tryProject = function (values) {
var result;
try {
result = this.project.apply(this, values);
}
catch (err) {
this.destination.error(err);
return;
}
this.destination.next(result);
};
return CombineLatestSubscriber;
}(OuterSubscriber));
//# sourceMappingURL=combineLatest.js.map