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