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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // @flow
  2. import { APP_WILL_MOUNT } from '../app';
  3. import { CONFERENCE_WILL_LEAVE, SET_ROOM } from '../base/conference';
  4. import { addKnownDomains } from '../base/known-domains';
  5. import { MiddlewareRegistry } from '../base/redux';
  6. import { parseURIString } from '../base/util';
  7. import { _storeCurrentConference, _updateConferenceDuration } from './actions';
  8. /**
  9. * Middleware that captures joined rooms so they can be saved into
  10. * {@code window.localStorage}.
  11. *
  12. * @param {Store} store - The redux store.
  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 CONFERENCE_WILL_LEAVE:
  20. return _conferenceWillLeave(store, next, action);
  21. case SET_ROOM:
  22. return _setRoom(store, next, action);
  23. }
  24. return next(action);
  25. });
  26. /**
  27. * Notifies the feature recent-list that the redux action {@link APP_WILL_MOUNT}
  28. * is being dispatched in a specific redux store.
  29. *
  30. * @param {Store} store - The redux store in which the specified action is being
  31. * dispatched.
  32. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  33. * specified action to the specified store.
  34. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  35. * being dispatched in the specified redux store.
  36. * @private
  37. * @returns {*} The result returned by {@code next(action)}.
  38. */
  39. function _appWillMount({ dispatch, getState }, next, action) {
  40. const result = next(action);
  41. // It's an opportune time to transfer the feature recent-list's knowledge
  42. // about "known domains" (which is local to the feature) to the feature
  43. // base/known-domains (which is global to the app).
  44. //
  45. // XXX Since the feature recent-list predates the feature calendar-sync and,
  46. // consequently, the feature known-domains, it's possible for the feature
  47. // known-list to know of domains which the feature known-domains is yet to
  48. // discover.
  49. const knownDomains = [];
  50. for (const { conference } of getState()['features/recent-list']) {
  51. const uri = parseURIString(conference);
  52. let host;
  53. uri && (host = uri.host) && knownDomains.push(host);
  54. }
  55. knownDomains.length && dispatch(addKnownDomains(knownDomains));
  56. return result;
  57. }
  58. /**
  59. * Updates the duration of the last conference stored in the list.
  60. *
  61. * @param {Store} store - The redux store.
  62. * @param {Dispatch} next - The redux {@code dispatch} function.
  63. * @param {Action} action - The redux action {@link CONFERENCE_WILL_LEAVE}.
  64. * @private
  65. * @returns {*} The result returned by {@code next(action)}.
  66. */
  67. function _conferenceWillLeave({ dispatch, getState }, next, action) {
  68. dispatch(
  69. _updateConferenceDuration(
  70. getState()['features/base/connection'].locationURL));
  71. return next(action);
  72. }
  73. /**
  74. * Checks if there is a current conference (upon SET_ROOM action), and saves it
  75. * if necessary.
  76. *
  77. * @param {Store} store - The redux store.
  78. * @param {Dispatch} next - The redux {@code dispatch} function.
  79. * @param {Action} action - The redux action {@link SET_ROOM}.
  80. * @private
  81. * @returns {*} The result returned by {@code next(action)}.
  82. */
  83. function _setRoom({ dispatch, getState }, next, action) {
  84. if (action.room) {
  85. const { locationURL } = getState()['features/base/connection'];
  86. if (locationURL) {
  87. dispatch(_storeCurrentConference(locationURL));
  88. // Whatever domain the feature recent-list knows about, the app as a
  89. // whole should know about.
  90. //
  91. // XXX Technically, _storeCurrentConference could be turned into an
  92. // asynchronous action creator which dispatches both
  93. // _STORE_CURRENT_CONFERENCE and addKnownDomains but...
  94. dispatch(addKnownDomains(locationURL.host));
  95. }
  96. }
  97. return next(action);
  98. }