[6a3a178] | 1 | import { Observable } from '../Observable';
|
---|
| 2 | import { ObservableInput } from '../types';
|
---|
| 3 | import { from } from './from';
|
---|
| 4 | import { isArray } from '../util/isArray';
|
---|
| 5 | import { EMPTY } from './empty';
|
---|
| 6 |
|
---|
| 7 | /* tslint:disable:max-line-length */
|
---|
| 8 | export function onErrorResumeNext<R>(v: ObservableInput<R>): Observable<R>;
|
---|
| 9 | export function onErrorResumeNext<T2, T3, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>): Observable<R>;
|
---|
| 10 | export function onErrorResumeNext<T2, T3, T4, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>): Observable<R>;
|
---|
| 11 | export function onErrorResumeNext<T2, T3, T4, T5, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>): Observable<R>;
|
---|
| 12 | export function onErrorResumeNext<T2, T3, T4, T5, T6, R>(v2: ObservableInput<T2>, v3: ObservableInput<T3>, v4: ObservableInput<T4>, v5: ObservableInput<T5>, v6: ObservableInput<T6>): Observable<R>;
|
---|
| 13 |
|
---|
| 14 | export function onErrorResumeNext<R>(...observables: Array<ObservableInput<any> | ((...values: Array<any>) => R)>): Observable<R>;
|
---|
| 15 | export function onErrorResumeNext<R>(array: ObservableInput<any>[]): Observable<R>;
|
---|
| 16 | /* tslint:enable:max-line-length */
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * When any of the provided Observable emits an complete or error notification, it immediately subscribes to the next one
|
---|
| 20 | * that was passed.
|
---|
| 21 | *
|
---|
| 22 | * <span class="informal">Execute series of Observables no matter what, even if it means swallowing errors.</span>
|
---|
| 23 | *
|
---|
| 24 | * ![](onErrorResumeNext.png)
|
---|
| 25 | *
|
---|
| 26 | * `onErrorResumeNext` Will subscribe to each observable source it is provided, in order.
|
---|
| 27 | * If the source it's subscribed to emits an error or completes, it will move to the next source
|
---|
| 28 | * without error.
|
---|
| 29 | *
|
---|
| 30 | * If `onErrorResumeNext` is provided no arguments, or a single, empty array, it will return {@link index/EMPTY}.
|
---|
| 31 | *
|
---|
| 32 | * `onErrorResumeNext` is basically {@link concat}, only it will continue, even if one of its
|
---|
| 33 | * sources emits an error.
|
---|
| 34 | *
|
---|
| 35 | * Note that there is no way to handle any errors thrown by sources via the result of
|
---|
| 36 | * `onErrorResumeNext`. If you want to handle errors thrown in any given source, you can
|
---|
| 37 | * always use the {@link catchError} operator on them before passing them into `onErrorResumeNext`.
|
---|
| 38 | *
|
---|
| 39 | * ## Example
|
---|
| 40 | * Subscribe to the next Observable after map fails</caption>
|
---|
| 41 | * ```ts
|
---|
| 42 | * import { onErrorResumeNext, of } from 'rxjs';
|
---|
| 43 | * import { map } from 'rxjs/operators';
|
---|
| 44 | *
|
---|
| 45 | * onErrorResumeNext(
|
---|
| 46 | * of(1, 2, 3, 0).pipe(
|
---|
| 47 | * map(x => {
|
---|
| 48 | * if (x === 0) throw Error();
|
---|
| 49 | * return 10 / x;
|
---|
| 50 | * })
|
---|
| 51 | * ),
|
---|
| 52 | * of(1, 2, 3),
|
---|
| 53 | * )
|
---|
| 54 | * .subscribe(
|
---|
| 55 | * val => console.log(val),
|
---|
| 56 | * err => console.log(err), // Will never be called.
|
---|
| 57 | * () => console.log('done'),
|
---|
| 58 | * );
|
---|
| 59 | *
|
---|
| 60 | * // Logs:
|
---|
| 61 | * // 10
|
---|
| 62 | * // 5
|
---|
| 63 | * // 3.3333333333333335
|
---|
| 64 | * // 1
|
---|
| 65 | * // 2
|
---|
| 66 | * // 3
|
---|
| 67 | * // "done"
|
---|
| 68 | * ```
|
---|
| 69 | *
|
---|
| 70 | * @see {@link concat}
|
---|
| 71 | * @see {@link catchError}
|
---|
| 72 | *
|
---|
| 73 | * @param {...ObservableInput} sources Observables (or anything that *is* observable) passed either directly or as an array.
|
---|
| 74 | * @return {Observable} An Observable that concatenates all sources, one after the other,
|
---|
| 75 | * ignoring all errors, such that any error causes it to move on to the next source.
|
---|
| 76 | */
|
---|
| 77 | export function onErrorResumeNext<T, R>(...sources: Array<ObservableInput<any> |
|
---|
| 78 | Array<ObservableInput<any>> |
|
---|
| 79 | ((...values: Array<any>) => R)>): Observable<R> {
|
---|
| 80 |
|
---|
| 81 | if (sources.length === 0) {
|
---|
| 82 | return EMPTY;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | const [ first, ...remainder ] = sources;
|
---|
| 86 |
|
---|
| 87 | if (sources.length === 1 && isArray(first)) {
|
---|
| 88 | return onErrorResumeNext(...first);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return new Observable(subscriber => {
|
---|
| 92 | const subNext = () => subscriber.add(
|
---|
| 93 | onErrorResumeNext(...remainder).subscribe(subscriber)
|
---|
| 94 | );
|
---|
| 95 |
|
---|
| 96 | return from(first).subscribe({
|
---|
| 97 | next(value) { subscriber.next(value); },
|
---|
| 98 | error: subNext,
|
---|
| 99 | complete: subNext,
|
---|
| 100 | });
|
---|
| 101 | });
|
---|
| 102 | }
|
---|