Skip to content

Commit b4ea5c2

Browse files
committed
refactor deserialization in persistState
1 parent 3294864 commit b4ea5c2

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/persistState.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ export default function persistState(sessionId, stateDeserializer = null, action
44
}
55

66
function deserializeState(fullState) {
7-
if (!fullState || typeof stateDeserializer !== 'function') {
8-
return fullState;
9-
}
107
return {
118
...fullState,
129
committedState: stateDeserializer(fullState.committedState),
@@ -20,9 +17,6 @@ export default function persistState(sessionId, stateDeserializer = null, action
2017
}
2118

2219
function deserializeActions(fullState) {
23-
if (!fullState || typeof actionDeserializer !== 'function') {
24-
return fullState;
25-
}
2620
return {
2721
...fullState,
2822
stagedActions: fullState.stagedActions.map((action) => {
@@ -31,12 +25,26 @@ export default function persistState(sessionId, stateDeserializer = null, action
3125
};
3226
}
3327

28+
function deserialize(fullState) {
29+
if (!fullState) {
30+
return fullState;
31+
}
32+
let deserializedState = fullState;
33+
if (typeof stateDeserializer === 'function') {
34+
deserializedState = deserializeState(deserializedState);
35+
}
36+
if (typeof actionDeserializer === 'function') {
37+
deserializedState = deserializeActions(deserializedState);
38+
}
39+
return deserializedState;
40+
}
41+
3442
return next => (reducer, initialState) => {
3543
const key = `redux-dev-session-${sessionId}`;
3644

3745
let finalInitialState;
3846
try {
39-
finalInitialState = deserializeActions(deserializeState(JSON.parse(localStorage.getItem(key)))) || initialState;
47+
finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState;
4048
next(reducer, initialState);
4149
} catch (e) {
4250
console.warn('Could not read debug session from localStorage:', e);

0 commit comments

Comments
 (0)