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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // @flow
  2. import { SET_ROOM } from '../base/conference';
  3. import {
  4. CONNECTION_ESTABLISHED,
  5. getURLWithoutParams,
  6. SET_LOCATION_URL
  7. } from '../base/connection';
  8. import { MiddlewareRegistry } from '../base/redux';
  9. import { _getRouteToRender } from './functions';
  10. MiddlewareRegistry.register(store => next => action => {
  11. switch (action.type) {
  12. case CONNECTION_ESTABLISHED:
  13. return _connectionEstablished(store, next, action);
  14. case SET_LOCATION_URL:
  15. return _setLocationURL(store, next, action);
  16. case SET_ROOM:
  17. return _setRoom(store, next, action);
  18. }
  19. return next(action);
  20. });
  21. /**
  22. * Notifies the feature app that the action {@link CONNECTION_ESTABLISHED} is
  23. * being dispatched within a specific redux {@code store}.
  24. *
  25. * @param {Store} store - The redux store in which the specified {@code action}
  26. * is being dispatched.
  27. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  28. * specified {@code action} to the specified {@code store}.
  29. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  30. * which is being dispatched in the specified {@code store}.
  31. * @private
  32. * @returns {Object} The new state that is the result of the reduction of the
  33. * specified {@code action}.
  34. */
  35. function _connectionEstablished(store, next, action) {
  36. const result = next(action);
  37. // In the Web app we explicitly do not want to display the hash and
  38. // query/search URL params. Unfortunately, window.location and, more
  39. // importantly, its params are used not only in jitsi-meet but also in
  40. // lib-jitsi-meet. Consequenlty, the time to remove the params is
  41. // determined by when no one needs them anymore.
  42. const { history, location } = window;
  43. if (history
  44. && location
  45. && history.length
  46. && typeof history.replaceState === 'function') {
  47. const replacement = getURLWithoutParams(location);
  48. if (location !== replacement) {
  49. history.replaceState(
  50. history.state,
  51. (document && document.title) || '',
  52. replacement);
  53. }
  54. }
  55. return result;
  56. }
  57. /**
  58. * Navigates to a route in accord with a specific redux state.
  59. *
  60. * @param {Store} store - The redux store which determines/identifies the route
  61. * to navigate to.
  62. * @private
  63. * @returns {void}
  64. */
  65. function _navigate({ getState }) {
  66. const state = getState();
  67. const { app } = state['features/app'];
  68. const routeToRender = _getRouteToRender(state);
  69. // XXX Web changed _getRouteToRender to return Promsie instead of Route.
  70. // Unfortunately, the commit left mobile to return Route.
  71. let routeToRenderPromise;
  72. if (routeToRender && typeof routeToRender.then === 'function') {
  73. routeToRenderPromise = routeToRender;
  74. }
  75. if (!routeToRenderPromise) {
  76. routeToRenderPromise = Promise.resolve(routeToRender);
  77. }
  78. routeToRenderPromise.then(app._navigate.bind(app));
  79. }
  80. /**
  81. * Notifies the feature app that the action {@link SET_LOCATION_URL} is being
  82. * dispatched within a specific redux {@code store}.
  83. *
  84. * @param {Store} store - The redux store in which the specified {@code action}
  85. * is being dispatched.
  86. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  87. * specified {@code action} to the specified {@code store}.
  88. * @param {Action} action - The redux action, {@code SET_LOCATION_URL}, which is
  89. * being dispatched in the specified {@code store}.
  90. * @private
  91. * @returns {Object} The new state that is the result of the reduction of the
  92. * specified {@code action}.
  93. */
  94. function _setLocationURL({ getState }, next, action) {
  95. return (
  96. getState()['features/app'].app._navigate(undefined)
  97. .then(() => next(action)));
  98. }
  99. /**
  100. * Notifies the feature app that the action {@link SET_ROOM} is being dispatched
  101. * within a specific redux {@code store}.
  102. *
  103. * @param {Store} store - The redux store in which the specified {@code action}
  104. * is being dispatched.
  105. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  106. * specified {@code action} to the specified {@code store}.
  107. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  108. * dispatched in the specified {@code store}.
  109. * @private
  110. * @returns {Object} The new state that is the result of the reduction of the
  111. * specified {@code action}.
  112. */
  113. function _setRoom(store, next, action) {
  114. const result = next(action);
  115. _navigate(store);
  116. return result;
  117. }