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

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