You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.ts 7.1KB

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