Is there a best practice way for incrementing id's in redux-saga?
This is how I am doing it, but if multiple requests dispatched at same time, multiple things are getting the same id:
My data shape of the reducer is this:
const INITIAL = {
lastId: -1,
entries: []
}
And here is my saga:
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);
I think I need to takeEvery but queue worker until the previous worker has declared it has finished taking an id, is this possible?
You could create an actionChannel that buffers all REQUEST actions until you "take" them.
It should work.