about summary refs log tree commit diff
path: root/app/javascript/mastodon/store/configureStore.js
blob: 0fe29f031a282f1a9c1da4a039da30c6d1da8e39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import appReducer, { createReducer } from '../reducers';
import { hydrateStoreLazy } from '../actions/store';
import { hydrateAction } from '../containers/mastodon';
import loadingBarMiddleware from '../middleware/loading_bar';
import errorsMiddleware from '../middleware/errors';
import soundsMiddleware from '../middleware/sounds';

export default function configureStore() {
  const store = createStore(appReducer, compose(applyMiddleware(
    thunk,
    loadingBarMiddleware({ promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAIL'] }),
    errorsMiddleware(),
    soundsMiddleware()
  ), window.devToolsExtension ? window.devToolsExtension() : f => f));

  store.asyncReducers = { };

  return store;
};

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));
  }
}