| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { innerFrom } from '../observable/innerFrom'; |
| 3 |
import { Subject } from '../Subject'; |
| 4 |
import { Subscription } from '../Subscription'; |
| 5 |
|
| 6 |
import { MonoTypeOperatorFunction, ObservableInput } from '../types'; |
| 7 |
import { operate } from '../util/lift'; |
| 8 |
import { createOperatorSubscriber } from './OperatorSubscriber'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Returns an Observable that mirrors the source Observable with the exception of an `error`. If the source Observable |
| 12 |
* calls `error`, this method will emit the Throwable that caused the error to the `ObservableInput` returned from `notifier`. |
| 13 |
* If that Observable calls `complete` or `error` then this method will call `complete` or `error` on the child |
| 14 |
* subscription. Otherwise this method will resubscribe to the source Observable. |
| 15 |
* |
| 16 |
*  |
| 17 |
* |
| 18 |
* Retry an observable sequence on error based on custom criteria. |
| 19 |
* |
| 20 |
* ## Example |
| 21 |
* |
| 22 |
* ```ts |
| 23 |
* import { interval, map, retryWhen, tap, delayWhen, timer } from 'rxjs'; |
| 24 |
* |
| 25 |
* const source = interval(1000); |
| 26 |
* const result = source.pipe( |
| 27 |
* map(value => { |
| 28 |
* if (value > 5) { |
| 29 |
* // error will be picked up by retryWhen |
| 30 |
* throw value; |
| 31 |
* } |
| 32 |
* return value; |
| 33 |
* }), |
| 34 |
* retryWhen(errors => |
| 35 |
* errors.pipe( |
| 36 |
* // log error message |
| 37 |
* tap(value => console.log(`Value ${ value } was too high!`)), |
| 38 |
* // restart in 5 seconds |
| 39 |
* delayWhen(value => timer(value * 1000)) |
| 40 |
* ) |
| 41 |
* ) |
| 42 |
* ); |
| 43 |
* |
| 44 |
* result.subscribe(value => console.log(value)); |
| 45 |
* |
| 46 |
* // results: |
| 47 |
* // 0 |
| 48 |
* // 1 |
| 49 |
* // 2 |
| 50 |
* // 3 |
| 51 |
* // 4 |
| 52 |
* // 5 |
| 53 |
* // 'Value 6 was too high!' |
| 54 |
* // - Wait 5 seconds then repeat |
| 55 |
* ``` |
| 56 |
* |
| 57 |
* @see {@link retry} |
| 58 |
* |
| 59 |
* @param notifier Function that receives an Observable of notifications with which a |
| 60 |
* user can `complete` or `error`, aborting the retry. |
| 61 |
* @return A function that returns an Observable that mirrors the source |
| 62 |
* Observable with the exception of an `error`. |
| 63 |
* @deprecated Will be removed in v9 or v10, use {@link retry}'s `delay` option instead. |
| 64 |
* Will be removed in v9 or v10. Use {@link retry}'s {@link RetryConfig#delay delay} option instead. |
| 65 |
* Instead of `retryWhen(() => notify$)`, use: `retry({ delay: () => notify$ })`. |
| 66 |
*/ |
| 67 |
export function retryWhen<T>(notifier: (errors: Observable<any>) => ObservableInput<any>): MonoTypeOperatorFunction<T> { |
| 68 |
return operate((source, subscriber) => { |
| 69 |
let innerSub: Subscription | null; |
| 70 |
let syncResub = false; |
| 71 |
let errors$: Subject<any>; |
| 72 |
|
| 73 |
const subscribeForRetryWhen = () => { |
| 74 |
innerSub = source.subscribe( |
| 75 |
createOperatorSubscriber(subscriber, undefined, undefined, (err) => { |
| 76 |
if (!errors$) { |
| 77 |
errors$ = new Subject(); |
| 78 |
innerFrom(notifier(errors$)).subscribe( |
| 79 |
createOperatorSubscriber(subscriber, () => |
| 80 |
// If we have an innerSub, this was an asynchronous call, kick off the retry. |
| 81 |
// Otherwise, if we don't have an innerSub yet, that's because the inner subscription |
| 82 |
// call hasn't even returned yet. We've arrived here synchronously. |
| 83 |
// So we flag that we want to resub, such that we can ensure finalization |
| 84 |
// happens before we resubscribe. |
| 85 |
innerSub ? subscribeForRetryWhen() : (syncResub = true) |
| 86 |
) |
| 87 |
); |
| 88 |
} |
| 89 |
if (errors$) { |
| 90 |
// We have set up the notifier without error. |
| 91 |
errors$.next(err); |
| 92 |
} |
| 93 |
}) |
| 94 |
); |
| 95 |
|
| 96 |
if (syncResub) { |
| 97 |
// Ensure that the inner subscription is torn down before |
| 98 |
// moving on to the next subscription in the synchronous case. |
| 99 |
// If we don't do this here, all inner subscriptions will not be |
| 100 |
// torn down until the entire observable is done. |
| 101 |
innerSub.unsubscribe(); |
| 102 |
innerSub = null; |
| 103 |
// We may need to do this multiple times, so reset the flag. |
| 104 |
syncResub = false; |
| 105 |
// Resubscribe |
| 106 |
subscribeForRetryWhen(); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
// Start the subscription |
| 111 |
subscribeForRetryWhen(); |
| 112 |
}); |
| 113 |
} |