Merge pull request #101 from conundrumer/master

add actionDeserializer callback to persistState
This commit is contained in:
Dan Abramov 2015-09-12 16:54:46 +03:00
commit 431adfd611

View File

@ -1,30 +1,50 @@
export default function persistState(sessionId, deserializer = null) { export default function persistState(sessionId, stateDeserializer = null, actionDeserializer = null) {
if (!sessionId) { if (!sessionId) {
return next => (...args) => next(...args); return next => (...args) => next(...args);
} }
function deserializeState(fullState) { function deserializeState(fullState) {
if (!fullState || typeof deserializer !== 'function') {
return fullState;
}
return { return {
...fullState, ...fullState,
committedState: deserializer(fullState.committedState), committedState: stateDeserializer(fullState.committedState),
computedStates: fullState.computedStates.map((computedState) => { computedStates: fullState.computedStates.map((computedState) => {
return { return {
...computedState, ...computedState,
state: deserializer(computedState.state) state: stateDeserializer(computedState.state)
}; };
}) })
}; };
} }
function deserializeActions(fullState) {
return {
...fullState,
stagedActions: fullState.stagedActions.map((action) => {
return actionDeserializer(action);
})
};
}
function deserialize(fullState) {
if (!fullState) {
return fullState;
}
let deserializedState = fullState;
if (typeof stateDeserializer === 'function') {
deserializedState = deserializeState(deserializedState);
}
if (typeof actionDeserializer === 'function') {
deserializedState = deserializeActions(deserializedState);
}
return deserializedState;
}
return next => (reducer, initialState) => { return next => (reducer, initialState) => {
const key = `redux-dev-session-${sessionId}`; const key = `redux-dev-session-${sessionId}`;
let finalInitialState; let finalInitialState;
try { try {
finalInitialState = deserializeState(JSON.parse(localStorage.getItem(key))) || initialState; finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState;
next(reducer, initialState); next(reducer, initialState);
} catch (e) { } catch (e) {
console.warn('Could not read debug session from localStorage:', e); console.warn('Could not read debug session from localStorage:', e);