Created
March 2, 2017 17:05
-
-
Save yantakus/14fe0d07167039e2ee8b6d0f9bfe3b9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
/** | |
* Create the store with asynchronously loaded reducers | |
*/ | |
import { createStore, applyMiddleware, compose } from 'redux' | |
import { Iterable, fromJS } from 'immutable' | |
import { routerMiddleware } from 'react-router-redux' | |
import createSagaMiddleware from 'redux-saga' | |
import createReducer from './reducers' | |
const sagaMiddleware = createSagaMiddleware() | |
export default function configureStore (initialState: Object = {}, history: Object) { | |
// 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) | |
] | |
/* istanbul ignore if */ | |
if (process.env.NODE_ENV === 'development') { | |
const createLogger = require('redux-logger') | |
const stateTransformer = (state) => { | |
if (Iterable.isIterable(state)) return state.toJS() | |
else return state | |
} | |
const logger = createLogger({ | |
stateTransformer | |
}) | |
middlewares.push(logger) | |
} | |
const enhancers = [ | |
applyMiddleware(...middlewares) | |
] | |
// If Redux DevTools Extension is installed use it, otherwise use Redux compose | |
const composeEnhancers = | |
process.env.NODE_ENV !== 'production' && | |
typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ : compose | |
const store = createStore( | |
createReducer(), | |
fromJS(initialState), | |
composeEnhancers(...enhancers) | |
) | |
// Extensions | |
store.runSaga = sagaMiddleware.run | |
store.asyncReducers = {} // Async reducer registry | |
// Make reducers hot reloadable, see http://mxs.is/googmo | |
/* istanbul ignore next */ | |
if (module.hot) { | |
module.hot.accept('./reducers', () => { | |
System.import('./reducers').then((reducerModule) => { | |
const createReducers = reducerModule.default | |
const nextReducers = createReducers(store.asyncReducers) | |
store.replaceReducer(nextReducers) | |
}) | |
}) | |
} | |
return store | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment