从效果中调用多个动作

时间:2019-05-30 07:54:42

标签: ngrx

请观看修改

我有一种从http检索对象列表的效果。

检索后,我希望为每个对象分配一个相同的效果,该操作将打开一个通道以获取这些对象的实时更新

  @Effect()
  getUsedDrones$ = this.actions$.pipe(
    ofType<featureActions.GetUsedDronesRequest>(featureActions.ActionTypes.GetUsedDronesRequest),
    switchMap((action) =>
      this.droneDataService.getUsedDrones(action.payload).pipe(
        map(drones => {
          drones.forEach((drone) => new featureActions.OpenUsedDroneUpdateChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
          new featureActions.GetUsedDronesSuccess({drones});
        }),
        catchError((error: HttpErrorResponse) => {
          this.snackBar.open(this.translate.instant('ERRORS.HTTP.GENERIC'), this.translate.instant('BUTTONS.OK'), {duration: 2500});
          return of(new featureActions.GetUsedDronesFailed({error}));
        }),
      ),
    ),
  );

基本上,在调用我的api并检索了无人机之后,我需要为每架无人机调用

    map(drones => {
      drones.forEach((drone) => new featureActions.OpenUsedDroneUpdateChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
      new featureActions.GetUsedDronesSuccess({drones});
    }),

,然后触发检索成功。

其他动作基本上称为

  startUpdate$ = this.actions$.pipe(
    ofType<featureActions.OpenUsedDroneUpdateChannelRequest>(featureActions.ActionTypes.OpenUsedDroneUpdateChannelRequest),
    switchMap((action) =>
      this.droneDataService.openUsedDroneUpdateChannel(action.payload).pipe(
        map(result => {
          if (this.currentUpdateEventList.has(action.payload.droneId)) { return; }
          new featureActions.OpenUsedDroneUpdateChannelSuccess(result);
          this.currentUpdateEventList.set(action.payload.droneId, [
            `get_position_drone${action.payload.droneId}`,
            `get_status_drone${action.payload.droneId}`
          ])
          this.sseService.subscribe(`get_position_drone${action.payload.droneId}`, (position: Cartesian3) => {
            new featureActions.SetUsedDronePosition({droneId : action.payload.droneId, position});
          });
          this.sseService.subscribe(`get_status_drone${action.payload.droneId}`, (status: IDroneStatus) => {
            new featureActions.SetUsedDroneStatus({droneId : action.payload.droneId, status});
          });
        }),
        catchError((error: HttpErrorResponse) => {
          this.snackBar.open(this.translate.instant('ERRORS.HTTP.DRONE.NOT_UPDATABLE', {droneId: action.payload.droneId}), this.translate.instant('BUTTONS.OK'), {duration: 2500});
          return of(new featureActions.OpenUsedDroneUpdateChannelFailed({error}));
        }),
      )
    )
  );

现在,我遇到的问题如下,我的第一个foreach返回此错误

  

core.js:15724错误错误:影响“ UsedDronesEffect.getUsedDrones $”   调度了无效的操作:未定义

     

core.js:15724错误TypeError:操作必须是对象       在ActionsSubject.push ../ node_modules/@ngrx/store/fesm5/store.js.ActionsSubject.next

编辑:我修复了该错误,但是现在我的forEach操作没有被调度

    map(drones => {

      drones.forEach((drone) => new featureActions.OpenUsedDroneUpdateChannelRequest({ droneId: drone.id, projectId : environment.projectId }));
      return new featureActions.GetUsedDronesSuccess({drones});
    }),

如何根据我拥有的无人机数量返回操作列表,并以成功告终?

编辑2:

这行不行吗?

map(drones => {
  const actions = [];
  drones.forEach((drone) => actions.push(new featureActions.OpenUsedDroneUpdateChannelRequest({ droneId: drone.id, projectId : environment.projectId })));
  actions.push(new featureActions.GetUsedDronesSuccess({drones}));
  return merge(actions);
}),

1 个答案:

答案 0 :(得分:0)

检查一下。我希望这能解决您的问题。

dispatching multiple actions from single Effect @ngrx