configureStore.js 1.65 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1 2
import { routerMiddleware } from 'connected-react-router/immutable';
import { Iterable } from 'immutable';
张烨's avatar
张烨 committed
3
import { applyMiddleware, compose, createStore } from 'redux';
dengxiaofeng's avatar
dengxiaofeng committed
4
import { createLogger } from 'redux-logger';
Julien Benchetrit's avatar
Julien Benchetrit committed
5
import createSagaMiddleware from 'redux-saga';
dengxiaofeng's avatar
dengxiaofeng committed
6
import thunk from 'redux-thunk';
dengxiaofeng's avatar
dengxiaofeng committed
7

Julien Benchetrit's avatar
Julien Benchetrit committed
8 9
import createReducer from './reducers';

dengxiaofeng's avatar
dengxiaofeng committed
10 11 12 13 14 15
const logger = createLogger({
  stateTransformer: state => {
    if (Iterable.isIterable(state)) return state.toJS();
    return state;
  },
});
Julien Benchetrit's avatar
Julien Benchetrit committed
16 17
export default function configureStore(initialState = {}, history) {
  let composeEnhancers = compose;
dengxiaofeng's avatar
dengxiaofeng committed
18 19
  let reduxSagaMonitorOptions = {};
  /* istanbul ignore next */
Julien Benchetrit's avatar
Julien Benchetrit committed
20 21 22 23
  if (process.env.NODE_ENV !== 'production' && typeof window === 'object') {
    /* eslint-disable no-underscore-dangle */
    if (window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__)
      composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({});
dengxiaofeng's avatar
dengxiaofeng committed
24 25 26 27 28 29

    if (window.__SAGA_MONITOR_EXTENSION__)
      reduxSagaMonitorOptions = {
        sagaMonitor: window.__SAGA_MONITOR_EXTENSION__,
      };
    /* eslint-enable */
Julien Benchetrit's avatar
Julien Benchetrit committed
30 31 32 33
  }

  const sagaMiddleware = createSagaMiddleware(reduxSagaMonitorOptions);

邓超's avatar
邓超 committed
34
  const middlewares = [sagaMiddleware, routerMiddleware(history), logger, thunk];
Julien Benchetrit's avatar
Julien Benchetrit committed
35 36 37

  const enhancers = [applyMiddleware(...middlewares)];

邓超's avatar
邓超 committed
38
  const store = createStore(createReducer(), initialState, composeEnhancers(...enhancers));
Julien Benchetrit's avatar
Julien Benchetrit committed
39

dengxiaofeng's avatar
dengxiaofeng committed
40
  // Extensions
Julien Benchetrit's avatar
Julien Benchetrit committed
41 42 43 44 45 46 47 48 49 50 51
  store.runSaga = sagaMiddleware.run;
  store.injectedReducers = {}; // Reducer registry
  store.injectedSagas = {}; // Saga registry
  if (module.hot) {
    module.hot.accept('./reducers', () => {
      store.replaceReducer(createReducer(store.injectedReducers));
    });
  }

  return store;
}