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

functions.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // @flow
  2. import { toState } from '../redux';
  3. import { loadScript } from '../util';
  4. import JitsiMeetJS from './_';
  5. import logger from './logger';
  6. declare var APP: Object;
  7. const JitsiConferenceErrors = JitsiMeetJS.errors.conference;
  8. const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
  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 { disableThirdPartyRequests, analytics = {} } = toState(stateful)['features/base/config'];
  39. return !disableThirdPartyRequests && !analytics.disabled;
  40. }
  41. /**
  42. * Determines whether a specific {@link JitsiConferenceErrors} instance
  43. * indicates a fatal {@link JitsiConference} error.
  44. *
  45. * FIXME Figure out the category of errors defined by the function and describe
  46. * that category. I've currently named the category fatal because it appears to
  47. * be used in the cases of unrecoverable errors that necessitate a reload.
  48. *
  49. * @param {Object|string} error - The {@code JitsiConferenceErrors} instance to
  50. * categorize/classify or an {@link Error}-like object.
  51. * @returns {boolean} If the specified {@code JitsiConferenceErrors} instance
  52. * indicates a fatal {@code JitsiConference} error, {@code true}; otherwise,
  53. * {@code false}.
  54. */
  55. export function isFatalJitsiConferenceError(error: Object | string) {
  56. if (typeof error !== 'string') {
  57. error = error.name; // eslint-disable-line no-param-reassign
  58. }
  59. return (
  60. error === JitsiConferenceErrors.FOCUS_DISCONNECTED
  61. || error === JitsiConferenceErrors.FOCUS_LEFT
  62. || error === JitsiConferenceErrors.OFFER_ANSWER_FAILED
  63. || error === JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
  64. }
  65. /**
  66. * Determines whether a specific {@link JitsiConnectionErrors} instance
  67. * indicates a fatal {@link JitsiConnection} error.
  68. *
  69. * FIXME Figure out the category of errors defined by the function and describe
  70. * that category. I've currently named the category fatal because it appears to
  71. * be used in the cases of unrecoverable errors that necessitate a reload.
  72. *
  73. * @param {Object|string} error - The {@code JitsiConnectionErrors} instance to
  74. * categorize/classify or an {@link Error}-like object.
  75. * @returns {boolean} If the specified {@code JitsiConnectionErrors} instance
  76. * indicates a fatal {@code JitsiConnection} error, {@code true}; otherwise,
  77. * {@code false}.
  78. */
  79. export function isFatalJitsiConnectionError(error: Object | string) {
  80. if (typeof error !== 'string') {
  81. error = error.name; // eslint-disable-line no-param-reassign
  82. }
  83. return (
  84. error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
  85. || error === JitsiConnectionErrors.OTHER_ERROR
  86. || error === JitsiConnectionErrors.SERVER_ERROR);
  87. }
  88. /**
  89. * Loads config.js from a specific remote server.
  90. *
  91. * @param {string} url - The URL to load.
  92. * @returns {Promise<Object>}
  93. */
  94. export function loadConfig(url: string): Promise<Object> {
  95. let promise;
  96. if (typeof APP === 'undefined') {
  97. promise
  98. = loadScript(url, 2.5 * 1000 /* Timeout in ms */)
  99. .then(() => {
  100. const { config } = window;
  101. // We don't want to pollute the global scope.
  102. window.config = undefined;
  103. if (typeof config !== 'object') {
  104. throw new Error('window.config is not an object');
  105. }
  106. return config;
  107. })
  108. .catch(err => {
  109. logger.error(`Failed to load config from ${url}`, err);
  110. throw err;
  111. });
  112. } else {
  113. // Return "the config.js file" from the global scope - that is how the
  114. // Web app on both the client and the server was implemented before the
  115. // React Native app was even conceived.
  116. promise = Promise.resolve(window.config);
  117. }
  118. return promise;
  119. }