Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple persistent reducers override reducers state #27

Open
fabioDMFerreira opened this issue Mar 4, 2020 · 1 comment
Open

Multiple persistent reducers override reducers state #27

fabioDMFerreira opened this issue Mar 4, 2020 · 1 comment

Comments

@fabioDMFerreira
Copy link

fabioDMFerreira commented Mar 4, 2020

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.

@lukevp
Copy link

lukevp commented Mar 10, 2020

You should definitely PR this and people can comment on it in the PR review, your reasoning makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants