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

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