From e76bb6956bce791c1eee2466694a68112adbd9c7 Mon Sep 17 00:00:00 2001 From: FredyC Date: Wed, 19 Aug 2015 21:58:25 +0200 Subject: [PATCH 1/3] custom parsers for persistState --- src/persistState.js | 48 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/persistState.js b/src/persistState.js index 09a6d598..ecab0a54 100644 --- a/src/persistState.js +++ b/src/persistState.js @@ -1,14 +1,58 @@ -export default function persistState(sessionId) { +export default function persistState(sessionId, parsers) { 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) { + return fullState; + } + return { + ...fullState, + committedState: deserializeState(fullState.committedState), + computedStates: fullState.computedStates.map((computedState) => { + return { + ...computedState, + state: deserializeState(computedState.state) + }; + }) + }; + } + return next => (reducer, initialState) => { const key = `redux-dev-session-${sessionId}`; let finalInitialState; try { - finalInitialState = JSON.parse(localStorage.getItem(key)) || initialState; + finalInitialState = parseState(JSON.parse(localStorage.getItem(key))) || initialState; next(reducer, initialState); } catch (e) { console.warn('Could not read debug session from localStorage:', e); From 6ee3b4373b7b2e1b4ad041ef9e86d4144abaa1f3 Mon Sep 17 00:00:00 2001 From: FredyC Date: Sun, 23 Aug 2015 13:43:26 +0200 Subject: [PATCH 2/3] 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); From 0a1a49c4d8deaf9c0516168d87d4fe7be68e52a4 Mon Sep 17 00:00:00 2001 From: Johnny Everson Date: Tue, 1 Sep 2015 13:20:58 -0300 Subject: [PATCH 3/3] Improve instructions on how to run example Note: taken as it is from the root Readme --- examples/counter/README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/counter/README.md b/examples/counter/README.md index 524cb4c1..3edaeb51 100644 --- a/examples/counter/README.md +++ b/examples/counter/README.md @@ -1,6 +1,14 @@ # Redux DevTools Counter example -## Getting Started +## Running example -1. Install dependencies: `npm i` -2. Start the development server: `npm start` +``` +git clone https://github.com/gaearon/redux-devtools.git +cd redux-devtools +npm install + +cd examples/counter +npm install +npm start +open http://localhost:3000 +```