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

问题描述 投票:1回答:1

我有以下效果:

  @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()})
    ])
);

如何调度多个操作并以延迟方式处理一个不同/异步?

angular rxjs observable ngrx ngrx-effects
1个回答
2
投票

您可以代替数组返回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),
  ),
)),
© www.soinside.com 2019 - 2024. All rights reserved.