Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

reducer.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* @flow */
  2. import { SET_ROOM } from '../conference';
  3. import { JitsiConnectionErrors } from '../lib-jitsi-meet';
  4. import { assign, ReducerRegistry } from '../redux';
  5. import { parseURIString } from '../util';
  6. import {
  7. CONNECTION_DISCONNECTED,
  8. CONNECTION_ESTABLISHED,
  9. CONNECTION_FAILED,
  10. CONNECTION_WILL_CONNECT,
  11. SET_LOCATION_URL
  12. } from './actionTypes';
  13. import type { ConnectionFailedError } from './actions.native';
  14. /**
  15. * Reduces the Redux actions of the feature base/connection.
  16. */
  17. ReducerRegistry.register(
  18. 'features/base/connection',
  19. (state: Object = {}, action: Object) => {
  20. switch (action.type) {
  21. case CONNECTION_DISCONNECTED:
  22. return _connectionDisconnected(state, action);
  23. case CONNECTION_ESTABLISHED:
  24. return _connectionEstablished(state, action);
  25. case CONNECTION_FAILED:
  26. return _connectionFailed(state, action);
  27. case CONNECTION_WILL_CONNECT:
  28. return _connectionWillConnect(state, action);
  29. case SET_LOCATION_URL:
  30. return _setLocationURL(state, action);
  31. case SET_ROOM:
  32. return _setRoom(state);
  33. }
  34. return state;
  35. });
  36. /**
  37. * Reduces a specific Redux action CONNECTION_DISCONNECTED of the feature
  38. * base/connection.
  39. *
  40. * @param {Object} state - The Redux state of the feature base/connection.
  41. * @param {Action} action - The Redux action CONNECTION_DISCONNECTED to reduce.
  42. * @private
  43. * @returns {Object} The new state of the feature base/connection after the
  44. * reduction of the specified action.
  45. */
  46. function _connectionDisconnected(
  47. state: Object,
  48. { connection }: { connection: Object }) {
  49. const connection_ = _getCurrentConnection(state);
  50. if (connection_ !== connection) {
  51. return state;
  52. }
  53. return assign(state, {
  54. connecting: undefined,
  55. connection: undefined
  56. });
  57. }
  58. /**
  59. * Reduces a specific Redux action CONNECTION_ESTABLISHED of the feature
  60. * base/connection.
  61. *
  62. * @param {Object} state - The Redux state of the feature base/connection.
  63. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED to reduce.
  64. * @private
  65. * @returns {Object} The new state of the feature base/connection after the
  66. * reduction of the specified action.
  67. */
  68. function _connectionEstablished(
  69. state: Object,
  70. { connection }: { connection: Object }) {
  71. return assign(state, {
  72. connecting: undefined,
  73. connection,
  74. error: undefined,
  75. passwordRequired: undefined
  76. });
  77. }
  78. /**
  79. * Reduces a specific Redux action CONNECTION_FAILED of the feature
  80. * base/connection.
  81. *
  82. * @param {Object} state - The Redux state of the feature base/connection.
  83. * @param {Action} action - The Redux action CONNECTION_FAILED to reduce.
  84. * @private
  85. * @returns {Object} The new state of the feature base/connection after the
  86. * reduction of the specified action.
  87. */
  88. function _connectionFailed(
  89. state: Object,
  90. { connection, error }: {
  91. connection: Object,
  92. error: ConnectionFailedError
  93. }) {
  94. const connection_ = _getCurrentConnection(state);
  95. if (connection_ && connection_ !== connection) {
  96. return state;
  97. }
  98. return assign(state, {
  99. connecting: undefined,
  100. connection: undefined,
  101. error,
  102. passwordRequired:
  103. error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  104. ? connection : undefined
  105. });
  106. }
  107. /**
  108. * Reduces a specific Redux action CONNECTION_WILL_CONNECT of the feature
  109. * base/connection.
  110. *
  111. * @param {Object} state - The Redux state of the feature base/connection.
  112. * @param {Action} action - The Redux action CONNECTION_WILL_CONNECT to reduce.
  113. * @private
  114. * @returns {Object} The new state of the feature base/connection after the
  115. * reduction of the specified action.
  116. */
  117. function _connectionWillConnect(
  118. state: Object,
  119. { connection }: { connection: Object }) {
  120. return assign(state, {
  121. connecting: connection,
  122. // We don't care if the previous connection has been closed already,
  123. // because it's an async process and there's no guarantee if it'll be
  124. // done before the new one is established.
  125. connection: undefined,
  126. error: undefined,
  127. passwordRequired: undefined
  128. });
  129. }
  130. /**
  131. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  132. * based on a specific location URL.
  133. *
  134. * @param {string} locationURL - The location URL with which the returned
  135. * options are to be constructed.
  136. * @private
  137. * @returns {Object} The options to be passed to the constructor of
  138. * {@code JitsiConnection} based on the location URL.
  139. */
  140. function _constructOptions(locationURL: URL) {
  141. const locationURI = parseURIString(locationURL.href);
  142. // FIXME The HTTPS scheme for the BOSH URL works with meet.jit.si on both
  143. // mobile & Web. It also works with beta.meet.jit.si on Web. Unfortunately,
  144. // it doesn't work with beta.meet.jit.si on mobile. Temporarily, use the
  145. // HTTP scheme for the BOSH URL with beta.meet.jit.si on mobile.
  146. let { protocol } = locationURI;
  147. const domain = locationURI.hostname;
  148. if (!protocol && domain === 'beta.meet.jit.si') {
  149. const windowLocation = window.location;
  150. windowLocation && (protocol = windowLocation.protocol);
  151. protocol || (protocol = 'http:');
  152. }
  153. // Default to the HTTPS scheme for the BOSH URL.
  154. protocol || (protocol = 'https:');
  155. return {
  156. bosh:
  157. `${String(protocol)}//${domain}${
  158. locationURI.contextRoot || '/'}http-bind`,
  159. hosts: {
  160. domain,
  161. // Required by:
  162. // - lib-jitsi-meet/modules/xmpp/xmpp.js
  163. muc: `conference.${domain}`
  164. }
  165. };
  166. }
  167. /**
  168. * The current (similar to getCurrentConference in base/conference/functions.js)
  169. * connection which is {@code connection} or {@code connecting}.
  170. *
  171. * @param {Object} baseConnectionState - The current state of the
  172. * {@code 'base/connection'} feature.
  173. * @returns {JitsiConnection} - The current {@code JitsiConnection} if any.
  174. * @private
  175. */
  176. function _getCurrentConnection(baseConnectionState: Object): ?Object {
  177. return baseConnectionState.connection || baseConnectionState.connecting;
  178. }
  179. /**
  180. * Reduces a specific redux action {@link SET_LOCATION_URL} of the feature
  181. * base/connection.
  182. *
  183. * @param {Object} state - The redux state of the feature base/connection.
  184. * @param {Action} action - The redux action {@code SET_LOCATION_URL} to reduce.
  185. * @private
  186. * @returns {Object} The new state of the feature base/connection after the
  187. * reduction of the specified action.
  188. */
  189. function _setLocationURL(
  190. state: Object,
  191. { locationURL }: { locationURL: ?URL }) {
  192. return assign(state, {
  193. locationURL,
  194. options: locationURL ? _constructOptions(locationURL) : undefined
  195. });
  196. }
  197. /**
  198. * Reduces a specific redux action {@link SET_ROOM} of the feature
  199. * base/connection.
  200. *
  201. * @param {Object} state - The redux state of the feature base/connection.
  202. * @private
  203. * @returns {Object} The new state of the feature base/connection after the
  204. * reduction of the specified action.
  205. */
  206. function _setRoom(state: Object) {
  207. return assign(state, {
  208. error: undefined,
  209. passwordRequired: undefined
  210. });
  211. }