mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2024-11-10 19:56:54 +03:00
using deserializer callback for persistState
This commit is contained in:
parent
e76bb6956b
commit
6ee3b4373b
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user