您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. };
  51. case LOAD_CONFIG_ERROR:
  52. return {
  53. error: action.error
  54. };
  55. case SET_CONFIG:
  56. return _setConfig(state, action);
  57. default:
  58. return state;
  59. }
  60. });
  61. /**
  62. * Gets the initial state of the feature base/config. The mandatory
  63. * configuration to be passed to JitsiMeetJS#init(). The app will download
  64. * config.js from the Jitsi Meet deployment and take its values into account but
  65. * the values bellow will be enforced (because they are essential to the correct
  66. * execution of the application).
  67. *
  68. * @returns {Object}
  69. */
  70. function _getInitialState() {
  71. return (
  72. navigator.product === 'ReactNative'
  73. ? INITIAL_RN_STATE
  74. : INITIAL_NON_RN_STATE);
  75. }
  76. /**
  77. * Reduces a specific Redux action SET_CONFIG of the feature
  78. * base/lib-jitsi-meet.
  79. *
  80. * @param {Object} state - The Redux state of the feature base/lib-jitsi-meet.
  81. * @param {Action} action - The Redux action SET_CONFIG to reduce.
  82. * @private
  83. * @returns {Object} The new state of the feature base/lib-jitsi-meet after the
  84. * reduction of the specified action.
  85. */
  86. function _setConfig(state, { config }) {
  87. // The mobile app bundles jitsi-meet and lib-jitsi-meet at build time and
  88. // does not download them at runtime from the deployment on which it will
  89. // join a conference. The downloading is planned for implementation in the
  90. // future (later rather than sooner) but is not implemented yet at the time
  91. // of this writing and, consequently, we must provide legacy support in the
  92. // meantime.
  93. // eslint-disable-next-line no-param-reassign
  94. config = _translateLegacyConfig(config);
  95. const newState = _.merge(
  96. {},
  97. config,
  98. { error: undefined },
  99. // The config of _getInitialState() is meant to override the config
  100. // downloaded from the Jitsi Meet deployment because the former contains
  101. // values that are mandatory.
  102. _getInitialState()
  103. );
  104. return equals(state, newState) ? state : newState;
  105. }
  106. /**
  107. * Constructs a new config {@code Object}, if necessary, out of a specific
  108. * config {@code Object} which is in the latest format supported by jitsi-meet.
  109. * Such a translation from an old config format to a new/the latest config
  110. * format is necessary because the mobile app bundles jitsi-meet and
  111. * lib-jitsi-meet at build time and does not download them at runtime from the
  112. * deployment on which it will join a conference.
  113. *
  114. * @param {Object} oldValue - The config {@code Object} which may or may not be
  115. * in the latest form supported by jitsi-meet and from which a new config
  116. * {@code Object} is to be constructed if necessary.
  117. * @returns {Object} A config {@code Object} which is in the latest format
  118. * supported by jitsi-meet.
  119. */
  120. function _translateLegacyConfig(oldValue: Object) {
  121. // jitsi/jitsi-meet#3ea2f005787c9f49c48febaeed9dc0340fe0a01b
  122. let newValue = oldValue;
  123. // At the time of this writing lib-jitsi-meet will rely on config having a
  124. // property with the name p2p and with a value of type Object.
  125. if (typeof oldValue.p2p !== 'object') {
  126. newValue = set(newValue, 'p2p', {});
  127. }
  128. /* eslint-disable indent */
  129. // Translate the old config properties into the new config.p2p properties.
  130. for (const [ oldKey, newKey ]
  131. of [
  132. [ 'backToP2PDelay', 'backToP2PDelay' ],
  133. [ 'enableP2P', 'enabled' ],
  134. [ 'p2pStunServers', 'stunServers' ]
  135. ]) {
  136. /* eslint-enable indent */
  137. if (oldKey in newValue) {
  138. const v = newValue[oldKey];
  139. // Do not modify oldValue.
  140. if (newValue === oldValue) {
  141. newValue = {
  142. ...newValue
  143. };
  144. }
  145. delete newValue[oldKey];
  146. // Do not modify p2p because it may be from oldValue i.e. do not
  147. // modify oldValue.
  148. newValue.p2p = {
  149. ...newValue.p2p,
  150. [newKey]: v
  151. };
  152. }
  153. }
  154. return newValue;
  155. }