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

reducer.ts 7.0KB

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