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.2KB

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