forkJoin.js
2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/** 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