You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* @flow */
  2. import { setConfigFromURLParams } from '../../base/config';
  3. import { toState } from '../../base/redux';
  4. import { loadScript } from '../../base/util';
  5. import JitsiMeetJS from './_';
  6. declare var APP: Object;
  7. const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
  8. /**
  9. * Creates a 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 JitsiConnectionErrors instance indicates a
  48. * fatal JitsiConnection error.
  49. *
  50. * FIXME Figure out the category of errors defined by the fucntion 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 JitsiConnectionErrors instance to
  55. * categorize/classify or an Error-like object.
  56. * @returns {boolean} True if the specified JitsiConnectionErrors instance
  57. * indicates a fatal JitsiConnection error; otherwise, false.
  58. */
  59. export function isFatalJitsiConnectionError(error: Object | string) {
  60. if (typeof error !== 'string') {
  61. error = error.name; // eslint-disable-line no-param-reassign
  62. }
  63. return (
  64. error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
  65. || error === JitsiConnectionErrors.OTHER_ERROR
  66. || error === JitsiConnectionErrors.SERVER_ERROR);
  67. }
  68. /**
  69. * Loads config.js from a specific remote server.
  70. *
  71. * @param {string} url - The URL to load.
  72. * @returns {Promise<Object>}
  73. */
  74. export function loadConfig(url: string) {
  75. let promise;
  76. if (typeof APP === 'undefined') {
  77. promise
  78. = loadScript(url)
  79. .then(() => {
  80. const { config } = window;
  81. // We don't want to pollute the global scope.
  82. window.config = undefined;
  83. if (typeof config !== 'object') {
  84. throw new Error('window.config is not an object');
  85. }
  86. return config;
  87. })
  88. .catch(err => {
  89. console.error(`Failed to load config from ${url}`, err);
  90. throw err;
  91. });
  92. } else {
  93. // Return "the config.js file" from the global scope - that is how the
  94. // Web app on both the client and the server was implemented before the
  95. // React Native app was even conceived.
  96. promise = Promise.resolve(window.config);
  97. }
  98. // FIXME It's neither here nor there at the time of this writing where
  99. // config, interfaceConfig, and loggingConfig should be overwritten by URL
  100. // params.
  101. promise = promise.then(value => {
  102. setConfigFromURLParams();
  103. return value;
  104. });
  105. return promise;
  106. }