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