如何等待另一个收听相同动作流的史诗

How to wait for another epic who listens to same action stream

所以在我努力掌握 RXjs 的过程中,我 运行 遇到了以下情况;当应用程序 rehydrates(第一次引导)时,我发送一个相应的 REHYDRATE 动作。我有两个 epics 听这个动作,但我希望第二个史诗只在第一个史诗结束时被调用。我目前的尝试如下所示;

export const rehydrateEpic = (
  action$: ActionsObservable
) =>
  action$.ofType(REHYDRATE)
    .switchMap(() => {
      // Wait for the JWT token to be successfully fetched.
      return action$.ofType(GET_JWT_SUCCESS)
        // When this line is removed, you will disappear in a spacetime continuum.
        .take(1)
        .do(action => {
          // I get the action here...
          console.log(action)
        })
        .switchMap(action => {
          return action$.ofType([
              actions.GET_ACCOUNT_INFO_SUCCESS,
              actions.GET_ACCOUNT_INFO_FAILURE,
            ])
            .do(action => {
              // ...but this one is never reached :(
              console.log(action)
            })
            .startWith(actions.getAccountInfo())
        })
    })

我在我的开发工具中看到我得到了一个 GET_ACCOUNT_INFO_FAILURE 所以 Redux 流程似乎以某种方式工作。

I have two epics listening to this action, but I want the second epic only to be invoked when the first epic finishes.

听起来第二部史诗不应该监听 REHYDRATE,而是应该监听第一部史诗完成后发出的动作。听起来怎么样?

所以我自己想通了。通过使用 zip(),我可以等待两个流发出。在我的例子中,它看起来像这样;

export const rehydrateEpic = (
  action$: ActionsObservable
) =>
  action$.ofType(REHYDRATE)
    // Wait for the JWT token to be successfully fetched.
    .zip(() => action$.ofType(GET_JWT_SUCCESS))
    .switchMap(() => {
      return action$.ofType([
          actions.GET_ACCOUNT_INFO_SUCCESS,
          actions.GET_ACCOUNT_INFO_FAILURE,
        ])
        .do(action => {
          // ...but this one is never reached :(
          console.log(action)
        })
        .startWith(actions.getAccountInfo())
    })