You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.4 KiB

  1. import { createStore, applyMiddleware, compose } from 'redux';
  2. import thunk from 'redux-thunk';
  3. import appReducer, { createReducer } from '../reducers';
  4. import { hydrateStoreLazy } from '../actions/store';
  5. import { hydrateAction } from '../containers/mastodon';
  6. import loadingBarMiddleware from '../middleware/loading_bar';
  7. import errorsMiddleware from '../middleware/errors';
  8. import soundsMiddleware from '../middleware/sounds';
  9. export default function configureStore() {
  10. const store = createStore(appReducer, compose(applyMiddleware(
  11. thunk,
  12. loadingBarMiddleware({ promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAIL'] }),
  13. errorsMiddleware(),
  14. soundsMiddleware()
  15. ), window.devToolsExtension ? window.devToolsExtension() : f => f));
  16. store.asyncReducers = { };
  17. return store;
  18. };
  19. export function injectAsyncReducer(store, name, asyncReducer) {
  20. if (!store.asyncReducers[name]) {
  21. // Keep track that we injected this reducer
  22. store.asyncReducers[name] = asyncReducer;
  23. // Add the current reducer to the store
  24. store.replaceReducer(createReducer(store.asyncReducers));
  25. // The state this reducer handles defaults to its initial state (stored inside the reducer)
  26. // But that state may be out of date because of the server-side hydration, so we replay
  27. // the hydration action but only for this reducer (all async reducers must listen for this dynamic action)
  28. store.dispatch(hydrateStoreLazy(name, hydrateAction.state));
  29. }
  30. }