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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // @flow
  2. import { APP_WILL_MOUNT } from '../app';
  3. import { addKnownDomains } from '../known-domains';
  4. import { MiddlewareRegistry } from '../redux';
  5. import { parseURIString } from '../util';
  6. import { SET_CONFIG } from './actionTypes';
  7. import { _CONFIG_STORE_PREFIX } from './constants';
  8. /**
  9. * The middleware of the feature {@code base/config}.
  10. *
  11. * @param {Store} store - The redux store.
  12. * @private
  13. * @returns {Function}
  14. */
  15. MiddlewareRegistry.register(store => next => action => {
  16. switch (action.type) {
  17. case APP_WILL_MOUNT:
  18. return _appWillMount(store, next, action);
  19. case SET_CONFIG:
  20. return _setConfig(store, next, action);
  21. }
  22. return next(action);
  23. });
  24. /**
  25. * Notifies the feature {@code base/config} that the {@link APP_WILL_MOUNT}
  26. * redux action is being {@code dispatch}ed in a specific redux store.
  27. *
  28. * @param {Store} store - The redux store in which the specified {@code action}
  29. * is being dispatched.
  30. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  31. * specified {@code action} in the specified {@code store}.
  32. * @param {Action} action - The redux action which is being {@code dispatch}ed
  33. * in the specified {@code store}.
  34. * @private
  35. * @returns {*} The return value of {@code next(action)}.
  36. */
  37. function _appWillMount(store, next, action) {
  38. const result = next(action);
  39. // It's an opportune time to transfer the feature base/config's knowledge
  40. // about "known domains" (which is local to the feature) to the feature
  41. // base/known-domains (which is global to the app).
  42. //
  43. // XXX Since the feature base/config predates the feature calendar-sync and,
  44. // consequently, the feature known-domains, it's possible for the feature
  45. // base/config to know of domains which the feature known-domains is yet to
  46. // discover.
  47. const { localStorage } = window;
  48. if (localStorage) {
  49. const prefix = `${_CONFIG_STORE_PREFIX}/`;
  50. const knownDomains = [];
  51. for (let i = 0; /* localStorage.key(i) */; ++i) {
  52. const key = localStorage.key(i);
  53. if (key) {
  54. let baseURL;
  55. if (key.startsWith(prefix)
  56. && (baseURL = key.substring(prefix.length))) {
  57. const uri = parseURIString(baseURL);
  58. let host;
  59. uri && (host = uri.host) && knownDomains.push(host);
  60. }
  61. } else {
  62. break;
  63. }
  64. }
  65. knownDomains.length && store.dispatch(addKnownDomains(knownDomains));
  66. }
  67. return result;
  68. }
  69. /**
  70. * Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
  71. * action is being {@code dispatch}ed in a specific redux store.
  72. *
  73. * @param {Store} store - The redux store in which the specified {@code action}
  74. * is being dispatched.
  75. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  76. * specified {@code action} in the specified {@code store}.
  77. * @param {Action} action - The redux action which is being {@code dispatch}ed
  78. * in the specified {@code store}.
  79. * @private
  80. * @returns {*} The return value of {@code next(action)}.
  81. */
  82. function _setConfig({ getState }, next, action) {
  83. // The reducer is doing some alterations to the config passed in the action,
  84. // so make sure it's the final state by waiting for the action to be
  85. // reduced.
  86. const result = next(action);
  87. // FIXME On Web we rely on the global 'config' variable which gets altered
  88. // multiple times, before it makes it to the reducer. At some point it may
  89. // not be the global variable which is being modified anymore due to
  90. // different merge methods being used along the way. The global variable
  91. // must be synchronized with the final state resolved by the reducer.
  92. if (typeof window.config !== 'undefined') {
  93. window.config = getState()['features/base/config'];
  94. }
  95. return result;
  96. }