mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2024-11-22 01:26:48 +03:00
add actionDeserializer callback to persistState
This commit is contained in:
parent
1d41c808a1
commit
3294864b7d
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue
Block a user