-
Notifications
You must be signed in to change notification settings - Fork 866
/
autoMergeLevel1.js
50 lines (45 loc) · 1.28 KB
/
autoMergeLevel1.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
// @flow
/*
autoMergeLevel1:
- merges 1 level of substate
- skips substate if already modified
*/
import type { PersistConfig } from '../types'
export default function autoMergeLevel1<State: Object>(
inboundState: State,
originalState: State,
reducedState: State,
{ debug }: PersistConfig
): State {
let newState = { ...reducedState }
// only rehydrate if inboundState exists and is an object
if (inboundState && typeof inboundState === 'object') {
Object.keys(inboundState).forEach(key => {
// ignore _persist data
if (key === '_persist') return
// if reducer modifies substate, skip auto rehydration
if (originalState[key] !== reducedState[key]) {
if (process.env.NODE_ENV !== 'production' && debug)
console.log(
'redux-persist/stateReconciler: sub state for key `%s` modified, skipping.',
key
)
return
}
// otherwise hard set the new value
newState[key] = inboundState[key]
})
}
if (
process.env.NODE_ENV !== 'production' &&
debug &&
inboundState &&
typeof inboundState === 'object'
)
console.log(
`redux-persist/stateReconciler: rehydrated keys '${Object.keys(
inboundState
).join(', ')}'`
)
return newState
}