在redux-saga中增加id有没有最佳实践方法?
这就是我的做法,但是如果同时发送多个请求,则多个事物将获得相同的id:
我的减速器数据形状是这样的:
const INITIAL = {
lastId: -1,
entries: []
}
这是我的传奇:
function* requestDownloadWorker(action: RequestAction) {
// other workers should wait
const id = (yield select()).downloads.lastId;
yield put(increment());
console.log('incremented the lastId incremented, for whoever needs it next');
// other workers can now continue
// below logic - should happen in parallel with other workers
}
function* requestDownloadWatcher() {
yield takeEvery(REQUEST, requestDownloadWorker);
}
sagas.push(requestDownloadWatcher);
我想我需要takeEvery
但是排队worker
直到前一个worker
宣布它已经完成了id
,这可能吗?
您可以创建一个 actionChannel 来缓冲所有 REQUEST 操作,直到您"执行"它们。
import { actionChannel, select, take } from 'redux-saga/effects'
...
function* requestDownloadWorker() {
const channel = yield actionChannel(REQUEST)
while (true) {
const action = yield take(channel)
const id = (yield select()).downloads.lastId
yield put(increment())
yield fork(doOtherParallelStuff)
}
}
它应该有效。