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.

middleware.js 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // @flow
  2. import { MiddlewareRegistry } from '../redux';
  3. import { SET_CONFIG } from './actionTypes';
  4. /**
  5. * The middleware of the feature {@code base/config}.
  6. *
  7. * @param {Store} store - The redux store.
  8. * @private
  9. * @returns {Function}
  10. */
  11. MiddlewareRegistry.register(store => next => action => {
  12. switch (action.type) {
  13. case SET_CONFIG:
  14. return _setConfig(store, next, action);
  15. }
  16. return next(action);
  17. });
  18. /**
  19. * Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
  20. * action is being {@code dispatch}ed in a specific redux store.
  21. *
  22. * @param {Store} store - The redux store in which the specified {@code action}
  23. * is being dispatched.
  24. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  25. * specified {@code action} in the specified {@code store}.
  26. * @param {Action} action - The redux action which is being {@code dispatch}ed
  27. * in the specified {@code store}.
  28. * @private
  29. * @returns {*} The return value of {@code next(action)}.
  30. */
  31. function _setConfig({ getState }, next, action) {
  32. // The reducer is doing some alterations to the config passed in the action,
  33. // so make sure it's the final state by waiting for the action to be
  34. // reduced.
  35. const result = next(action);
  36. // FIXME On Web we rely on the global 'config' variable which gets altered
  37. // multiple times, before it makes it to the reducer. At some point it may
  38. // not be the global variable which is being modified anymore due to
  39. // different merge methods being used along the way. The global variable
  40. // must be synchronized with the final state resolved by the reducer.
  41. if (typeof window.config !== 'undefined') {
  42. window.config = getState()['features/base/config'];
  43. }
  44. return result;
  45. }