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

时间:2020-04-28 10:45:14

标签: angular ngrx ngrx-effects

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

  1. 订阅某些动作的效果
  2. 触发takeUntil()内部效果
  3. 注销
  4. 重置所有效果
  5. 再次对同一动作进行相同的效果订阅(从步骤1开始)。

当前第5步不起作用,导致takeUntil()取消订阅该效果。

2 个答案:

答案 0 :(得分:0)

问题在于takeUntil完成了一个可观察的结果:https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/takeUntil.ts#L85 而且您无法再次订阅。如果您需要在用户注销后暂停某些效果,则可以使用以下过滤方式:

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

答案 1 :(得分:0)

我添加了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)))
          )
        )
      );