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

reducer.js 5.4KB

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