From 6ee3b4373b7b2e1b4ad041ef9e86d4144abaa1f3 Mon Sep 17 00:00:00 2001 From: FredyC Date: Sun, 23 Aug 2015 13:43:26 +0200 Subject: [PATCH] using deserializer callback for persistState --- src/persistState.js | 40 ++++++---------------------------------- 1 file changed, 6 insertions(+), 34 deletions(-) diff --git a/src/persistState.js b/src/persistState.js index ecab0a54..1469ab24 100644 --- a/src/persistState.js +++ b/src/persistState.js @@ -1,47 +1,19 @@ -export default function persistState(sessionId, parsers) { +export default function persistState(sessionId, deserializer = null) { if (!sessionId) { return next => (...args) => next(...args); } - function callParser(parserMethod, parserName, stateSlice) { - const parser = parsers[parserName]; - if (parser && typeof parser[parserMethod] === 'function') { - return parser[parserMethod](stateSlice); - } - return stateSlice; - } - - // function serializeState(state) { - // if (!state) { - // return state; - // } - // return Object.keys(state).reduce((serialized, sliceName) => { - // serialized[sliceName] = callParser('serialize', sliceName, state[sliceName]); - // return serialized; - // }, {}); - // } - - function deserializeState(state) { - if (!state) { - return state; - } - return Object.keys(state).reduce((deserialized, sliceName) => { - deserialized[sliceName] = callParser('deserialize', sliceName, state[sliceName]); - return deserialized; - }, {}); - } - - function parseState(fullState) { - if (!fullState || !parsers) { + function deserializeState(fullState) { + if (!fullState || typeof deserializer !== 'function') { return fullState; } return { ...fullState, - committedState: deserializeState(fullState.committedState), + committedState: deserializer(fullState.committedState), computedStates: fullState.computedStates.map((computedState) => { return { ...computedState, - state: deserializeState(computedState.state) + state: deserializer(computedState.state) }; }) }; @@ -52,7 +24,7 @@ export default function persistState(sessionId, parsers) { let finalInitialState; try { - finalInitialState = parseState(JSON.parse(localStorage.getItem(key))) || initialState; + finalInitialState = deserializeState(JSON.parse(localStorage.getItem(key))) || initialState; next(reducer, initialState); } catch (e) { console.warn('Could not read debug session from localStorage:', e);