您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 5.6KB

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