|
| 1 | +import _ from 'lodash'; |
1 | 2 | import { createStore as _createStore, applyMiddleware, compose, combineReducers } from 'redux';
|
2 | 3 | import { routerMiddleware } from 'react-router-redux';
|
3 |
| -import { createPersistor } from 'redux-persist'; |
| 4 | +import { createPersistoid, persistCombineReducers, REGISTER } from 'redux-persist'; |
4 | 5 | import clientMiddleware from './middleware/clientMiddleware';
|
5 | 6 | import createReducers from './reducer';
|
6 | 7 |
|
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) { |
8 | 16 | Object.entries(reducers).forEach(([name, reducer]) => {
|
9 | 17 | if (store.asyncReducers[name]) return;
|
10 | 18 | store.asyncReducers[name] = reducer.__esModule ? reducer.default : reducer;
|
11 | 19 | });
|
12 | 20 |
|
13 |
| - store.replaceReducer(combineReducers(createReducers(store.asyncReducers))); |
| 21 | + store.replaceReducer(combine(createReducers(store.asyncReducers), persistConfig)); |
14 | 22 | }
|
15 | 23 |
|
16 | 24 | function getNoopReducers(reducers, data) {
|
@@ -48,20 +56,24 @@ export default function createStore({
|
48 | 56 | const finalCreateStore = compose(...enhancers)(_createStore);
|
49 | 57 | const reducers = createReducers();
|
50 | 58 | const noopReducers = getNoopReducers(reducers, data);
|
51 |
| - const store = finalCreateStore(combineReducers({ ...noopReducers, ...reducers }), data); |
| 59 | + const store = finalCreateStore(combine({ ...noopReducers, ...reducers }, persistConfig), data); |
52 | 60 |
|
53 | 61 | store.asyncReducers = {};
|
54 |
| - store.inject = inject.bind(null, store); |
| 62 | + store.inject = _.partial(inject, store, _, persistConfig); |
55 | 63 |
|
56 | 64 | 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 }); |
59 | 70 | }
|
60 | 71 |
|
61 | 72 | if (__DEVELOPMENT__ && module.hot) {
|
62 | 73 | 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); |
65 | 77 | });
|
66 | 78 | }
|
67 | 79 |
|
|
0 commit comments