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.js 6.5KB

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