52 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-08-18 11:41:13 +02:00
/**
* Create the store with asynchronously loaded reducers
*/
import { createStore, applyMiddleware, compose } from 'redux';
import { fromJS } from 'immutable';
import { routerMiddleware } from 'react-router-redux';
import createSagaMiddleware from 'redux-saga';
import createReducer from './reducers';
const sagaMiddleware = createSagaMiddleware();
const devtools = window.devToolsExtension || (() => noop => noop);
export default function configureStore(initialState = {}, history) {
// Create the store with two middlewares
// 1. sagaMiddleware: Makes redux-sagas work
// 2. routerMiddleware: Syncs the location/URL path to the state
const middlewares = [
sagaMiddleware,
routerMiddleware(history),
];
const enhancers = [
applyMiddleware(...middlewares),
devtools(),
];
const store = createStore(
createReducer(),
fromJS(initialState),
compose(...enhancers)
);
2016-08-18 11:47:26 +02:00
// Create hook for async sagas
2016-08-18 11:41:13 +02:00
store.runSaga = sagaMiddleware.run;
// Make reducers hot reloadable, see http://mxs.is/googmo
/* istanbul ignore next */
if (module.hot) {
2016-08-18 11:47:26 +02:00
System.import('./reducers').then((reducerModule) => {
const createReducers = reducerModule.default;
const nextReducers = createReducers(store.asyncReducers);
2016-08-18 11:41:13 +02:00
2016-08-18 11:47:26 +02:00
store.replaceReducer(nextReducers);
2016-08-18 11:41:13 +02:00
});
}
2016-08-18 11:47:26 +02:00
// Initialize it with no other reducers
store.asyncReducers = {};
2016-08-18 11:41:13 +02:00
return store;
}