[RxJS] Simplified retryWhen and repeatWhen

src$.pipe(
    retryWhen(error$ => error$.pipe(
        switchMap(getNotifier)
    ))
)

// can just be
src$.pipe(
    retry({
        delay: getNotifier
    })
)

/**
 * The {@link retry} operator configuration object. `retry` either accepts a `number`
 * or an object described by this interface.
 */
export interface RetryConfig {
  /**
   * The maximum number of times to retry. If `count` is omitted, `retry` will try to
   * resubscribe on errors infinite number of times.
   */
  count?: number;
  /**
   * The number of milliseconds to delay before retrying, OR a function to
   * return a notifier for delaying. If a function is given, that function should
   * return a notifier that, when it emits will retry the source. If the notifier
   * completes _without_ emitting, the resulting observable will complete without error,
   * if the notifier errors, the error will be pushed to the result.
   */
  delay?: number | ((error: any, retryCount: number) => ObservableInput<any>);
  /**
   * Whether or not to reset the retry counter when the retried subscription
   * emits its first value.
   */
  resetOnSuccess?: boolean;
}

 

// Before: a simple daly
src$.pipe(
    retryWhen(error$ => error$.pipe(
        switchMap(() => timer(5000))
    ))
)

// Now
src$.pipe(
    retry({
        delay: 5000
    })
)

 

posted @ 2022-03-30 15:21  Zhentiw  阅读(112)  评论(0编辑  收藏  举报