Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { AnyAction } from 'redux';
  2. import { createConnectionEvent } from '../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../analytics/functions';
  4. import { SET_ROOM } from '../base/conference/actionTypes';
  5. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection/actionTypes';
  6. import { getURLWithoutParams } from '../base/connection/utils';
  7. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  8. import { inIframe } from '../base/util/iframeUtils';
  9. import { reloadNow } from './actions';
  10. import { _getRouteToRender } from './getRouteToRender';
  11. import { IStore } from './types';
  12. MiddlewareRegistry.register(store => next => action => {
  13. switch (action.type) {
  14. case CONNECTION_ESTABLISHED:
  15. return _connectionEstablished(store, next, action);
  16. case CONNECTION_FAILED:
  17. return _connectionFailed(store, next, action);
  18. case SET_ROOM:
  19. return _setRoom(store, next, action);
  20. }
  21. return next(action);
  22. });
  23. /**
  24. * Notifies the feature app that the action {@link CONNECTION_ESTABLISHED} is
  25. * being dispatched within a specific redux {@code store}.
  26. *
  27. * @param {Store} store - The redux store in which the specified {@code action}
  28. * is being dispatched.
  29. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  30. * specified {@code action} to the specified {@code store}.
  31. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  32. * which is being dispatched in the specified {@code store}.
  33. * @private
  34. * @returns {Object} The new state that is the result of the reduction of the
  35. * specified {@code action}.
  36. */
  37. function _connectionEstablished(store: IStore, next: Function, action: AnyAction) {
  38. const result = next(action);
  39. // In the Web app we explicitly do not want to display the hash and
  40. // query/search URL params. Unfortunately, window.location and, more
  41. // importantly, its params are used not only in jitsi-meet but also in
  42. // lib-jitsi-meet. Consequently, the time to remove the params is
  43. // determined by when no one needs them anymore.
  44. // @ts-ignore
  45. const { history, location } = window;
  46. if (inIframe()) {
  47. return;
  48. }
  49. if (history
  50. && location
  51. && history.length
  52. && typeof history.replaceState === 'function') {
  53. // @ts-ignore
  54. const replacement = getURLWithoutParams(location);
  55. // @ts-ignore
  56. if (location !== replacement) {
  57. history.replaceState(
  58. history.state,
  59. document?.title || '',
  60. replacement);
  61. }
  62. }
  63. return result;
  64. }
  65. /**
  66. * CONNECTION_FAILED action side effects.
  67. *
  68. * @param {Object} store - The Redux store.
  69. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the specified {@code action} to
  70. * the specified {@code store}.
  71. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is being dispatched in the specified
  72. * {@code store}.
  73. * @returns {Object}
  74. * @private
  75. */
  76. function _connectionFailed({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  77. // In the case of a split-brain error, reload early and prevent further
  78. // handling of the action.
  79. if (_isMaybeSplitBrainError(getState, action)) {
  80. dispatch(reloadNow());
  81. return;
  82. }
  83. return next(action);
  84. }
  85. /**
  86. * Returns whether or not a CONNECTION_FAILED action is for a possible split brain error. A split brain error occurs
  87. * when at least two users join a conference on different bridges. It is assumed the split brain scenario occurs very
  88. * early on in the call.
  89. *
  90. * @param {Function} getState - The redux function for fetching the current state.
  91. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is being dispatched in the specified
  92. * {@code store}.
  93. * @private
  94. * @returns {boolean}
  95. */
  96. function _isMaybeSplitBrainError(getState: IStore['getState'], action: AnyAction) {
  97. const { error } = action;
  98. const isShardChangedError = error
  99. && error.message === 'item-not-found'
  100. && error.details
  101. && error.details.shard_changed;
  102. if (isShardChangedError) {
  103. const state = getState();
  104. const { timeEstablished } = state['features/base/connection'];
  105. const { _immediateReloadThreshold } = state['features/base/config'];
  106. const timeSinceConnectionEstablished = Number(timeEstablished && Date.now() - timeEstablished);
  107. const reloadThreshold = typeof _immediateReloadThreshold === 'number' ? _immediateReloadThreshold : 1500;
  108. const isWithinSplitBrainThreshold = !timeEstablished || timeSinceConnectionEstablished <= reloadThreshold;
  109. sendAnalytics(createConnectionEvent('failed', {
  110. ...error,
  111. connectionEstablished: timeEstablished,
  112. splitBrain: isWithinSplitBrainThreshold,
  113. timeSinceConnectionEstablished
  114. }));
  115. return isWithinSplitBrainThreshold;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Navigates to a route in accord with a specific redux state.
  121. *
  122. * @param {Store} store - The redux store which determines/identifies the route
  123. * to navigate to.
  124. * @private
  125. * @returns {void}
  126. */
  127. function _navigate({ getState }: IStore) {
  128. const state = getState();
  129. const { app } = state['features/base/app'];
  130. _getRouteToRender(state).then(route => app._navigate(route));
  131. }
  132. /**
  133. * Notifies the feature app that the action {@link SET_ROOM} is being dispatched
  134. * within a specific redux {@code store}.
  135. *
  136. * @param {Store} store - The redux store in which the specified {@code action}
  137. * is being dispatched.
  138. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  139. * specified {@code action} to the specified {@code store}.
  140. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  141. * dispatched in the specified {@code store}.
  142. * @private
  143. * @returns {Object} The new state that is the result of the reduction of the
  144. * specified {@code action}.
  145. */
  146. function _setRoom(store: IStore, next: Function, action: AnyAction) {
  147. const result = next(action);
  148. _navigate(store);
  149. return result;
  150. }