From 3294864b7dbd8bd2b60e1d4c483ca4576820f3a7 Mon Sep 17 00:00:00 2001 From: David Lu Date: Thu, 10 Sep 2015 23:44:49 -0400 Subject: [PATCH 1/2] add actionDeserializer callback to persistState --- src/persistState.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/persistState.js b/src/persistState.js index 1469ab24..69e9e6dc 100644 --- a/src/persistState.js +++ b/src/persistState.js @@ -1,30 +1,42 @@ -export default function persistState(sessionId, deserializer = null) { +export default function persistState(sessionId, stateDeserializer = null, actionDeserializer = null) { if (!sessionId) { return next => (...args) => next(...args); } function deserializeState(fullState) { - if (!fullState || typeof deserializer !== 'function') { + if (!fullState || typeof stateDeserializer !== 'function') { return fullState; } return { ...fullState, - committedState: deserializer(fullState.committedState), + committedState: stateDeserializer(fullState.committedState), computedStates: fullState.computedStates.map((computedState) => { return { ...computedState, - state: deserializer(computedState.state) + state: stateDeserializer(computedState.state) }; }) }; } + function deserializeActions(fullState) { + if (!fullState || typeof actionDeserializer !== 'function') { + return fullState; + } + return { + ...fullState, + stagedActions: fullState.stagedActions.map((action) => { + return actionDeserializer(action); + }) + }; + } + return next => (reducer, initialState) => { const key = `redux-dev-session-${sessionId}`; let finalInitialState; try { - finalInitialState = deserializeState(JSON.parse(localStorage.getItem(key))) || initialState; + finalInitialState = deserializeActions(deserializeState(JSON.parse(localStorage.getItem(key)))) || initialState; next(reducer, initialState); } catch (e) { console.warn('Could not read debug session from localStorage:', e); From b4ea5c278e94c619c61c49c35e12721a7345604d Mon Sep 17 00:00:00 2001 From: David Lu Date: Thu, 10 Sep 2015 23:46:33 -0400 Subject: [PATCH 2/2] refactor deserialization in persistState --- src/persistState.js | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/persistState.js b/src/persistState.js index 69e9e6dc..976a3b05 100644 --- a/src/persistState.js +++ b/src/persistState.js @@ -4,9 +4,6 @@ export default function persistState(sessionId, stateDeserializer = null, action } function deserializeState(fullState) { - if (!fullState || typeof stateDeserializer !== 'function') { - return fullState; - } return { ...fullState, committedState: stateDeserializer(fullState.committedState), @@ -20,9 +17,6 @@ export default function persistState(sessionId, stateDeserializer = null, action } function deserializeActions(fullState) { - if (!fullState || typeof actionDeserializer !== 'function') { - return fullState; - } return { ...fullState, stagedActions: fullState.stagedActions.map((action) => { @@ -31,12 +25,26 @@ export default function persistState(sessionId, stateDeserializer = null, 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) => { const key = `redux-dev-session-${sessionId}`; let finalInitialState; try { - finalInitialState = deserializeActions(deserializeState(JSON.parse(localStorage.getItem(key)))) || initialState; + finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState; next(reducer, initialState); } catch (e) { console.warn('Could not read debug session from localStorage:', e);