如何在RXJS retryWhen中发射/合并另一个可观察的对象

问题描述 投票:0回答:1

我有一个可观察到的api请求,基本上我在事件连接失败/错误时添加了retryWhen。这样做时,我想在请求抛出错误时调度另一个操作(不通知用户系统正在重试。)

//....
export const saveArticle1 = (action$, state$) =>
  action$.pipe(
    ofType(AUTO_SAVE_ARTICLE_READY),
    withLatestFrom(state$, (a, b) => b),
    switchMap(({
      article,
    }) => {
      const payload = getArticlePayload(article);
      return ajax.patch(`/api/article/${article.id}`, payload, { 'Content-Type': 'application/json' }).pipe(
        flatMap(({ response }) => of({
          type: ARTICLE_AUTO_SAVED,
          value: response,
        })),
        retryWhen(errors =>
          errors.pipe(
            // TODO: I try to concat or merge observable here but no luck
            delay(1000),
            take(60),
          ),
        ),
        catchError((ex) => {
          articleSaveFailureAlert();
          return showErrorNotification('Api Error. Unable to save this article.')(ex);
        }),
      );
    }),
  );

[retryWhen中分派另一个动作的最佳方法是什么?还是有另一种方法可以实现这一目标?

我有一个可观察的api请求,基本上我在事件连接失败/错误时添加了retryWhen。这样做时,我想调度另一个操作(不通知用户系统正在重试。)...

javascript reactjs redux rxjs redux-observable
1个回答
0
投票

您可以在递归循环的停止条件下使用尝试次数大于允许的最大尝试次数的循环。然后,您可以将“重试”操作的单个可观察值与可观察到的补丁连接起来。如果您将代码段修改为将maxAttempts更改为小于5的数字,则会看到正在发出“失败”操作。

© www.soinside.com 2019 - 2024. All rights reserved.