我有这部史诗:
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))
})
})
但是它阻止了前两个动作的发送。
答案 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)
)
)