UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

169 lines 7.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Observable_1 = require("../Observable"); var isArray_1 = require("../util/isArray"); var map_1 = require("../operators/map"); var isObject_1 = require("../util/isObject"); var from_1 = require("./from"); /* tslint:enable:max-line-length */ /** * Accepts an `Array` of {@link ObservableInput} or a dictionary `Object` of {@link ObservableInput} and returns * an {@link Observable} that emits either an array of values in the exact same order as the passed array, * or a dictionary of values in the same shape as the passed dictionary. * * <span class="informal">Wait for Observables to complete and then combine last values they emitted.</span> * * ![](forkJoin.png) * * `forkJoin` is an operator that takes any number of input observables which can be passed either as an array * or a dictionary of input observables. If no input observables are provided, resulting stream will complete * immediately. * * `forkJoin` will wait for all passed observables to complete and then it will emit an array or an object with last * values from corresponding observables. * * If you pass an array of `n` observables to the operator, resulting * array will have `n` values, where first value is the last thing emitted by the first observable, * second value is the last thing emitted by the second observable and so on. * * If you pass a dictionary of observables to the operator, resulting * objects will have the same keys as the dictionary passed, with their last values they've emitted * located at the corresponding key. * * That means `forkJoin` will not emit more than once and it will complete after that. If you need to emit combined * values not only at the end of lifecycle of passed observables, but also throughout it, try out {@link combineLatest} * or {@link zip} instead. * * In order for resulting array to have the same length as the number of input observables, whenever any of * that observables completes without emitting any value, `forkJoin` will complete at that moment as well * and it will not emit anything either, even if it already has some last values from other observables. * Conversely, if there is an observable that never completes, `forkJoin` will never complete as well, * unless at any point some other observable completes without emitting value, which brings us back to * the previous case. Overall, in order for `forkJoin` to emit a value, all observables passed as arguments * have to emit something at least once and complete. * * If any input observable errors at some point, `forkJoin` will error as well and all other observables * will be immediately unsubscribed. * * Optionally `forkJoin` accepts project function, that will be called with values which normally * would land in emitted array. Whatever is returned by project function, will appear in output * observable instead. This means that default project can be thought of as a function that takes * all its arguments and puts them into an array. Note that project function will be called only * when output observable is supposed to emit a result. * * ## Examples * * ### Use forkJoin with a dictionary of observable inputs * ```ts * import { forkJoin, of, timer } from 'rxjs'; * * const observable = forkJoin({ * foo: of(1, 2, 3, 4), * bar: Promise.resolve(8), * baz: timer(4000), * }); * observable.subscribe({ * next: value => console.log(value), * complete: () => console.log('This is how it ends!'), * }); * * // Logs: * // { foo: 4, bar: 8, baz: 0 } after 4 seconds * // "This is how it ends!" immediately after * ``` * * ### Use forkJoin with an array of observable inputs * ```ts * import { forkJoin, of } from 'rxjs'; * * const observable = forkJoin([ * of(1, 2, 3, 4), * Promise.resolve(8), * timer(4000), * ]); * observable.subscribe({ * next: value => console.log(value), * complete: () => console.log('This is how it ends!'), * }); * * // Logs: * // [4, 8, 0] after 4 seconds * // "This is how it ends!" immediately after * ``` * * @see {@link combineLatest} * @see {@link zip} * * @param {...ObservableInput} sources Any number of Observables provided either as an array or as an arguments * passed directly to the operator. * @param {function} [project] Function that takes values emitted by input Observables and returns value * that will appear in resulting Observable instead of default array. * @return {Observable} Observable emitting either an array of last values emitted by passed Observables * or value from project function. */ function forkJoin() { var sources = []; for (var _i = 0; _i < arguments.length; _i++) { sources[_i] = arguments[_i]; } if (sources.length === 1) { var first_1 = sources[0]; if (isArray_1.isArray(first_1)) { return forkJoinInternal(first_1, null); } // TODO(benlesh): isObservable check will not be necessary when deprecated path is removed. if (isObject_1.isObject(first_1) && Object.getPrototypeOf(first_1) === Object.prototype) { var keys = Object.keys(first_1); return forkJoinInternal(keys.map(function (key) { return first_1[key]; }), keys); } } // DEPRECATED PATHS BELOW HERE if (typeof sources[sources.length - 1] === 'function') { var resultSelector_1 = sources.pop(); sources = (sources.length === 1 && isArray_1.isArray(sources[0])) ? sources[0] : sources; return forkJoinInternal(sources, null).pipe(map_1.map(function (args) { return resultSelector_1.apply(void 0, args); })); } return forkJoinInternal(sources, null); } exports.forkJoin = forkJoin; function forkJoinInternal(sources, keys) { return new Observable_1.Observable(function (subscriber) { var len = sources.length; if (len === 0) { subscriber.complete(); return; } var values = new Array(len); var completed = 0; var emitted = 0; var _loop_1 = function (i) { var source = from_1.from(sources[i]); var hasValue = false; subscriber.add(source.subscribe({ next: function (value) { if (!hasValue) { hasValue = true; emitted++; } values[i] = value; }, error: function (err) { return subscriber.error(err); }, complete: function () { completed++; if (completed === len || !hasValue) { if (emitted === len) { subscriber.next(keys ? keys.reduce(function (result, key, i) { return (result[key] = values[i], result); }, {}) : values); } subscriber.complete(); } } })); }; for (var i = 0; i < len; i++) { _loop_1(i); } }); } //# sourceMappingURL=forkJoin.js.map