我希望通过Redux从应用程序的不同部分调用两组动作。但是,我想以正确的顺序运行它们,并确保set b永远不要在set a之前运行。
例如,假设我有两个动作configure
和execute
。我需要确保execute
永远不会在configure
之前运行,但是它们可能会无序分派。在那种情况下,我需要排队execute
,并且只在configure
完成后才进行处理。
这可能在redux-saga中做吗?
我尝试了以下操作,但似乎完全没有阻塞:
function* configureSaga({ payload }): IterableIterator<any> {
yield put(aFirstThing(payload));
yield put(aSecondThing());
}
function* executeSaga({ payload }): IterableIterator<any> {
yield put(aThirdThing(payload));
yield put(aFourthThing());
}
export function* adsSaga(): IterableIterator<any> {
const configureChanel = yield actionChannel({ type: 'configure' });
yield takeEvery(configureChanel, configureSaga);
yield takeEvery({ type: 'execute' }), executeSaga);
}
按以下顺序给出两个调度:
dispatch({ type: 'execute' })
dispatch({ type: 'configure' })
我需要按该顺序执行aFirstThing, aSecondThing, aThirdThing, aFourthThing
操作。
redux-sagas这样的事情有可能吗?
takeEvery
在大多数情况下很有用,但是对于更复杂的情况,您通常会最终写一个自定义循环。您可以大致按照以下步骤进行操作:
export function* adsSaga(): IterableIterator<any> {
const configureChanel = yield actionChannel('configure');
while (true) {
let payload = yield take(configureChanel)
yield call(configureSaga, payload)
payload = yield take('execute')
yield fork(executeSaga, payload)
}
}