-
Notifications
You must be signed in to change notification settings - Fork 1k
/
importState.js
52 lines (48 loc) · 1.94 KB
/
importState.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import mapValues from 'lodash/mapValues';
import jsan from 'jsan';
import seralizeImmutable from 'remotedev-serialize/immutable/serialize';
function deprecate(param) {
console.warn(`\`${param}\` parameter for Redux DevTools Extension is deprecated. Use \`serialize\` parameter instead: https://github.com/zalmoxisus/redux-devtools-extension/releases/tag/v2.12.1`); // eslint-disable-line
}
export default function importState(state, { deserializeState, deserializeAction, serialize }) {
if (!state) return undefined;
let parse = jsan.parse;
if (serialize) {
if (serialize.immutable) {
parse = v => jsan.parse(v, seralizeImmutable(
serialize.immutable, serialize.refs, serialize.replacer, serialize.reviver
).reviver);
} else if (serialize.reviver) {
parse = v => jsan.parse(v, serialize.reviver);
}
}
let preloadedState;
let nextLiftedState = parse(state);
if (nextLiftedState.payload) {
if (nextLiftedState.preloadedState) preloadedState = parse(nextLiftedState.preloadedState);
nextLiftedState = parse(nextLiftedState.payload);
}
if (deserializeState) {
deprecate('deserializeState');
if (typeof nextLiftedState.computedStates !== 'undefined') {
nextLiftedState.computedStates = nextLiftedState.computedStates.map(computedState => ({
...computedState,
state: deserializeState(computedState.state)
}));
}
if (typeof nextLiftedState.committedState !== 'undefined') {
nextLiftedState.committedState = deserializeState(nextLiftedState.committedState);
}
if (typeof preloadedState !== 'undefined') {
preloadedState = deserializeState(preloadedState);
}
}
if (deserializeAction) {
deprecate('deserializeAction');
nextLiftedState.actionsById = mapValues(nextLiftedState.actionsById, liftedAction => ({
...liftedAction,
action: deserializeAction(liftedAction.action)
}));
}
return { nextLiftedState, preloadedState };
}