Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

reducer.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // @flow
  2. import _ from 'lodash';
  3. import Platform from '../react/Platform';
  4. import { equals, ReducerRegistry, set } from '../redux';
  5. import { _UPDATE_CONFIG, CONFIG_WILL_LOAD, LOAD_CONFIG_ERROR, SET_CONFIG } from './actionTypes';
  6. import { _cleanupConfig } from './functions';
  7. /**
  8. * The initial state of the feature base/config when executing in a
  9. * non-React Native environment. The mandatory configuration to be passed to
  10. * JitsiMeetJS#init(). The app will download config.js from the Jitsi Meet
  11. * deployment and take its values into account but the values bellow will be
  12. * enforced (because they are essential to the correct execution of the
  13. * application).
  14. *
  15. * @type {Object}
  16. */
  17. const INITIAL_NON_RN_STATE = {
  18. };
  19. /**
  20. * When we should enable H.264 on mobile. iOS 10 crashes so we disable it there.
  21. * See: https://bugs.chromium.org/p/webrtc/issues/detail?id=11002
  22. * Note that this is only used for P2P calls.
  23. *
  24. * @type {boolean}
  25. */
  26. const RN_ENABLE_H264 = navigator.product === 'ReactNative' && !(Platform.OS === 'ios' && Platform.Version === 10);
  27. /**
  28. * The initial state of the feature base/config when executing in a React Native
  29. * environment. The mandatory configuration to be passed to JitsiMeetJS#init().
  30. * The app will download config.js from the Jitsi Meet deployment and take its
  31. * values into account but the values bellow will be enforced (because they are
  32. * essential to the correct execution of the application).
  33. *
  34. * @type {Object}
  35. */
  36. const INITIAL_RN_STATE = {
  37. analytics: {},
  38. // FIXME The support for audio levels in lib-jitsi-meet polls the statistics
  39. // of WebRTC at a short interval multiple times a second. Unfortunately,
  40. // React Native is slow to fetch these statistics from the native WebRTC
  41. // API, through the React Native bridge and eventually to JavaScript.
  42. // Because the audio levels are of no interest to the mobile app, it is
  43. // fastest to merely disable them.
  44. disableAudioLevels: true,
  45. p2p: {
  46. disableH264: !RN_ENABLE_H264,
  47. preferH264: RN_ENABLE_H264
  48. }
  49. };
  50. ReducerRegistry.register('features/base/config', (state = _getInitialState(), action) => {
  51. switch (action.type) {
  52. case _UPDATE_CONFIG:
  53. return _updateConfig(state, action);
  54. case CONFIG_WILL_LOAD:
  55. return {
  56. error: undefined,
  57. /**
  58. * The URL of the location associated with/configured by this
  59. * configuration.
  60. *
  61. * @type URL
  62. */
  63. locationURL: action.locationURL
  64. };
  65. case LOAD_CONFIG_ERROR:
  66. // XXX LOAD_CONFIG_ERROR is one of the settlement execution paths of
  67. // the asynchronous "loadConfig procedure/process" started with
  68. // CONFIG_WILL_LOAD. Due to the asynchronous nature of it, whoever
  69. // is settling the process needs to provide proof that they have
  70. // started it and that the iteration of the process being completed
  71. // now is still of interest to the app.
  72. if (state.locationURL === action.locationURL) {
  73. return {
  74. /**
  75. * The {@link Error} which prevented the loading of the
  76. * configuration of the associated {@code locationURL}.
  77. *
  78. * @type Error
  79. */
  80. error: action.error
  81. };
  82. }
  83. break;
  84. case SET_CONFIG:
  85. return _setConfig(state, action);
  86. }
  87. return state;
  88. });
  89. /**
  90. * Gets the initial state of the feature base/config. The mandatory
  91. * configuration to be passed to JitsiMeetJS#init(). The app will download
  92. * config.js from the Jitsi Meet deployment and take its values into account but
  93. * the values bellow will be enforced (because they are essential to the correct
  94. * execution of the application).
  95. *
  96. * @returns {Object}
  97. */
  98. function _getInitialState() {
  99. return (
  100. navigator.product === 'ReactNative'
  101. ? INITIAL_RN_STATE
  102. : INITIAL_NON_RN_STATE);
  103. }
  104. /**
  105. * Reduces a specific Redux action SET_CONFIG of the feature
  106. * base/lib-jitsi-meet.
  107. *
  108. * @param {Object} state - The Redux state of the feature base/config.
  109. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  110. * @private
  111. * @returns {Object} The new state after the reduction of the specified action.
  112. */
  113. function _setConfig(state, { config }) {
  114. // The mobile app bundles jitsi-meet and lib-jitsi-meet at build time and
  115. // does not download them at runtime from the deployment on which it will
  116. // join a conference. The downloading is planned for implementation in the
  117. // future (later rather than sooner) but is not implemented yet at the time
  118. // of this writing and, consequently, we must provide legacy support in the
  119. // meantime.
  120. // eslint-disable-next-line no-param-reassign
  121. config = _translateLegacyConfig(config);
  122. const newState = _.merge(
  123. {},
  124. config,
  125. { error: undefined },
  126. // The config of _getInitialState() is meant to override the config
  127. // downloaded from the Jitsi Meet deployment because the former contains
  128. // values that are mandatory.
  129. _getInitialState()
  130. );
  131. _cleanupConfig(newState);
  132. return equals(state, newState) ? state : newState;
  133. }
  134. /**
  135. * Constructs a new config {@code Object}, if necessary, out of a specific
  136. * config {@code Object} which is in the latest format supported by jitsi-meet.
  137. * Such a translation from an old config format to a new/the latest config
  138. * format is necessary because the mobile app bundles jitsi-meet and
  139. * lib-jitsi-meet at build time and does not download them at runtime from the
  140. * deployment on which it will join a conference.
  141. *
  142. * @param {Object} oldValue - The config {@code Object} which may or may not be
  143. * in the latest form supported by jitsi-meet and from which a new config
  144. * {@code Object} is to be constructed if necessary.
  145. * @returns {Object} A config {@code Object} which is in the latest format
  146. * supported by jitsi-meet.
  147. */
  148. function _translateLegacyConfig(oldValue: Object) {
  149. let newValue = oldValue;
  150. const oldConfigToNewConfig = {
  151. analytics: [
  152. [ 'analyticsScriptUrls', 'scriptURLs' ],
  153. [ 'googleAnalyticsTrackingId', 'googleAnalyticsTrackingId' ]
  154. ]
  155. };
  156. // Translate the old config properties into the new config properties.
  157. Object.keys(oldConfigToNewConfig).forEach(section => {
  158. if (typeof oldValue[section] !== 'object') {
  159. newValue = set(newValue, section, {});
  160. }
  161. for (const [ oldKey, newKey ] of oldConfigToNewConfig[section]) {
  162. if (oldKey in newValue && !(newKey in newValue[section])) {
  163. const v = newValue[oldKey];
  164. // Do not modify oldValue.
  165. if (newValue === oldValue) {
  166. newValue = {
  167. ...newValue
  168. };
  169. }
  170. delete newValue[oldKey];
  171. // Do not modify the section because it may be from oldValue
  172. // i.e. do not modify oldValue.
  173. newValue[section] = {
  174. ...newValue[section],
  175. [newKey]: v
  176. };
  177. }
  178. }
  179. });
  180. return newValue;
  181. }
  182. /**
  183. * Updates the stored configuration with the given extra options.
  184. *
  185. * @param {Object} state - The Redux state of the feature base/config.
  186. * @param {Action} action - The Redux action to reduce.
  187. * @private
  188. * @returns {Object} The new state after the reduction of the specified action.
  189. */
  190. function _updateConfig(state, { config }) {
  191. const newState = _.merge({}, state, config);
  192. _cleanupConfig(newState);
  193. return equals(state, newState) ? state : newState;
  194. }