Skip to content

Commit

Permalink
(persistReducer): return unmodified state if possible (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
rt2zz committed Feb 1, 2018
1 parent f89d60b commit 96525fb
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/persistReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default function persistReducer<State: Object, Action: Object>(
let _purge = false
let _paused = true
const conditionalUpdate = state => {
// update the persistoid only if we are rehydrated and not paused
state._persist.rehydrated &&
_persistoid &&
!_paused &&
Expand Down Expand Up @@ -147,13 +148,13 @@ export default function persistReducer<State: Object, Action: Object>(
// if we have not already handled PERSIST, straight passthrough
if (!_persist) return baseReducer(state, action)

// otherwise, pull off _persist, run the reducer, and update the persistoid
// @TODO more performant workaround for combineReducers warning
let newState = {
...baseReducer(restState, action),
_persist,
// run base reducer:
// is state modified ? return original : return updated
let newState = baseReducer(restState, action)
if (newState === restState) return state
else {
newState._persist = _persist
return conditionalUpdate(newState)
}
// update the persistoid only if we are already rehydrated and are not paused
return conditionalUpdate(newState)
}
}

0 comments on commit 96525fb

Please sign in to comment.