ironbar-persea/frontend/store.dev.js

34 lines
1.0 KiB
JavaScript
Raw Permalink Normal View History

2019-03-05 23:20:26 -06:00
import {compose, createStore, applyMiddleware} from 'redux';
import {apiMiddleware} from 'redux-api-middleware';
import createSagaMiddleware from 'redux-saga';
import DevTools from './dev-tools';
import {reducer, saga} from './app';
export function create() {
const sagaMiddleware = createSagaMiddleware({
onError: (error, options) => {
console.error(error);
if (options && options.sagaStack) {
console.error(`(above error originates from: ${options.sagaStack})`);
}
}
});
const middleware = applyMiddleware(apiMiddleware, sagaMiddleware);
const enhancer = compose(middleware, DevTools.instrument());
const store = createStore(reducer, enhancer);
let sagaTask = sagaMiddleware.run(saga);
if (module.hot) {
module.hot.accept('./app', () => {
const {reducer: newReducer, saga: newSaga} = require('./app');
store.replaceReducer(newReducer);
sagaTask.cancel();
sagaTask.done.then(() => {
sagaTask = sagaMiddleware.run(newSaga);
});
});
}
return store;
}