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 8.2KB

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