【发布时间】:2020-04-16 22:22:40
【问题描述】:
我基本上是在寻找史诗:
- Epic A - 当 REQUEST_A 进入时,发出动作 EPIC_A_STARTED,在一段时间后(异步动作)发出动作 EPIC_A_END
- Epic B - 当 REQUEST_B 进来时,如果 Epic A 还没有开始继续,否则等到 EPIC_A_END 完成。
理想情况下,两个史诗在运行时都会等待它们的替代完成,因此一次只发生一个。
感觉就像我需要一个计数器在 EPIC_A_STARTED 发生时递增,在 EPIC_A_COMPLETED 发生时递减,但我不确定如何继续。
如果我们能有一个类似这样的伪代码的操作符就好了:
// Operator we are trying to write
const waitForActionsToComplete = (startAction, endAction) => {
// Not sure how to structure this so that it waits for there to be equal of both actions passing through
}
// Epic A
const EPIC_A_FILTER = (action$, state$) =>
action$.pipe(
of(actions.REQUEST_A),
waitForActionsToComplete(actions.EPIC_B_START, actions.EPIC_B_END),
mergeMap(() => { type: actions.EPIC_A_START }));
const EPIC_A_PROCESS = (action$, state$) =>
action$.pipe(
of(actions.EPIC_A_START),
...async task,
mergeMap(() => { type: actions.EPIC_A_END }));
// Epic B
const EPIC_B_FILTER = (action$, state$) =>
action$.pipe(
of(actions.REQUEST_B),
waitForActionsToComplete(actions.EPIC_A_START, actions.EPIC_A_END),
mergeMap(() => { type: actions.EPIC_B_START }));
const EPIC_B_PROCESS = (action$, state$) =>
action$.pipe(
of(actions.EPIC_B_START),
...async task,
mergeMap(() => { type: actions.EPIC_B_END }));
【问题讨论】:
标签: javascript rxjs redux-observable