Skip to content

Instantly share code, notes, and snippets.

@akd3vs
Created March 7, 2019 00:16
Show Gist options
  • Save akd3vs/2fe1b9c004069d8b0030402a344975fa to your computer and use it in GitHub Desktop.
Save akd3vs/2fe1b9c004069d8b0030402a344975fa to your computer and use it in GitHub Desktop.
store.js
import { combineReducers } from 'redux-immutable';
import rootReducers from 'store/rootReducers';
const rootReducer = { rootReducers };
export const createReducer = asyncReducers => combineReducers({ ...rootReducer, ...asyncReducers });
export default combineReducers(rootReducer);
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)]);
}
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