Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

reducer.ts 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { SET_ROOM } from '../conference/actionTypes';
  2. import { SET_JWT } from '../jwt/actionTypes';
  3. import { JitsiConnectionErrors } from '../lib-jitsi-meet';
  4. import ReducerRegistry from '../redux/ReducerRegistry';
  5. import { assign, set } from '../redux/functions';
  6. import {
  7. CONNECTION_DISCONNECTED,
  8. CONNECTION_ESTABLISHED,
  9. CONNECTION_FAILED,
  10. CONNECTION_WILL_CONNECT,
  11. SET_LOCATION_URL,
  12. SHOW_CONNECTION_INFO
  13. } from './actionTypes';
  14. import { ConnectionFailedError } from './types';
  15. export interface IConnectionState {
  16. connecting?: any;
  17. connection?: {
  18. addFeature: Function;
  19. disconnect: Function;
  20. getJid: () => string;
  21. getLogs: () => Object;
  22. initJitsiConference: Function;
  23. removeFeature: Function;
  24. };
  25. error?: ConnectionFailedError;
  26. locationURL?: URL;
  27. passwordRequired?: Object;
  28. preferVisitor?: boolean;
  29. showConnectionInfo?: boolean;
  30. timeEstablished?: number;
  31. }
  32. /**
  33. * Reduces the Redux actions of the feature base/connection.
  34. */
  35. ReducerRegistry.register<IConnectionState>(
  36. 'features/base/connection',
  37. (state = {}, action): IConnectionState => {
  38. switch (action.type) {
  39. case CONNECTION_DISCONNECTED:
  40. return _connectionDisconnected(state, action);
  41. case CONNECTION_ESTABLISHED:
  42. return _connectionEstablished(state, action);
  43. case CONNECTION_FAILED:
  44. return _connectionFailed(state, action);
  45. case CONNECTION_WILL_CONNECT:
  46. return _connectionWillConnect(state, action);
  47. case SET_JWT:
  48. return _setJWT(state, action);
  49. case SET_LOCATION_URL:
  50. return _setLocationURL(state, action);
  51. case SET_ROOM:
  52. return _setRoom(state);
  53. case SHOW_CONNECTION_INFO:
  54. return _setShowConnectionInfo(state, action);
  55. }
  56. return state;
  57. });
  58. /**
  59. * Reduces a specific Redux action CONNECTION_DISCONNECTED of the feature
  60. * base/connection.
  61. *
  62. * @param {IConnectionState} state - The Redux state of the feature base/connection.
  63. * @param {Action} action - The Redux action CONNECTION_DISCONNECTED 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 _connectionDisconnected(
  69. state: IConnectionState,
  70. { connection }: { connection: Object; }) {
  71. const connection_ = _getCurrentConnection(state);
  72. if (connection_ !== connection) {
  73. return state;
  74. }
  75. return assign(state, {
  76. connecting: undefined,
  77. connection: undefined,
  78. preferVisitor: undefined,
  79. timeEstablished: undefined
  80. });
  81. }
  82. /**
  83. * Reduces a specific Redux action CONNECTION_ESTABLISHED of the feature
  84. * base/connection.
  85. *
  86. * @param {IConnectionState} state - The Redux state of the feature base/connection.
  87. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED to reduce.
  88. * @private
  89. * @returns {Object} The new state of the feature base/connection after the
  90. * reduction of the specified action.
  91. */
  92. function _connectionEstablished(
  93. state: IConnectionState,
  94. { connection, timeEstablished }: {
  95. connection: any;
  96. timeEstablished: number;
  97. }) {
  98. return assign(state, {
  99. connecting: undefined,
  100. connection,
  101. error: undefined,
  102. passwordRequired: undefined,
  103. timeEstablished
  104. });
  105. }
  106. /**
  107. * Reduces a specific Redux action CONNECTION_FAILED of the feature
  108. * base/connection.
  109. *
  110. * @param {IConnectionState} state - The Redux state of the feature base/connection.
  111. * @param {Action} action - The Redux action CONNECTION_FAILED to reduce.
  112. * @private
  113. * @returns {Object} The new state of the feature base/connection after the
  114. * reduction of the specified action.
  115. */
  116. function _connectionFailed(
  117. state: IConnectionState,
  118. { connection, error }: {
  119. connection: Object;
  120. error: ConnectionFailedError;
  121. }) {
  122. const connection_ = _getCurrentConnection(state);
  123. if (connection_ && connection_ !== connection) {
  124. return state;
  125. }
  126. return assign(state, {
  127. connecting: undefined,
  128. connection: undefined,
  129. error,
  130. passwordRequired:
  131. error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  132. ? connection : undefined,
  133. preferVisitor: undefined
  134. });
  135. }
  136. /**
  137. * Reduces a specific Redux action CONNECTION_WILL_CONNECT of the feature
  138. * base/connection.
  139. *
  140. * @param {IConnectionState} state - The Redux state of the feature base/connection.
  141. * @param {Action} action - The Redux action CONNECTION_WILL_CONNECT to reduce.
  142. * @private
  143. * @returns {Object} The new state of the feature base/connection after the
  144. * reduction of the specified action.
  145. */
  146. function _connectionWillConnect(
  147. state: IConnectionState,
  148. { connection }: { connection: Object; }) {
  149. return assign(state, {
  150. connecting: connection,
  151. // We don't care if the previous connection has been closed already,
  152. // because it's an async process and there's no guarantee if it'll be
  153. // done before the new one is established.
  154. connection: undefined,
  155. error: undefined,
  156. passwordRequired: undefined,
  157. timeEstablished: undefined
  158. });
  159. }
  160. /**
  161. * The current (similar to getCurrentConference in base/conference/functions.any.js)
  162. * connection which is {@code connection} or {@code connecting}.
  163. *
  164. * @param {IConnectionState} baseConnectionState - The current state of the
  165. * {@code 'base/connection'} feature.
  166. * @returns {JitsiConnection} - The current {@code JitsiConnection} if any.
  167. * @private
  168. */
  169. function _getCurrentConnection(baseConnectionState: IConnectionState): IConnectionState | undefined {
  170. return baseConnectionState.connection || baseConnectionState.connecting;
  171. }
  172. /**
  173. * Reduces a specific redux action {@link SET_JWT} of the feature
  174. * base/connection.
  175. *
  176. * @param {IConnectionState} state - The redux state of the feature base/connection.
  177. * @param {Action} action - The Redux action SET_JWT to reduce.
  178. * @private
  179. * @returns {Object} The new state of the feature base/connection after the
  180. * reduction of the specified action.
  181. */
  182. function _setJWT(state: IConnectionState, { preferVisitor }: { preferVisitor: boolean; }) {
  183. return assign(state, {
  184. preferVisitor
  185. });
  186. }
  187. /**
  188. * Reduces a specific redux action {@link SET_LOCATION_URL} of the feature
  189. * base/connection.
  190. *
  191. * @param {IConnectionState} state - The redux state of the feature base/connection.
  192. * @param {Action} action - The redux action {@code SET_LOCATION_URL} to reduce.
  193. * @private
  194. * @returns {Object} The new state of the feature base/connection after the
  195. * reduction of the specified action.
  196. */
  197. function _setLocationURL(
  198. state: IConnectionState,
  199. { locationURL }: { locationURL?: URL; }) {
  200. return set(state, 'locationURL', locationURL);
  201. }
  202. /**
  203. * Reduces a specific redux action {@link SET_ROOM} of the feature
  204. * base/connection.
  205. *
  206. * @param {IConnectionState} state - The redux state of the feature base/connection.
  207. * @private
  208. * @returns {Object} The new state of the feature base/connection after the
  209. * reduction of the specified action.
  210. */
  211. function _setRoom(state: IConnectionState) {
  212. return assign(state, {
  213. error: undefined,
  214. passwordRequired: undefined
  215. });
  216. }
  217. /**
  218. * Reduces a specific redux action {@link SHOW_CONNECTION_INFO} of the feature
  219. * base/connection.
  220. *
  221. * @param {IConnectionState} state - The redux state of the feature base/connection.
  222. * @param {Action} action - The redux action {@code SHOW_CONNECTION_INFO} to reduce.
  223. * @private
  224. * @returns {Object} The new state of the feature base/connection after the
  225. * reduction of the specified action.
  226. */
  227. function _setShowConnectionInfo(
  228. state: IConnectionState,
  229. { showConnectionInfo }: { showConnectionInfo: boolean; }) {
  230. return set(state, 'showConnectionInfo', showConnectionInfo);
  231. }