1 import { Observable } from '../Observable';
2 import { ObservableInput, ObservedValuesFromArray, ObservedValueOf, SubscribableOrPromise } from '../types';
3 import { isArray } from '../util/isArray';
4 import { map } from '../operators/map';
5 import { isObject } from '../util/isObject';
6 import { isObservable } from '../util/isObservable';
7 import { from } from './from';
9 /* tslint:disable:max-line-length */
11 // forkJoin(a$, b$, c$)
12 /** @deprecated Use the version that takes an array of Observables instead */
13 export function forkJoin<T>(v1: SubscribableOrPromise<T>): Observable<[T]>;
14 /** @deprecated Use the version that takes an array of Observables instead */
15 export function forkJoin<T, T2>(v1: ObservableInput<T>, v2: ObservableInput<T2>): Observable<[T, T2]>;
16 /** @deprecated Use the version that takes an array of Observables instead */
17 export function forkJoin<T, T2, T3>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<[T, T2, T3]>;
18 /** @deprecated Use the version that takes an array of Observables instead */
19 export function forkJoin<T, T2, T3, T4>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<[T, T2, T3, T4]>;
20 /** @deprecated Use the version that takes an array of Observables instead */
21 export function forkJoin<T, T2, T3, T4, T5>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<[T, T2, T3, T4, T5]>;
22 /** @deprecated Use the version that takes an array of Observables instead */
23 export function forkJoin<T, T2, T3, T4, T5, T6>(v1: ObservableInput<T>, v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<[T, T2, T3, T4, T5, T6]>;
25 // forkJoin([a$, b$, c$]);
26 // TODO(benlesh): Uncomment for TS 3.0
27 // export function forkJoin(sources: []): Observable<never>;
28 export function forkJoin<A>(sources: [ObservableInput<A>]): Observable<[A]>;
29 export function forkJoin<A, B>(sources: [ObservableInput<A>, ObservableInput<B>]): Observable<[A, B]>;
30 export function forkJoin<A, B, C>(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>]): Observable<[A, B, C]>;
31 export function forkJoin<A, B, C, D>(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>]): Observable<[A, B, C, D]>;
32 export function forkJoin<A, B, C, D, E>(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>, ObservableInput<E>]): Observable<[A, B, C, D, E]>;
33 export function forkJoin<A, B, C, D, E, F>(sources: [ObservableInput<A>, ObservableInput<B>, ObservableInput<C>, ObservableInput<D>, ObservableInput<E>, ObservableInput<F>]): Observable<[A, B, C, D, E, F]>;
34 export function forkJoin<A extends ObservableInput<any>[]>(sources: A): Observable<ObservedValuesFromArray<A>[]>;
37 export function forkJoin(sourcesObject: {}): Observable<never>;
38 export function forkJoin<T, K extends keyof T>(sourcesObject: T): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }>;
40 /** @deprecated resultSelector is deprecated, pipe to map instead */
41 export function forkJoin(...args: Array<ObservableInput<any>|Function>): Observable<any>;
42 /** @deprecated Use the version that takes an array of Observables instead */
43 export function forkJoin<T>(...sources: ObservableInput<T>[]): Observable<T[]>;
44 /* tslint:enable:max-line-length */
47 * Accepts an `Array` of {@link ObservableInput} or a dictionary `Object` of {@link ObservableInput} and returns
48 * an {@link Observable} that emits either an array of values in the exact same order as the passed array,
49 * or a dictionary of values in the same shape as the passed dictionary.
51 * <span class="informal">Wait for Observables to complete and then combine last values they emitted.</span>
55 * `forkJoin` is an operator that takes any number of input observables which can be passed either as an array
56 * or a dictionary of input observables. If no input observables are provided, resulting stream will complete
59 * `forkJoin` will wait for all passed observables to complete and then it will emit an array or an object with last
60 * values from corresponding observables.
62 * If you pass an array of `n` observables to the operator, resulting
63 * array will have `n` values, where first value is the last thing emitted by the first observable,
64 * second value is the last thing emitted by the second observable and so on.
66 * If you pass a dictionary of observables to the operator, resulting
67 * objects will have the same keys as the dictionary passed, with their last values they've emitted
68 * located at the corresponding key.
70 * That means `forkJoin` will not emit more than once and it will complete after that. If you need to emit combined
71 * values not only at the end of lifecycle of passed observables, but also throughout it, try out {@link combineLatest}
72 * or {@link zip} instead.
74 * In order for resulting array to have the same length as the number of input observables, whenever any of
75 * that observables completes without emitting any value, `forkJoin` will complete at that moment as well
76 * and it will not emit anything either, even if it already has some last values from other observables.
77 * Conversely, if there is an observable that never completes, `forkJoin` will never complete as well,
78 * unless at any point some other observable completes without emitting value, which brings us back to
79 * the previous case. Overall, in order for `forkJoin` to emit a value, all observables passed as arguments
80 * have to emit something at least once and complete.
82 * If any input observable errors at some point, `forkJoin` will error as well and all other observables
83 * will be immediately unsubscribed.
85 * Optionally `forkJoin` accepts project function, that will be called with values which normally
86 * would land in emitted array. Whatever is returned by project function, will appear in output
87 * observable instead. This means that default project can be thought of as a function that takes
88 * all its arguments and puts them into an array. Note that project function will be called only
89 * when output observable is supposed to emit a result.
93 * ### Use forkJoin with a dictionary of observable inputs
95 * import { forkJoin, of, timer } from 'rxjs';
97 * const observable = forkJoin({
98 * foo: of(1, 2, 3, 4),
99 * bar: Promise.resolve(8),
102 * observable.subscribe({
103 * next: value => console.log(value),
104 * complete: () => console.log('This is how it ends!'),
108 * // { foo: 4, bar: 8, baz: 0 } after 4 seconds
109 * // "This is how it ends!" immediately after
112 * ### Use forkJoin with an array of observable inputs
114 * import { forkJoin, of } from 'rxjs';
116 * const observable = forkJoin([
118 * Promise.resolve(8),
121 * observable.subscribe({
122 * next: value => console.log(value),
123 * complete: () => console.log('This is how it ends!'),
127 * // [4, 8, 0] after 4 seconds
128 * // "This is how it ends!" immediately after
131 * @see {@link combineLatest}
134 * @param {...ObservableInput} sources Any number of Observables provided either as an array or as an arguments
135 * passed directly to the operator.
136 * @param {function} [project] Function that takes values emitted by input Observables and returns value
137 * that will appear in resulting Observable instead of default array.
138 * @return {Observable} Observable emitting either an array of last values emitted by passed Observables
139 * or value from project function.
141 export function forkJoin(
144 if (sources.length === 1) {
145 const first = sources[0];
146 if (isArray(first)) {
147 return forkJoinInternal(first, null);
149 // TODO(benlesh): isObservable check will not be necessary when deprecated path is removed.
150 if (isObject(first) && Object.getPrototypeOf(first) === Object.prototype) {
151 const keys = Object.keys(first);
152 return forkJoinInternal(keys.map(key => first[key]), keys);
156 // DEPRECATED PATHS BELOW HERE
157 if (typeof sources[sources.length - 1] === 'function') {
158 const resultSelector = sources.pop() as Function;
159 sources = (sources.length === 1 && isArray(sources[0])) ? sources[0] : sources;
160 return forkJoinInternal(sources, null).pipe(
161 map((args: any[]) => resultSelector(...args))
165 return forkJoinInternal(sources, null);
168 function forkJoinInternal(sources: ObservableInput<any>[], keys: string[] | null): Observable<any> {
169 return new Observable(subscriber => {
170 const len = sources.length;
172 subscriber.complete();
175 const values = new Array(len);
178 for (let i = 0; i < len; i++) {
179 const source = from(sources[i]);
180 let hasValue = false;
181 subscriber.add(source.subscribe({
189 error: err => subscriber.error(err),
192 if (completed === len || !hasValue) {
193 if (emitted === len) {
194 subscriber.next(keys ?
195 keys.reduce((result, key, i) => (result[key] = values[i], result), {}) :
198 subscriber.complete();