import {
createStore,
applyMiddleware,
combineReducers,
compose,
} from 'redux';
import createSagaMiddleware, { END } from 'redux-saga';
import reducers from './reducers';
import rootSaga from './sagas';
const sagaMiddleware = createSagaMiddleware();
const makeStore = (initialState) => {
// Make exception for redux dev tools
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-undef */
const composeEnhancers = (typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) || compose;
/* eslint-enable */
const store = createStore(
combineReducers(reducers),
initialState,
composeEnhancers(applyMiddleware(sagaMiddleware)),
);
store.runSaga = () => {
// Avoid running twice
if (store.saga) return;
store.saga = sagaMiddleware.run(rootSaga);
};
store.stopSaga = async () => {
// Avoid running twice
if (!store.saga) return;
store.dispatch(END);
await store.saga.done;
store.saga = null;
};
store.execSagaTasks = async (isServer, tasks) => {
// run saga
store.runSaga();
// dispatch saga tasks
tasks(store.dispatch);
// Stop running and wait for the tasks to be done
await store.stopSaga();
// Re-run on client side
if (!isServer) {
store.runSaga();
}
};
// Initial run
store.runSaga();
return store;
};
export default makeStore;
Created
July 29, 2018 09:21
-
-
Save victor36max/6a61f50285df5cc3852fca135f927d55 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment