2016-08-31 17:15:12 +03:00
|
|
|
import { createStore, applyMiddleware, compose } from 'redux';
|
2017-01-09 13:37:15 +02:00
|
|
|
import thunk from 'redux-thunk';
|
2017-07-08 01:06:02 +03:00
|
|
|
import appReducer, { createReducer } from '../reducers';
|
|
|
|
import { hydrateStoreLazy } from '../actions/store';
|
|
|
|
import { hydrateAction } from '../containers/mastodon';
|
2017-01-16 14:27:58 +02:00
|
|
|
import loadingBarMiddleware from '../middleware/loading_bar';
|
2017-01-09 13:37:15 +02:00
|
|
|
import errorsMiddleware from '../middleware/errors';
|
2017-03-13 18:12:30 +02:00
|
|
|
import soundsMiddleware from '../middleware/sounds';
|
2016-08-24 18:56:44 +03:00
|
|
|
|
2017-01-09 13:37:15 +02:00
|
|
|
export default function configureStore() {
|
2017-07-08 01:06:02 +03:00
|
|
|
const store = createStore(appReducer, compose(applyMiddleware(
|
2017-01-17 21:09:03 +02:00
|
|
|
thunk,
|
|
|
|
loadingBarMiddleware({ promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAIL'] }),
|
|
|
|
errorsMiddleware(),
|
2017-03-13 18:12:30 +02:00
|
|
|
soundsMiddleware()
|
2017-01-17 21:09:03 +02:00
|
|
|
), window.devToolsExtension ? window.devToolsExtension() : f => f));
|
2017-07-08 01:06:02 +03:00
|
|
|
|
|
|
|
store.asyncReducers = { };
|
|
|
|
|
|
|
|
return store;
|
2016-09-20 00:25:59 +03:00
|
|
|
};
|
2017-07-08 01:06:02 +03: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));
|
|
|
|
}
|
|
|
|
}
|