redux-pouchdb
redux-pouchdb copied to clipboard
Multiple persistent reducers override reducers state
Hi,
The state of my reducers is overridden with one of the reducers state if there is more than one persistent reducer.
This is what I have in my root reducer:
pouchdb => combineReducers({
localize,
authentication,
financialForecast: FinancialForecastReducer,
wallets: persistentDocumentReducer(pouchdb, 'wallets')(walletsReducer),
tags: persistentDocumentReducer(pouchdb, 'tags')(tagsReducer),
rules: rulesReducer,
contracts: contractsReducer,
budgets: budgetsReducer,
})
I debugged and found that the middleware is initializing both reducers with the two documents (wallets and tags) in pouchdb. As these are synchronous actions both end with reducer in the same state.
The next code change in persistentObjectReducer fixes this issue.
const setReducer = (store, doc, reducerName) => {
const { _id, _rev, state } = doc
if (_id === reducerName) {
store.dispatch({
type: SET_OBJECT_REDUCER,
reducerName, //_id,
state,
_rev
})
}
}
I added the condition to prevent the reducer from being wrongly initialized.
If you feel this code doesn't have any implications in the rest of the library I can create a PR.
You should definitely PR this and people can comment on it in the PR review, your reasoning makes sense to me.