es6/observable/ForkJoinObservable.js
import { Observable } from '../Observable';
import { EmptyObservable } from './EmptyObservable';
import { isArray } from '../util/isArray';
import { subscribeToResult } from '../util/subscribeToResult';
import { OuterSubscriber } from '../OuterSubscriber';
/**
* We need this JSDoc comment for affecting ESDoc.
* @extends {Ignored}
* @hide true
*/
export class ForkJoinObservable extends Observable {
constructor(sources, resultSelector) {
super();
this.sources = sources;
this.resultSelector = resultSelector;
}
/* tslint:enable:max-line-length */
/**
* @param sources
* @return {any}
* @static true
* @name forkJoin
* @owner Observable
*/
static create(...sources) {
if (sources === null || arguments.length === 0) {
return new EmptyObservable();
}
let resultSelector = null;
if (typeof sources[sources.length - 1] === 'function') {
resultSelector = sources.pop();
}
// if the first and only other argument besides the resultSelector is an array
// assume it's been called with `forkJoin([obs1, obs2, obs3], resultSelector)`
if (sources.length === 1 && isArray(sources[0])) {
sources = sources[0];
}
if (sources.length === 0) {
return new EmptyObservable();
}
return new ForkJoinObservable(sources, resultSelector);
}
_subscribe(subscriber) {
return new ForkJoinSubscriber(subscriber, this.sources, this.resultSelector);
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
class ForkJoinSubscriber extends OuterSubscriber {
constructor(destination, sources, resultSelector) {
super(destination);
this.sources = sources;
this.resultSelector = resultSelector;
this.completed = 0;
this.haveValues = 0;
const len = sources.length;
this.total = len;
this.values = new Array(len);
for (let i = 0; i < len; i++) {
const source = sources[i];
const innerSubscription = subscribeToResult(this, source, null, i);
if (innerSubscription) {
innerSubscription.outerIndex = i;
this.add(innerSubscription);
}
}
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
this.values[outerIndex] = innerValue;
if (!innerSub._hasValue) {
innerSub._hasValue = true;
this.haveValues++;
}
}
notifyComplete(innerSub) {
const destination = this.destination;
const { haveValues, resultSelector, values } = this;
const len = values.length;
if (!innerSub._hasValue) {
destination.complete();
return;
}
this.completed++;
if (this.completed !== len) {
return;
}
if (haveValues === len) {
const value = resultSelector ? resultSelector.apply(this, values) : values;
destination.next(value);
}
destination.complete();
}
}
//# sourceMappingURL=ForkJoinObservable.js.map