如果发生错误,需要从相同的效果中调用相同的NGRX效果

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

我需要连续调用 NGRX 效果两次,因此第一次如果效果出错,则更改参数数据并再次调用相同的效果。

例如:下面是我的操作,我想在特定的错误代码中连续回调两次。

connect$ = createEffect(() => this.actions$.pipe(
        ofType(MVHostActions.connect),
        withLatestFrom(
            this.store.pipe(select(selectService)),
            this.store.pipe(select(selectUser)),
        ),
        exhaustMap(([{ mvHost, route, navExtras, isUsingUserDefinedCustomViews },
        isService, appUser]) => 
            this.apiService.connect(mvHost).pipe(
                exhaustMap((apiResponse) => {
                    .....
                    .....
                }),
                takeUntil(this.actions$.pipe(ofType(MVHostActions.cancel))),
                catchError((error: any) => of(MVHostActions.connectToFailure({ error, navExtras, mvHost, route,isUsingUserDefinedCustomViews })))
            )
            )));

因此,在此效果期间,如果效果出错,那么在失败效果中,我做了以下事情。

connectToFailure1$ = createEffect(() => this.actions$.pipe(
        ofType(MVHostActions.connectToFailure),
        filter((error) => (error?.error?.error?.error?.code === DATASETS_ERROR || error?.error?.error?.error?.code === ARCHIVED_DATASETS_ERROR)),
        mergeMap(({navExtras,mvHost,route,isUsingUserDefinedCustomViews}) => {
                
               //Change the necessary data to by-pass that error
                
            return of (
                MVHostActions.connect({ navExtras, mvHost, route,isUsingUserDefinedCustomViews })
            );
        })
    ));

所以从上面的失败效果来看,我需要根据错误代码再次调用“connect”效果。

如果您可以提供不同的结构也可以,或者您可以提供在这种情况下调用效果的架构,这对我来说也有好处。

感谢大家...

angular typescript ngrx ngrx-effects
1个回答
0
投票

我不太明白这个问题。您是否正在寻找如何以不同方式做到这一点的建议?

恕我直言,建议的解决方案很好,我以前用过它。

Action A
Effect A => Error => Action B
Effect B => Action A
Effect A

根据更新参数的逻辑,效果也可以调度相同的操作。

Action A
Effect A => Error => Action A
Effect A
© www.soinside.com 2019 - 2024. All rights reserved.