-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
persist.js
66 lines (59 loc) · 1.78 KB
/
persist.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* External dependencies
*/
import { get } from 'lodash';
/**
* Adds the rehydratation behavior to redux reducers
*
* @param {Function} reducer The reducer to enhance
* @param {String} reducerKey The reducer key to persist
*
* @return {Function} Enhanced reducer
*/
export function withRehydratation( reducer, reducerKey ) {
// EnhancedReducer with auto-rehydration
const enhancedReducer = ( state, action ) => {
const nextState = reducer( state, action );
if ( action.type === 'REDUX_REHYDRATE' ) {
return {
...nextState,
[ reducerKey ]: action.payload,
};
}
return nextState;
};
return enhancedReducer;
}
/**
* Loads the initial state and persist on changes
*
* This should be executed after the reducer's registration
*
* @param {Object} store Store to enhance
* @param {String} reducerKey The reducer key to persist (example: reducerKey.subReducerKey)
* @param {String} storageKey The storage key to use
* @param {Object} defaults Default values of the reducer key
*/
export function loadAndPersist( store, reducerKey, storageKey, defaults = {} ) {
// Load initially persisted value
const persistedString = window.localStorage.getItem( storageKey );
if ( persistedString ) {
const persistedState = {
...defaults,
...JSON.parse( persistedString ),
};
store.dispatch( {
type: 'REDUX_REHYDRATE',
payload: persistedState,
} );
}
// Persist updated preferences
let currentStateValue = get( store.getState(), reducerKey );
store.subscribe( () => {
const newStateValue = get( store.getState(), reducerKey );
if ( newStateValue !== currentStateValue ) {
currentStateValue = newStateValue;
window.localStorage.setItem( storageKey, JSON.stringify( currentStateValue ) );
}
} );
}