Redux可观察的史诗,设置动作之间的超时间隔

时间:2019-04-25 00:56:04

标签: react-redux rxjs5 redux-observable

我有这部史诗:

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))
    })
  })

但是它阻止了前两个动作的发送。

1 个答案:

答案 0 :(得分: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)
    )
  )
相关问题