Created
May 11, 2016 16:56
-
-
Save hoschi/6538249ad079116840825e20c48f1690 to your computer and use it in GitHub Desktop.
Hot reloadable redux-saga ... sagas
This file contains hidden or 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 { applyMiddleware, createStore } from 'redux'; | |
import createSagaMiddleware from 'redux-saga'; | |
import rootReducer from './rootReducer'; | |
import SagaManager from './SagaManager'; | |
export default function configureStore (initialState = {}) { | |
// create saga middleware | |
const sagaMiddleware = createSagaMiddleware(); | |
// create middleware function, which contains all normal | |
let middleware = applyMiddleware(sagaMiddleware); | |
// Create final store and subscribe router in debug env ie. for devtools | |
const store = middleware(createStore)(rootReducer, initialState); | |
// run sagas | |
SagaManager.startSagas(sagaMiddleware); | |
// enable hot module reloading for reducers | |
if (module.hot) { | |
module.hot.accept('./rootReducer', () => { | |
store.replaceReducer(require('./rootReducer').default); | |
}); | |
module.hot.accept('./SagaManager', () => { | |
SagaManager.cancelSagas(store); | |
require('./SagaManager').default.startSagas(sagaMiddleware); | |
}); | |
} | |
return store; | |
} |
This file contains hidden or 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 mySaga from 'mySaga'; | |
import { take, fork, cancel } from 'redux-saga/effects'; | |
const sagas = [mySaga]; | |
export const CANCEL_SAGAS_HMR = 'CANCEL_SAGAS_HMR'; | |
function createAbortableSaga (saga) { | |
if (process.env.NODE_ENV === 'development') { | |
return function* main () { | |
const sagaTask = yield fork(saga); | |
yield take(CANCEL_SAGAS_HMR); | |
yield cancel(sagaTask); | |
}; | |
} else { | |
return saga; | |
} | |
} | |
const SagaManager = { | |
startSagas(sagaMiddleware) { | |
sagas.map(createAbortableSaga).forEach((saga) => sagaMiddleware.run(saga)); | |
}, | |
cancelSagas(store) { | |
store.dispatch({ | |
type: CANCEL_SAGAS_HMR | |
}); | |
} | |
}; | |
export default SagaManager; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See this issue for more context and potential problems.