NGRX 效果:单独调度多个动作

NGRX Effects: Dispatch multiple actions individually

我的效果如下:

  @Effect()
  bookingSuccess$: Observable<Action> = this.actions$.pipe(
    ofType(BookingActionTypes.BOOK_SEAT_SUCCESS),
    map((action: BookSeatSuccess) => action.payload.userUuid),
    switchMap(userUuid => [
      new SetConfirmation({confirmationType: ConfirmationType.Booking}),
      new GetAllBookings({floorId: this.configService.getSelectedFloorId()}),
      new HighlightUser({highlightedUser: userUuid})
    ])
  );

我的目标是延迟发送最后一个操作。

不幸的是,将它放在它自己的 switchMap 中是行不通的,至少不是这样,因为那样的话一切都会延迟:

@Effect()
  bookingSuccess$: Observable<Action> = this.actions$.pipe(
    ofType(BookingActionTypes.BOOK_SEAT_SUCCESS),
    map((action: BookSeatSuccess) => action.payload.userUuid),
    switchMap(userUuid => {
      // DOES NOT WORK, BECAUSE NOW ALL ACTIONS ARE DELAYED 5s
      return of(new HighlightUser({highlightedUser: userUuid})).pipe(delay(5000));
    }
    switchMap(() => [
      new SetConfirmation({confirmationType: ConfirmationType.Booking}),
      new GetAllBookings({floorId: this.configService.getSelectedFloorId()})
    ])
);

如何分派多个动作并延迟处理一个 differently/async?

您可以代替数组 return merge(静态变体),然后将每个动作变成一个 Observable,然后只用 delay() 延迟最后一个动作。

switchMap(userUuid => merge(
  of(new SetConfirmation({confirmationType: ConfirmationType.Booking})),
  of(new GetAllBookings({floorId: this.configService.getSelectedFloorId()})),
  of(new HighlightUser({highlightedUser: userUuid})).pipe(
    delay(1000),
  ),
)),