Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

reducer.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* @flow */
  2. import _ from 'lodash';
  3. import { equals, ReducerRegistry, set } from '../redux';
  4. import {
  5. CONFIG_WILL_LOAD,
  6. LOAD_CONFIG_ERROR,
  7. SET_CONFIG
  8. } from './actionTypes';
  9. /**
  10. * The initial state of the feature base/config when executing in a
  11. * non-React Native environment. The mandatory configuration to be passed to
  12. * JitsiMeetJS#init(). The app will download config.js from the Jitsi Meet
  13. * deployment and take its values into account but the values bellow will be
  14. * enforced (because they are essential to the correct execution of the
  15. * application).
  16. *
  17. * @type {Object}
  18. */
  19. const INITIAL_NON_RN_STATE = {
  20. };
  21. /**
  22. * The initial state of the feature base/config when executing in a React Native
  23. * environment. The mandatory configuration to be passed to JitsiMeetJS#init().
  24. * The app will download config.js from the Jitsi Meet deployment and take its
  25. * values into account but the values bellow will be enforced (because they are
  26. * essential to the correct execution of the application).
  27. *
  28. * @type {Object}
  29. */
  30. const INITIAL_RN_STATE = {
  31. // FIXME The support for audio levels in lib-jitsi-meet polls the statistics
  32. // of WebRTC at a short interval multiple times a second. Unfortunately,
  33. // React Native is slow to fetch these statistics from the native WebRTC
  34. // API, through the React Native bridge and eventually to JavaScript.
  35. // Because the audio levels are of no interest to the mobile app, it is
  36. // fastest to merely disable them.
  37. disableAudioLevels: true,
  38. p2p: {
  39. disableH264: false,
  40. preferH264: true
  41. }
  42. };
  43. ReducerRegistry.register(
  44. 'features/base/config',
  45. (state = _getInitialState(), action) => {
  46. switch (action.type) {
  47. case CONFIG_WILL_LOAD:
  48. return {
  49. error: undefined,
  50. locationURL: action.locationURL
  51. };
  52. case LOAD_CONFIG_ERROR:
  53. return {
  54. error: action.error
  55. };
  56. case SET_CONFIG:
  57. return _setConfig(state, action);
  58. default:
  59. return state;
  60. }
  61. });
  62. /**
  63. * Gets the initial state of the feature base/config. The mandatory
  64. * configuration to be passed to JitsiMeetJS#init(). The app will download
  65. * config.js from the Jitsi Meet deployment and take its values into account but
  66. * the values bellow will be enforced (because they are essential to the correct
  67. * execution of the application).
  68. *
  69. * @returns {Object}
  70. */
  71. function _getInitialState() {
  72. return (
  73. navigator.product === 'ReactNative'
  74. ? INITIAL_RN_STATE
  75. : INITIAL_NON_RN_STATE);
  76. }
  77. /**
  78. * Reduces a specific Redux action SET_CONFIG of the feature
  79. * base/lib-jitsi-meet.
  80. *
  81. * @param {Object} state - The Redux state of the feature base/lib-jitsi-meet.
  82. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  83. * @private
  84. * @returns {Object} The new state of the feature base/lib-jitsi-meet after the
  85. * reduction of the specified action.
  86. */
  87. function _setConfig(state, { config }) {
  88. // The mobile app bundles jitsi-meet and lib-jitsi-meet at build time and
  89. // does not download them at runtime from the deployment on which it will
  90. // join a conference. The downloading is planned for implementation in the
  91. // future (later rather than sooner) but is not implemented yet at the time
  92. // of this writing and, consequently, we must provide legacy support in the
  93. // meantime.
  94. // eslint-disable-next-line no-param-reassign
  95. config = _translateLegacyConfig(config);
  96. const newState = _.merge(
  97. {},
  98. config,
  99. { error: undefined },
  100. // The config of _getInitialState() is meant to override the config
  101. // downloaded from the Jitsi Meet deployment because the former contains
  102. // values that are mandatory.
  103. _getInitialState()
  104. );
  105. return equals(state, newState) ? state : newState;
  106. }
  107. /**
  108. * Constructs a new config {@code Object}, if necessary, out of a specific
  109. * config {@code Object} which is in the latest format supported by jitsi-meet.
  110. * Such a translation from an old config format to a new/the latest config
  111. * format is necessary because the mobile app bundles jitsi-meet and
  112. * lib-jitsi-meet at build time and does not download them at runtime from the
  113. * deployment on which it will join a conference.
  114. *
  115. * @param {Object} oldValue - The config {@code Object} which may or may not be
  116. * in the latest form supported by jitsi-meet and from which a new config
  117. * {@code Object} is to be constructed if necessary.
  118. * @returns {Object} A config {@code Object} which is in the latest format
  119. * supported by jitsi-meet.
  120. */
  121. function _translateLegacyConfig(oldValue: Object) {
  122. // jitsi/jitsi-meet#3ea2f005787c9f49c48febaeed9dc0340fe0a01b
  123. let newValue = oldValue;
  124. // At the time of this writing lib-jitsi-meet will rely on config having a
  125. // property with the name p2p and with a value of type Object.
  126. if (typeof oldValue.p2p !== 'object') {
  127. newValue = set(newValue, 'p2p', {});
  128. }
  129. /* eslint-disable indent */
  130. // Translate the old config properties into the new config.p2p properties.
  131. for (const [ oldKey, newKey ]
  132. of [
  133. [ 'backToP2PDelay', 'backToP2PDelay' ],
  134. [ 'enableP2P', 'enabled' ],
  135. [ 'p2pStunServers', 'stunServers' ]
  136. ]) {
  137. /* eslint-enable indent */
  138. if (oldKey in newValue && !(newKey in newValue.p2p)) {
  139. const v = newValue[oldKey];
  140. // Do not modify oldValue.
  141. if (newValue === oldValue) {
  142. newValue = {
  143. ...newValue
  144. };
  145. }
  146. delete newValue[oldKey];
  147. // Do not modify p2p because it may be from oldValue i.e. do not
  148. // modify oldValue.
  149. newValue.p2p = {
  150. ...newValue.p2p,
  151. [newKey]: v
  152. };
  153. }
  154. }
  155. return newValue;
  156. }