Skip to content

Commit 431adfd

Browse files
committed
Merge pull request #101 from conundrumer/master
add actionDeserializer callback to persistState
2 parents 2c0d51a + b4ea5c2 commit 431adfd

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/persistState.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
1-
export default function persistState(sessionId, deserializer = null) {
1+
export default function persistState(sessionId, stateDeserializer = null, actionDeserializer = null) {
22
if (!sessionId) {
33
return next => (...args) => next(...args);
44
}
55

66
function deserializeState(fullState) {
7-
if (!fullState || typeof deserializer !== 'function') {
8-
return fullState;
9-
}
107
return {
118
...fullState,
12-
committedState: deserializer(fullState.committedState),
9+
committedState: stateDeserializer(fullState.committedState),
1310
computedStates: fullState.computedStates.map((computedState) => {
1411
return {
1512
...computedState,
16-
state: deserializer(computedState.state)
13+
state: stateDeserializer(computedState.state)
1714
};
1815
})
1916
};
2017
}
2118

19+
function deserializeActions(fullState) {
20+
return {
21+
...fullState,
22+
stagedActions: fullState.stagedActions.map((action) => {
23+
return actionDeserializer(action);
24+
})
25+
};
26+
}
27+
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+
2242
return next => (reducer, initialState) => {
2343
const key = `redux-dev-session-${sessionId}`;
2444

2545
let finalInitialState;
2646
try {
27-
finalInitialState = deserializeState(JSON.parse(localStorage.getItem(key))) || initialState;
47+
finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState;
2848
next(reducer, initialState);
2949
} catch (e) {
3050
console.warn('Could not read debug session from localStorage:', e);

0 commit comments

Comments
 (0)