Redux Observable epic,设置动作之间的超时间隔

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

我有这个史诗:

export const updateIsNotVeganInDbFulfilledEpic: Epic < * , * , * > = (
        action$: ActionsObservable < * > ,
        store: Store < * , * >
    ): Observable < any > =>
    action$.ofType('UPDATE_IS_NOT_VEGAN_IN_DB_FULFILLED').mergeMap(action => {
        return Observable.of(
            updateToastComponentIsOpen(true),
            updateToastComponentMessage(action.payload.response.errors[0])
        )
    })

如何在updateToastComponentIsOpen(false)之后2秒发出另一个动作(updateToastComponentIsOpen(true))?

我试过这个:

  action$.ofType('UPDATE_IS_NOT_VEGAN_IN_DB_FULFILLED').mergeMap(action => {
    return Observable.of(
      updateToastComponentIsOpen(true),
      updateToastComponentMessage(action.payload.response.errors[0])
    ).timeout(2000)
    .flatMap(function(result) {
      return Observable.of(updateToastComponentIsOpen(false))
    })
  })

但它阻止了前两个行动被派遣。

react-redux rxjs5 redux-observable
1个回答
0
投票

flatMap正在吞咽你的前两个动作。此外,如果某些内容未在指定的时间范围内到达,timeout将用于发送错误通知。

相反,你想引入一个delay

export const updateIsNotVeganInDbFulfilledEpic: Epic<*, *, *> = (
  action$: ActionsObservable<*>,
  store: Store<*, *>
): Observable<any> => action$
  .ofType('UPDATE_IS_NOT_VEGAN_IN_DB_FULFILLED')
  .mergeMap(action =>
    Observable.concat(
      Observable.of(
        updateToastComponentIsOpen(true),
        updateToastComponentMessage(action.payload.response.errors[0]),
      ),
      Observable.of(updateToastComponentIsOpen(false)).delay(2000)
    )
  )
© www.soinside.com 2019 - 2024. All rights reserved.