如何从 ngrx 效果中多次调度相同的操作

Cha*_*har 5 ngrx-effects angular ngrx-store

我想从我的效果中多次调度一个动作,为此我正在使用concatMap,但当我调度相同的动作时,它会被下一次调度取消。有没有办法在之前的调度完成后调度一个动作。

下面的代码将有助于更好地理解问题。

@Effect()
  setCategory$ = this.actions$
    .ofType(GET_SESSION_AD_SUCCESS)
    .concatMap((action: Action) => {

      const category = action.payload;
      const categoryPath = category.path;
      const categoryDispatchArray = [];

      categoryPath.forEach((path, index) => {
        categoryDispatchArray.push({
          type: GET_SUBCATEGORIES_BY_PARENT,
          payload: { categoryId: path.id, index }
        });
      })

      return dispatchArray;

}
Run Code Online (Sandbox Code Playgroud)

j2L*_*L4e 1

我认为你的问题是,当传递给 concatMapped Observable 的最后一个 Observable 关闭时,它就会关闭。你需要的就mergeMap(又名flatMap)在这里。

我还用它.map来创建dispatchArray:

@Effect()
  setCategory$ = this.actions$
    .ofType(GET_SESSION_AD_SUCCESS)
    .mergeMap((action: Action) => {

      const category = action.payload;
      const categoryPath = category.path;

      const categoryDispatchArray = categoryPath
        .map((path, index) => ({
          type: GET_SUBCATEGORIES_BY_PARENT,
          payload: { categoryId: path.id, index }
        });

      return categoryDispatchArray;
    }
Run Code Online (Sandbox Code Playgroud)