es6/operator/combineLatest.js
import { ArrayObservable } from '../observable/ArrayObservable';
import { isArray } from '../util/isArray';
import { OuterSubscriber } from '../OuterSubscriber';
import { subscribeToResult } from '../util/subscribeToResult';
const none = {};
/* tslint:enable:max-line-length */
/**
* 组合多个 Observables 来创建一个 Observable ,该 Observable 的值根据每个输入 Observable 的最新值计算得出的。
*
* <span class="informal">它将使用所有输入中的最新值计算公式,然后发出该公式的输出。</span>
*
* <img src="./img/combineLatest.png" width="100%">
*
* `combineLatest` 结合传入的多个 Observables。 通过顺序的订阅每个输入Observable, 在每次任一输入Observables发送的时候收集
* 每个输入Observables最新的值组成一个数组, 然后要么将这个数组传给可选的投射函数并发送投射函数返回的结果,
* 或者在没有提供投射函数时仅仅发出该数组。
*
* @example <caption>根据一个身高的 Observable 和一个体重的 Observable 动态的计算BMI指数</caption>
* var weight = Rx.Observable.of(70, 72, 76, 79, 75);
* var height = Rx.Observable.of(1.76, 1.77, 1.78);
* var bmi = weight.combineLatest(height, (w, h) => w / (h * h));
* bmi.subscribe(x => console.log('BMI is ' + x));
*
* // With output to console:
* // BMI is 24.212293388429753
* // BMI is 23.93948099205209
* // BMI is 23.671253629592222
*
* @see {@link combineAll}
* @see {@link merge}
* @see {@link withLatestFrom}
*
* @param {ObservableInput} other 将要和源 Observable 结合的输入 Observable。 可以传入多个输入 Observables。
* @param {function} [project] 可选的投射函数,将输出 Observable 返回的值投射为要发出的新的值。
* @return {Observable} 该 Observable 为每个输入 Observable 的最新值的投射结果或数组。
* @method combineLatest
* @owner Observable
*/
export function combineLatest(...observables) {
let 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].slice();
}
observables.unshift(this);
return this.lift.call(new ArrayObservable(observables), new CombineLatestOperator(project));
}
export class CombineLatestOperator {
constructor(project) {
this.project = project;
}
call(subscriber, source) {
return source.subscribe(new CombineLatestSubscriber(subscriber, this.project));
}
}
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
export class CombineLatestSubscriber extends OuterSubscriber {
constructor(destination, project) {
super(destination);
this.project = project;
this.active = 0;
this.values = [];
this.observables = [];
}
_next(observable) {
this.values.push(none);
this.observables.push(observable);
}
_complete() {
const observables = this.observables;
const len = observables.length;
if (len === 0) {
this.destination.complete();
}
else {
this.active = len;
this.toRespond = len;
for (let i = 0; i < len; i++) {
const observable = observables[i];
this.add(subscribeToResult(this, observable, observable, i));
}
}
}
notifyComplete(unused) {
if ((this.active -= 1) === 0) {
this.destination.complete();
}
}
notifyNext(outerValue, innerValue, outerIndex, innerIndex, innerSub) {
const values = this.values;
const oldVal = values[outerIndex];
const 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());
}
}
}
_tryProject(values) {
let result;
try {
result = this.project.apply(this, values);
}
catch (err) {
this.destination.error(err);
return;
}
this.destination.next(result);
}
}
//# sourceMappingURL=combineLatest.js.map