autoMergeLevel1.js.flow 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // @flow
  2. /*
  3. autoMergeLevel1:
  4. - merges 1 level of substate
  5. - skips substate if already modified
  6. */
  7. import type { PersistConfig } from '../types'
  8. export default function autoMergeLevel1<State: Object>(
  9. inboundState: State,
  10. originalState: State,
  11. reducedState: State,
  12. { debug }: PersistConfig
  13. ): State {
  14. let newState = { ...reducedState }
  15. // only rehydrate if inboundState exists and is an object
  16. if (inboundState && typeof inboundState === 'object') {
  17. Object.keys(inboundState).forEach(key => {
  18. // ignore _persist data
  19. if (key === '_persist') return
  20. // if reducer modifies substate, skip auto rehydration
  21. if (originalState[key] !== reducedState[key]) {
  22. if (process.env.NODE_ENV !== 'production' && debug)
  23. console.log(
  24. 'redux-persist/stateReconciler: sub state for key `%s` modified, skipping.',
  25. key
  26. )
  27. return
  28. }
  29. // otherwise hard set the new value
  30. newState[key] = inboundState[key]
  31. })
  32. }
  33. if (
  34. process.env.NODE_ENV !== 'production' &&
  35. debug &&
  36. inboundState &&
  37. typeof inboundState === 'object'
  38. )
  39. console.log(
  40. `redux-persist/stateReconciler: rehydrated keys '${Object.keys(
  41. inboundState
  42. ).join(', ')}'`
  43. )
  44. return newState
  45. }