Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

reducer.ts 7.1KB

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