|
1 | | -export default function persistState(sessionId, deserializer = null) { |
| 1 | +export default function persistState(sessionId, stateDeserializer = null, actionDeserializer = null) { |
2 | 2 | if (!sessionId) { |
3 | 3 | return next => (...args) => next(...args); |
4 | 4 | } |
5 | 5 |
|
6 | 6 | function deserializeState(fullState) { |
7 | | - if (!fullState || typeof deserializer !== 'function') { |
8 | | - return fullState; |
9 | | - } |
10 | 7 | return { |
11 | 8 | ...fullState, |
12 | | - committedState: deserializer(fullState.committedState), |
| 9 | + committedState: stateDeserializer(fullState.committedState), |
13 | 10 | computedStates: fullState.computedStates.map((computedState) => { |
14 | 11 | return { |
15 | 12 | ...computedState, |
16 | | - state: deserializer(computedState.state) |
| 13 | + state: stateDeserializer(computedState.state) |
17 | 14 | }; |
18 | 15 | }) |
19 | 16 | }; |
20 | 17 | } |
21 | 18 |
|
| 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 | + |
22 | 42 | return next => (reducer, initialState) => { |
23 | 43 | const key = `redux-dev-session-${sessionId}`; |
24 | 44 |
|
25 | 45 | let finalInitialState; |
26 | 46 | try { |
27 | | - finalInitialState = deserializeState(JSON.parse(localStorage.getItem(key))) || initialState; |
| 47 | + finalInitialState = deserialize(JSON.parse(localStorage.getItem(key))) || initialState; |
28 | 48 | next(reducer, initialState); |
29 | 49 | } catch (e) { |
30 | 50 | console.warn('Could not read debug session from localStorage:', e); |
|
0 commit comments