[6a3a178] | 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';
|
---|
| 8 |
|
---|
| 9 | /* tslint:disable:max-line-length */
|
---|
| 10 |
|
---|
| 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]>;
|
---|
| 24 |
|
---|
| 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>[]>;
|
---|
| 35 |
|
---|
| 36 | // forkJoin({})
|
---|
| 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]> }>;
|
---|
| 39 |
|
---|
| 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 */
|
---|
| 45 |
|
---|
| 46 | /**
|
---|
| 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.
|
---|
| 50 | *
|
---|
| 51 | * <span class="informal">Wait for Observables to complete and then combine last values they emitted.</span>
|
---|
| 52 | *
|
---|
| 53 | * ![](forkJoin.png)
|
---|
| 54 | *
|
---|
| 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
|
---|
| 57 | * immediately.
|
---|
| 58 | *
|
---|
| 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.
|
---|
| 61 | *
|
---|
| 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.
|
---|
| 65 | *
|
---|
| 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.
|
---|
| 69 | *
|
---|
| 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.
|
---|
| 73 | *
|
---|
| 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.
|
---|
| 81 | *
|
---|
| 82 | * If any input observable errors at some point, `forkJoin` will error as well and all other observables
|
---|
| 83 | * will be immediately unsubscribed.
|
---|
| 84 | *
|
---|
| 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.
|
---|
| 90 | *
|
---|
| 91 | * ## Examples
|
---|
| 92 | *
|
---|
| 93 | * ### Use forkJoin with a dictionary of observable inputs
|
---|
| 94 | * ```ts
|
---|
| 95 | * import { forkJoin, of, timer } from 'rxjs';
|
---|
| 96 | *
|
---|
| 97 | * const observable = forkJoin({
|
---|
| 98 | * foo: of(1, 2, 3, 4),
|
---|
| 99 | * bar: Promise.resolve(8),
|
---|
| 100 | * baz: timer(4000),
|
---|
| 101 | * });
|
---|
| 102 | * observable.subscribe({
|
---|
| 103 | * next: value => console.log(value),
|
---|
| 104 | * complete: () => console.log('This is how it ends!'),
|
---|
| 105 | * });
|
---|
| 106 | *
|
---|
| 107 | * // Logs:
|
---|
| 108 | * // { foo: 4, bar: 8, baz: 0 } after 4 seconds
|
---|
| 109 | * // "This is how it ends!" immediately after
|
---|
| 110 | * ```
|
---|
| 111 | *
|
---|
| 112 | * ### Use forkJoin with an array of observable inputs
|
---|
| 113 | * ```ts
|
---|
| 114 | * import { forkJoin, of } from 'rxjs';
|
---|
| 115 | *
|
---|
| 116 | * const observable = forkJoin([
|
---|
| 117 | * of(1, 2, 3, 4),
|
---|
| 118 | * Promise.resolve(8),
|
---|
| 119 | * timer(4000),
|
---|
| 120 | * ]);
|
---|
| 121 | * observable.subscribe({
|
---|
| 122 | * next: value => console.log(value),
|
---|
| 123 | * complete: () => console.log('This is how it ends!'),
|
---|
| 124 | * });
|
---|
| 125 | *
|
---|
| 126 | * // Logs:
|
---|
| 127 | * // [4, 8, 0] after 4 seconds
|
---|
| 128 | * // "This is how it ends!" immediately after
|
---|
| 129 | * ```
|
---|
| 130 | *
|
---|
| 131 | * @see {@link combineLatest}
|
---|
| 132 | * @see {@link zip}
|
---|
| 133 | *
|
---|
| 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.
|
---|
| 140 | */
|
---|
| 141 | export function forkJoin(
|
---|
| 142 | ...sources: any[]
|
---|
| 143 | ): Observable<any> {
|
---|
| 144 | if (sources.length === 1) {
|
---|
| 145 | const first = sources[0];
|
---|
| 146 | if (isArray(first)) {
|
---|
| 147 | return forkJoinInternal(first, null);
|
---|
| 148 | }
|
---|
| 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);
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 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))
|
---|
| 162 | );
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return forkJoinInternal(sources, null);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | function forkJoinInternal(sources: ObservableInput<any>[], keys: string[] | null): Observable<any> {
|
---|
| 169 | return new Observable(subscriber => {
|
---|
| 170 | const len = sources.length;
|
---|
| 171 | if (len === 0) {
|
---|
| 172 | subscriber.complete();
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 | const values = new Array(len);
|
---|
| 176 | let completed = 0;
|
---|
| 177 | let emitted = 0;
|
---|
| 178 | for (let i = 0; i < len; i++) {
|
---|
| 179 | const source = from(sources[i]);
|
---|
| 180 | let hasValue = false;
|
---|
| 181 | subscriber.add(source.subscribe({
|
---|
| 182 | next: value => {
|
---|
| 183 | if (!hasValue) {
|
---|
| 184 | hasValue = true;
|
---|
| 185 | emitted++;
|
---|
| 186 | }
|
---|
| 187 | values[i] = value;
|
---|
| 188 | },
|
---|
| 189 | error: err => subscriber.error(err),
|
---|
| 190 | complete: () => {
|
---|
| 191 | completed++;
|
---|
| 192 | if (completed === len || !hasValue) {
|
---|
| 193 | if (emitted === len) {
|
---|
| 194 | subscriber.next(keys ?
|
---|
| 195 | keys.reduce((result, key, i) => (result[key] = values[i], result), {}) :
|
---|
| 196 | values);
|
---|
| 197 | }
|
---|
| 198 | subscriber.complete();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | }));
|
---|
| 202 | }
|
---|
| 203 | });
|
---|
| 204 | }
|
---|