Last active
August 15, 2017 21:32
-
-
Save rt2zz/2eddff64ddf258412760a5328bc0c99a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return (state: State = defaultState, action: Action) => { | |
let { _persist, ...rest } = state || {} | |
let restState: State = rest | |
switch (action.type) { | |
case PERSIST: | |
if (state._persist) { | |
console.warn( | |
'redux-persist: unexpected _persist state before PERSIST action is handled. If you are doing hmr or code-splitting this may be a valid use case. Please open a ticket, requires further review.' | |
) | |
return state | |
} | |
if (typeof action.rehydrate !== 'function') | |
throw new Error( | |
'redux-persist: action.rehydrate is not a function. This can happen if the action is being replayed. This is an unexplored use case, please open an issue and we will figure out a resolution.' | |
) | |
if (typeof action.register !== 'function') | |
throw new Error( | |
'redux-persist: action.register is not a function. This can happen if the action is being replayed. This is an unexplored use case, please open an issue and we will figure out a resolution.' | |
) | |
let rehydrate = action.rehydrate | |
action.register(config.key) | |
getStoredState(config, (err, restoredState) => { | |
_persistoid = createPersistoid(config) | |
if (err) { | |
action.rehydrate(config.key, undefined, err) | |
} else { | |
const migrate = config.migrate || ((s, v) => Promise.resolve(s)) | |
migrate(restoredState, version).then( | |
migratedState => { | |
action.rehydrate(config.key, migratedState) | |
}, | |
migrateErr => { | |
if (process.env.NODE_ENV !== 'production' && migrateErr) | |
console.error('redux-persist: migration error', migrateErr) | |
action.rehydrate(config.key, undefined, migrateErr) | |
} | |
) | |
} | |
}) | |
return { ...state, _persist: { version, rehydrated: false } } | |
case REHYDRATE: | |
// noop if purging | |
if (_purge) return state | |
// @NOTE if key does not match, will continue to default case | |
if (action.key === config.key) { | |
let reducedState = baseReducer(restState, action) | |
let inboundState = action.payload | |
let reconciledRest: State = stateReconciler( | |
state, | |
inboundState, | |
reducedState, | |
config | |
) | |
return { | |
...reconciledRest, | |
_persist: { ..._persist, rehydrated: true }, | |
} | |
} | |
case PURGE: | |
_purge = true | |
purgeStoredState(config) | |
return state | |
} | |
// @NOTE default pass through reducer if the switch statement above did not return | |
// @TODO more performant workaround for combineReducers warning | |
let newState = { | |
...baseReducer(restState, action), | |
_persist, | |
} | |
_persistoid && _persistoid.update(newState) | |
return newState | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment