123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /** PURE_IMPORTS_START .._observable_ArrayObservable,.._util_isArray,.._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
- var __extends = (this && this.__extends) || function (d, b) {
- for (var p in b)
- if (b.hasOwnProperty(p))
- d[p] = b[p];
- function __() { this.constructor = d; }
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
- };
- import { ArrayObservable } from '../observable/ArrayObservable';
- import { isArray } from '../util/isArray';
- import { OuterSubscriber } from '../OuterSubscriber';
- import { subscribeToResult } from '../util/subscribeToResult';
- var none = {};
- /* tslint:enable:max-line-length */
- /**
- * Combines multiple Observables to create an Observable whose values are
- * calculated from the latest values of each of its input Observables.
- *
- * <span class="informal">Whenever any input Observable emits a value, it
- * computes a formula using the latest values from all the inputs, then emits
- * the output of that formula.</span>
- *
- * <img src="./img/combineLatest.png" width="100%">
- *
- * `combineLatest` combines the values from this Observable with values from
- * Observables passed as arguments. This is done by subscribing to each
- * Observable, in order, and collecting an array of each of the most recent
- * values any time any of the input Observables emits, then either taking that
- * array and passing it as arguments to an optional `project` function and
- * emitting the return value of that, or just emitting the array of recent
- * values directly if there is no `project` function.
- *
- * @example <caption>Dynamically calculate the Body-Mass Index from an Observable of weight and one for height</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 An input Observable to combine with the source
- * Observable. More than one input Observables may be given as argument.
- * @param {function} [project] An optional function to project the values from
- * the combined latest values into a new value on the output Observable.
- * @return {Observable} An Observable of projected values from the most recent
- * values from each input Observable, or an array of the most recent values from
- * each input Observable.
- * @method combineLatest
- * @owner Observable
- */
- 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].slice();
- }
- return function (source) { return source.lift.call(new ArrayObservable([source].concat(observables)), new CombineLatestOperator(project)); };
- }
- export var CombineLatestOperator = /*@__PURE__*/ (/*@__PURE__*/ 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 = /*@__PURE__*/ (/*@__PURE__*/ 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
|