NGRX 连接两个动作

NGRX concat two action

我正在使用 NGRX 和 Angular11。 我正在尝试,当一个动作被执行时,调用另一个动作,听它的成功,如果它成功调度最后的动作:

这里我的代码有点简化:

@Effect()
  getUserSettingSuccess$ = this.actions$.pipe(
    // When this action `GetUserSettingsSuccess` is executed
    ofType<featureActions.GetUserSettingsSuccess>(featureActions.ActionTypes.GetUserSettingsSuccess),
    concatMap((action) =>
      of(action).pipe(withLatestFrom(this.store$.pipe(select(ProjectsStoreSelectors.selectedProjectId))))
    ),
    // I want to dispatch a new action 
    tap(([action, projectId]) => new ModelsStoreAction.GetAllModelsRequest({ projectId })),
    // and listen to the success / failure of that action. 
    // If success dispatch `SetModelSelection` else do nothing.
    map(([action]) =>
      this.actions$.pipe(
        ofType(ModelsStoreAction.ActionTypes.GetAllModelsSuccess),
        takeUntil(this.actions$.pipe(ofType(ModelsStoreAction.ActionTypes.GetAllCesiumModelsFailed))),
        first(),
        map(() => new ModelsStoreAction.SetModelSelection())
      )
    )

我遇到的问题是,上面的代码没有发送有效的操作。但是我对所有那些 rxjs 运算符有点迷茫。

这是我的方法:

@Effect()
  getUserSettingSuccess$ = this.actions$.pipe(
    // When this action `GetUserSettingsSuccess` is executed
    ofType<featureActions.GetUserSettingsSuccess>(featureActions.ActionTypes.GetUserSettingsSuccess),
    
    concatMap((action) =>
      of(action).pipe(withLatestFrom(this.store$.pipe(select(ProjectsStoreSelectors.selectedProjectId))))
    ),

    // I want to dispatch a new action 
    tap(([action, projectId]) => new ModelsStoreAction.GetAllModelsRequest({ projectId })),
    
    // and listen to the success / failure of that action. 
    // If success dispatch `SetModelSelection` else do nothing.
    switchMap(
      ([action]) => this.actions$.pipe(
        // adding all the possibilities here
        ofType(ModelsStoreAction.ActionTypes.GetAllModelsSuccess, ModelsStoreAction.ActionTypes.GetAllCesiumModelsFailed),

        first(),
        filter(a => a.type === ModelsStoreAction.ActionTypes.GetAllModelsSuccess.type),
        map(() => new ModelsStoreAction.SetModelSelection()),
      )
    )
  )

它之前没有用,因为 this.actions$ 本质上是一种 Subject 并且效果不应该 return 一个类似 Observable 的值。为了解决这个问题,我使用了 switchMap,它将自动处理内部订阅。