1 import { Observable } from '../Observable';
2 import { isArray } from '../util/isArray';
3 import { map } from '../operators/map';
4 import { isObject } from '../util/isObject';
5 import { from } from './from';
6 export function forkJoin(...sources) {
7 if (sources.length === 1) {
8 const first = sources[0];
10 return forkJoinInternal(first, null);
12 if (isObject(first) && Object.getPrototypeOf(first) === Object.prototype) {
13 const keys = Object.keys(first);
14 return forkJoinInternal(keys.map(key => first[key]), keys);
17 if (typeof sources[sources.length - 1] === 'function') {
18 const resultSelector = sources.pop();
19 sources = (sources.length === 1 && isArray(sources[0])) ? sources[0] : sources;
20 return forkJoinInternal(sources, null).pipe(map((args) => resultSelector(...args)));
22 return forkJoinInternal(sources, null);
24 function forkJoinInternal(sources, keys) {
25 return new Observable(subscriber => {
26 const len = sources.length;
28 subscriber.complete();
31 const values = new Array(len);
34 for (let i = 0; i < len; i++) {
35 const source = from(sources[i]);
37 subscriber.add(source.subscribe({
45 error: err => subscriber.error(err),
48 if (completed === len || !hasValue) {
49 if (emitted === len) {
50 subscriber.next(keys ?
51 keys.reduce((result, key, i) => (result[key] = values[i], result), {}) :
54 subscriber.complete();
61 //# sourceMappingURL=forkJoin.js.map