ngrx效应:期望两个操作都完成



如何创建只有在两个操作都完成并返回值时才会触发的ngrx效果?

示例:actionASuccess+actionBSuccess->影响

我尝试了这个解决方案:如何使用combineLatest或Type(…waitFor(在@ngrx/effects中等待2个操作,但两者都无法按预期工作。

这是我的密码。switchMap的内容被调用两次,但应该只调用一次:

navigateEmployeeNotificationsTypeSuccess$ = createEffect(() =>
combineLatest([
this.actions$.pipe(ofType(RouterActions.navigateSuccess)),
this.actions$.pipe(ofType(RouterActions.navigateEmployeeNotificationsTypeSuccess)),
]).pipe(
filter(
([action1, action2]) =>
RouterEffects.notNull(action1.success) && RouterEffects.notNull(action2.tab) && RouterEffects.notNull(action2.notificationsType),
),
switchMap(([{ success }, { tab, notificationsType }]) => {
if (success) {
// have to set all sections active = false before changing state of the current
const data = cloneDeep(tab.data);
data.notificationTypes = data.notificationTypes.map((notificationType: NotificationType) => {
const active = isEqual(notificationType, notificationsType);
return {
...notificationType,
active,
};
});
return [
TabsActions.setTabData({ tab, data }),
TabsActions.saveCurrentRoute({ url: this.router.url }),
TabsActions.replaceLastBreadcrumb({ breadcrumb: notificationsType.managerName }),
];
}
return EMPTY;
}),
catchError((error) => of(requestError({ error }))),
),

);

我认为正在发生的事情是RouterActions.navigateSuccess被多次调用,因此combineLatest将多次发出值(还假设RouterActions.navigateEmployeeNotificationsTypeSuccess发出了单个值(。

我将向您推荐的方法是使用switchMap运算符来连锁这两个操作,否则当您导航到navigateEmployeeNotificationsTypeSuccess=>导航出去=>再次导航到navigateEmployeeNotificationsTypeSuccess,在这种情况下,您将执行效果内部的逻辑更多次。

因此,我的基于switchMap的方法看起来如下(假设RouterActions.navigateSuccessRouterActions.navigateEmployeeNotificationsTypeSuccess之前被调度(

navigateEmployeeNotificationsTypeSuccess$ = createEffect(() =>
combineLatest([
this.actions$.pipe(ofType(RouterActions.navigateSuccess)),
this.actions$.pipe(ofType(RouterActions.navigateEmployeeNotificationsTypeSuccess)),
])
...
To be
navigateEmployeeNotificationsTypeSuccess$ = createEffect(() =>
this.actions$.pipe(ofType(RouterActions.navigateSuccess)).pipe(
switchmap(() => this.actions$.pipe(ofType(RouterActions.navigateEmployeeNotificationsTypeSuccess)))
...

相关内容

  • 没有找到相关文章

最新更新