69 lines
2.6 KiB
JavaScript
69 lines
2.6 KiB
JavaScript
/** PURE_IMPORTS_START _Observable,_util_isArray,_operators_map,_util_isObject,_from PURE_IMPORTS_END */
|
|
import { Observable } from '../Observable';
|
|
import { isArray } from '../util/isArray';
|
|
import { map } from '../operators/map';
|
|
import { isObject } from '../util/isObject';
|
|
import { from } from './from';
|
|
export 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(first_1)) {
|
|
return forkJoinInternal(first_1, null);
|
|
}
|
|
if (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);
|
|
}
|
|
}
|
|
if (typeof sources[sources.length - 1] === 'function') {
|
|
var resultSelector_1 = sources.pop();
|
|
sources = (sources.length === 1 && isArray(sources[0])) ? sources[0] : sources;
|
|
return forkJoinInternal(sources, null).pipe(map(function (args) { return resultSelector_1.apply(void 0, args); }));
|
|
}
|
|
return forkJoinInternal(sources, null);
|
|
}
|
|
function forkJoinInternal(sources, keys) {
|
|
return new 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(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
|