2016-08-31 15:15:12 +01:00
|
|
|
import { createStore, applyMiddleware, compose } from 'redux';
|
2017-01-09 11:37:15 +00:00
|
|
|
import thunk from 'redux-thunk';
|
2017-07-07 23:06:02 +01:00
|
|
|
import appReducer, { createReducer } from '../reducers';
|
|
|
|
import { hydrateStoreLazy } from '../actions/store';
|
|
|
|
import { hydrateAction } from '../containers/mastodon';
|
2017-01-16 12:27:58 +00:00
|
|
|
import loadingBarMiddleware from '../middleware/loading_bar';
|
2017-01-09 11:37:15 +00:00
|
|
|
import errorsMiddleware from '../middleware/errors';
|
2017-03-13 16:12:30 +00:00
|
|
|
import soundsMiddleware from '../middleware/sounds';
|
2016-08-24 16:56:44 +01:00
|
|
|
|
2017-01-09 11:37:15 +00:00
|
|
|
export default function configureStore() {
|
2017-07-07 23:06:02 +01:00
|
|
|
const store = createStore(appReducer, compose(applyMiddleware(
|
2017-01-17 19:09:03 +00:00
|
|
|
thunk,
|
|
|
|
loadingBarMiddleware({ promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAIL'] }),
|
|
|
|
errorsMiddleware(),
|
2017-03-13 16:12:30 +00:00
|
|
|
soundsMiddleware()
|
2017-01-17 19:09:03 +00:00
|
|
|
), window.devToolsExtension ? window.devToolsExtension() : f => f));
|
2017-07-07 23:06:02 +01:00
|
|
|
|
|
|
|
store.asyncReducers = { };
|
|
|
|
|
|
|
|
return store;
|
2016-09-19 22:25:59 +01:00
|
|
|
};
|
2017-07-07 23:06:02 +01:00
|
|
|
|
|
|
|
export function injectAsyncReducer(store, name, asyncReducer) {
|
|
|
|
if (!store.asyncReducers[name]) {
|
|
|
|
// Keep track that we injected this reducer
|
|
|
|
store.asyncReducers[name] = asyncReducer;
|
|
|
|
|
|
|
|
// Add the current reducer to the store
|
|
|
|
store.replaceReducer(createReducer(store.asyncReducers));
|
|
|
|
|
|
|
|
// The state this reducer handles defaults to its initial state (stored inside the reducer)
|
|
|
|
// But that state may be out of date because of the server-side hydration, so we replay
|
|
|
|
// the hydration action but only for this reducer (all async reducers must listen for this dynamic action)
|
|
|
|
store.dispatch(hydrateStoreLazy(name, hydrateAction.state));
|
|
|
|
}
|
|
|
|
}
|