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

functions.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // @flow
  2. import { toState } from '../redux';
  3. import { loadScript } from '../util';
  4. import JitsiMeetJS from './_';
  5. declare var APP: Object;
  6. const JitsiConferenceErrors = JitsiMeetJS.errors.conference;
  7. const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. /**
  10. * Creates a {@link JitsiLocalTrack} model from the given device id.
  11. *
  12. * @param {string} type - The media type of track being created. Expected values
  13. * are "video" or "audio".
  14. * @param {string} deviceId - The id of the target media source.
  15. * @returns {Promise<JitsiLocalTrack>}
  16. */
  17. export function createLocalTrack(type: string, deviceId: string) {
  18. return (
  19. JitsiMeetJS.createLocalTracks({
  20. cameraDeviceId: deviceId,
  21. devices: [ type ],
  22. // eslint-disable-next-line camelcase
  23. firefox_fake_device:
  24. window.config && window.config.firefox_fake_device,
  25. micDeviceId: deviceId
  26. })
  27. .then(([ jitsiLocalTrack ]) => jitsiLocalTrack));
  28. }
  29. /**
  30. * Determines whether analytics is enabled in a specific redux {@code store}.
  31. *
  32. * @param {Function|Object} stateful - The redux store, state, or
  33. * {@code getState} function.
  34. * @returns {boolean} If analytics is enabled, {@code true}; {@code false},
  35. * otherwise.
  36. */
  37. export function isAnalyticsEnabled(stateful: Function | Object) {
  38. const {
  39. analytics = {},
  40. disableThirdPartyRequests
  41. } = toState(stateful)['features/base/config'];
  42. const { scriptURLs } = analytics;
  43. return (
  44. !disableThirdPartyRequests
  45. && Array.isArray(scriptURLs)
  46. && Boolean(scriptURLs.length));
  47. }
  48. /**
  49. * Determines whether a specific {@link JitsiConferenceErrors} instance
  50. * indicates a fatal {@link JitsiConference} error.
  51. *
  52. * FIXME Figure out the category of errors defined by the function and describe
  53. * that category. I've currently named the category fatal because it appears to
  54. * be used in the cases of unrecoverable errors that necessitate a reload.
  55. *
  56. * @param {Object|string} error - The {@code JitsiConferenceErrors} instance to
  57. * categorize/classify or an {@link Error}-like object.
  58. * @returns {boolean} If the specified {@code JitsiConferenceErrors} instance
  59. * indicates a fatal {@code JitsiConference} error, {@code true}; otherwise,
  60. * {@code false}.
  61. */
  62. export function isFatalJitsiConferenceError(error: Object | string) {
  63. if (typeof error !== 'string') {
  64. error = error.name; // eslint-disable-line no-param-reassign
  65. }
  66. return (
  67. error === JitsiConferenceErrors.FOCUS_DISCONNECTED
  68. || error === JitsiConferenceErrors.FOCUS_LEFT
  69. || error === JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  70. }
  71. /**
  72. * Determines whether a specific {@link JitsiConnectionErrors} instance
  73. * indicates a fatal {@link JitsiConnection} error.
  74. *
  75. * FIXME Figure out the category of errors defined by the function and describe
  76. * that category. I've currently named the category fatal because it appears to
  77. * be used in the cases of unrecoverable errors that necessitate a reload.
  78. *
  79. * @param {Object|string} error - The {@code JitsiConnectionErrors} instance to
  80. * categorize/classify or an {@link Error}-like object.
  81. * @returns {boolean} If the specified {@code JitsiConnectionErrors} instance
  82. * indicates a fatal {@code JitsiConnection} error, {@code true}; otherwise,
  83. * {@code false}.
  84. */
  85. export function isFatalJitsiConnectionError(error: Object | string) {
  86. if (typeof error !== 'string') {
  87. error = error.name; // eslint-disable-line no-param-reassign
  88. }
  89. return (
  90. error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
  91. || error === JitsiConnectionErrors.OTHER_ERROR
  92. || error === JitsiConnectionErrors.SERVER_ERROR);
  93. }
  94. /**
  95. * Loads config.js from a specific remote server.
  96. *
  97. * @param {string} url - The URL to load.
  98. * @param {number} [timeout] - The timeout in milliseconds for the {@code url}
  99. * to load. If not specified, a default value deemed appropriate for the purpose
  100. * is used.
  101. * @returns {Promise<Object>}
  102. */
  103. export function loadConfig(
  104. url: string,
  105. timeout: ?number = 10 /* seconds */ * 1000 /* in milliseconds */
  106. ): Promise<Object> {
  107. let promise;
  108. if (typeof APP === 'undefined') {
  109. promise
  110. = loadScript(url, timeout)
  111. .then(() => {
  112. const { config } = window;
  113. // We don't want to pollute the global scope.
  114. window.config = undefined;
  115. if (typeof config !== 'object') {
  116. throw new Error('window.config is not an object');
  117. }
  118. return config;
  119. })
  120. .catch(err => {
  121. logger.error(`Failed to load config from ${url}`, err);
  122. throw err;
  123. });
  124. } else {
  125. // Return "the config.js file" from the global scope - that is how the
  126. // Web app on both the client and the server was implemented before the
  127. // React Native app was even conceived.
  128. promise = Promise.resolve(window.config);
  129. }
  130. return promise;
  131. }