如何在注销时重置ngrx效果?NGRX效率



请告诉我如何重置某些操作(用户注销(的所有效果?我想重置LOG_OUT操作的所有效果。例如:

  1. 订阅某些操作的某些效果
  2. 触发takeUntil()内部效应
  3. 注销
  4. 重置所有效果
  5. 再次在相同的动作上订阅相同的效果(来自步骤1(

此时步骤5不起作用,导致takeUntil()取消订阅该效果。

我添加了mergeMap并将takeUntil放在那里。现在很好。

@Effect() createConversation$ = this.actions$.pipe(
ofType(CREATE_CONVERSATION),
map((action: CreateConversation) => action.payload),
withLatestFrom(this.store.pipe(select(selectConversation))),
mergeMap(([message, mdConversation]) => {
return this.httpService
.createConversation(mdConversation.data.taskId, message)
.pipe(
map(
result =>
new CreateConversationComplete({
id: result.data.id,
tmpId: mdConversation.data.id
})
),
catchError((error: MyError) => {
if (error.type === MyerrorTypes.NETWORK) {
return of(new CreateConversationRetry(message));
}
if (error.type === MyerrorTypes.APPLICATION) {
return of(new CreateConversationError(mdConversation.data));
}
}),
takeUntil(this.actions$.pipe(ofType(LOG_OUT)))
);
})
);

@Effect() createConversationRetry$ = this.actions$.pipe(
ofType(CREATE_CONVERSATION_RETRY),
mergeMap((action: CustomAction) =>
of(action).pipe(
delay(NETWORK_TIMEOUT),
map(data => new CreateConversation(action.payload)),
takeUntil(this.actions$.pipe(ofType(LOG_OUT)))
)
)
);

问题是takeUntil完成了一个可观察的:https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeUntil.ts#L85并且您不能再次订阅。如果你需要在用户注销后暂停一些效果,我会使用某种过滤:

withLatestFrom(..is logged in selector)
filter((isLoggedIn: boolean) => isLoggedIn)

最新更新