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

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