Created
March 7, 2019 00:16
-
-
Save akd3vs/2fe1b9c004069d8b0030402a344975fa to your computer and use it in GitHub Desktop.
store.js
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
import { combineReducers } from 'redux-immutable'; | |
import rootReducers from 'store/rootReducers'; | |
const rootReducer = { rootReducers }; | |
export const createReducer = asyncReducers => combineReducers({ ...rootReducer, ...asyncReducers }); | |
export default combineReducers(rootReducer); |
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
import { all } from 'redux-saga/effects'; | |
import rootSagas from 'store/rootSagas'; | |
const run = sagas => sagas.map(saga => saga()); | |
export default function* rootSaga() { | |
yield all([...run(rootSagas)]); | |
} |
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
import { createStore, compose, applyMiddleware } from 'redux'; | |
import Immutable from 'immutable'; | |
import createSagaMiddleware from 'redux-saga'; | |
import rootReducer, { createReducer } from 'rootReducer'; | |
import rootSaga from 'rootSaga'; | |
const sagaMiddleware = createSagaMiddleware(); | |
const configureStore = initialState => { | |
const store = createStore( | |
rootReducer, | |
initialState, | |
compose( | |
applyMiddleware(sagaMiddleware), | |
process.env.NODE_ENV === 'development' && window.__REDUX_DEVTOOLS_EXTENSION__ | |
? window.__REDUX_DEVTOOLS_EXTENSION__() | |
: f => f | |
) | |
); | |
sagaMiddleware.run(rootSaga); | |
if (process.env.NODE_ENV === 'development') { | |
if (module.hot) { | |
module.hot.accept('./rootReducer', () => { | |
store.replaceReducer(createReducer(store.asyncReducers)); | |
}); | |
} | |
} | |
store.runSaga = sagaMiddleware.run; | |
store.asyncReducers = store.asyncReducers || {}; | |
store.asyncSagas = store.asyncSagas || []; | |
return store; | |
}; | |
export const injectAsyncReducer = ({ name, asyncReducer, store }) => { | |
store.asyncReducers[name] = asyncReducer; | |
store.replaceReducer(createReducer(store.asyncReducers)); | |
}; | |
export function injectAsyncSagas({ name, sagas, store }) { | |
if (!store.asyncSagas.includes(name)) { | |
sagas.forEach(store.runSaga); | |
store.asyncSagas.push(name); | |
} | |
} | |
const initialState = Immutable.Map(); | |
export default configureStore(initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment