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

functions.js 5.1KB

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