Skip to content

Commit 531adc5

Browse files
committed
upgrade redux-persist
1 parent 94734f6 commit 531adc5

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
"redux-auth-wrapper": "^2.0.2",
183183
"redux-form": "^7.1.1",
184184
"redux-logger": "^3.0.6",
185-
"redux-persist": "^4.8.3",
185+
"redux-persist": "^5.3.4",
186186
"serialize-javascript": "^1.3.0",
187187
"serve-favicon": "^2.3.2",
188188
"socket.io": "^2.0.1",

src/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import asyncMatchRoutes from 'utils/asyncMatchRoutes';
2121
import { ReduxAsyncConnect, Provider } from 'components';
2222

2323
const persistConfig = {
24+
key: 'primary',
2425
storage: localForage,
2526
whitelist: ['auth', 'info', 'chat']
2627
};

src/redux/create.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
import _ from 'lodash';
12
import { createStore as _createStore, applyMiddleware, compose, combineReducers } from 'redux';
23
import { routerMiddleware } from 'react-router-redux';
3-
import { createPersistor } from 'redux-persist';
4+
import { createPersistoid, persistCombineReducers, REGISTER } from 'redux-persist';
45
import clientMiddleware from './middleware/clientMiddleware';
56
import createReducers from './reducer';
67

7-
export function inject(store, reducers) {
8+
function combine(reducers, persistConfig) {
9+
if (persistConfig) {
10+
return persistCombineReducers(persistConfig, reducers);
11+
}
12+
return combineReducers(reducers);
13+
}
14+
15+
export function inject(store, reducers, persistConfig) {
816
Object.entries(reducers).forEach(([name, reducer]) => {
917
if (store.asyncReducers[name]) return;
1018
store.asyncReducers[name] = reducer.__esModule ? reducer.default : reducer;
1119
});
1220

13-
store.replaceReducer(combineReducers(createReducers(store.asyncReducers)));
21+
store.replaceReducer(combine(createReducers(store.asyncReducers), persistConfig));
1422
}
1523

1624
function getNoopReducers(reducers, data) {
@@ -48,20 +56,24 @@ export default function createStore({
4856
const finalCreateStore = compose(...enhancers)(_createStore);
4957
const reducers = createReducers();
5058
const noopReducers = getNoopReducers(reducers, data);
51-
const store = finalCreateStore(combineReducers({ ...noopReducers, ...reducers }), data);
59+
const store = finalCreateStore(combine({ ...noopReducers, ...reducers }, persistConfig), data);
5260

5361
store.asyncReducers = {};
54-
store.inject = inject.bind(null, store);
62+
store.inject = _.partial(inject, store, _, persistConfig);
5563

5664
if (persistConfig) {
57-
createPersistor(store, persistConfig);
58-
store.dispatch({ type: 'PERSIST' });
65+
const persistoid = createPersistoid(persistConfig);
66+
store.subscribe(() => {
67+
persistoid.update(store.getState());
68+
});
69+
store.dispatch({ type: REGISTER });
5970
}
6071

6172
if (__DEVELOPMENT__ && module.hot) {
6273
module.hot.accept('./reducer', () => {
63-
const reducer = require('./reducer');
64-
store.replaceReducer(combineReducers((reducer.__esModule ? reducer.default : reducer)(store.asyncReducers)));
74+
let reducer = require('./reducer');
75+
reducer = combine((reducer.__esModule ? reducer.default : reducer)(store.asyncReducers), persistConfig);
76+
store.replaceReducer(reducer);
6577
});
6678
}
6779

0 commit comments

Comments
 (0)