jiti-meet/react/features/base/lib-jitsi-meet/functions.js

159 lines
5.4 KiB
JavaScript
Raw Normal View History

// @flow
import { setConfigFromURLParams } from '../config';
import { toState } from '../redux';
import { loadScript } from '../util';
2017-02-19 00:42:11 +00:00
import JitsiMeetJS from './_';
declare var APP: Object;
const JitsiConferenceErrors = JitsiMeetJS.errors.conference;
2017-02-19 00:42:11 +00:00
const JitsiConnectionErrors = JitsiMeetJS.errors.connection;
/**
* Creates a JitsiLocalTrack model from the given device id.
*
* @param {string} type - The media type of track being created. Expected values
* are "video" or "audio".
* @param {string} deviceId - The id of the target media source.
* @returns {Promise<JitsiLocalTrack>}
*/
export function createLocalTrack(type: string, deviceId: string) {
return (
JitsiMeetJS.createLocalTracks({
cameraDeviceId: deviceId,
devices: [ type ],
// eslint-disable-next-line camelcase
firefox_fake_device:
window.config && window.config.firefox_fake_device,
micDeviceId: deviceId
})
.then(([ jitsiLocalTrack ]) => jitsiLocalTrack));
}
2017-09-25 17:31:19 +00:00
/**
* Determines whether analytics is enabled in a specific redux {@code store}.
*
* @param {Function|Object} stateful - The redux store, state, or
* {@code getState} function.
* @returns {boolean} If analytics is enabled, {@code true}; {@code false},
* otherwise.
*/
export function isAnalyticsEnabled(stateful: Function | Object) {
const {
analyticsScriptUrls,
disableThirdPartyRequests
} = toState(stateful)['features/base/config'];
return (
!disableThirdPartyRequests
&& Array.isArray(analyticsScriptUrls)
&& Boolean(analyticsScriptUrls.length));
}
/**
* Determines whether a specific {@link JitsiConferenceErrors} instance
* indicates a fatal {@link JitsiConference} error.
*
* FIXME Figure out the category of errors defined by the function and describe
* that category. I've currently named the category fatal because it appears to
* be used in the cases of unrecoverable errors that necessitate a reload.
*
* @param {Object|string} error - The {@code JitsiConferenceErrors} instance to
* categorize/classify or an {@link Error}-like object.
* @returns {boolean} If the specified {@code JitsiConferenceErrors} instance
* indicates a fatal {@code JitsiConference} error, {@code true}; otherwise,
* {@code false}.
*/
export function isFatalJitsiConferenceError(error: Object | string) {
if (typeof error !== 'string') {
error = error.name; // eslint-disable-line no-param-reassign
}
return (
error === JitsiConferenceErrors.FOCUS_DISCONNECTED
|| error === JitsiConferenceErrors.FOCUS_LEFT
|| error === JitsiConferenceErrors.VIDEOBRIDGE_NOT_AVAILABLE);
}
2017-02-19 00:42:11 +00:00
/**
* Determines whether a specific {@link JitsiConnectionErrors} instance
* indicates a fatal {@link JitsiConnection} error.
2017-02-19 00:42:11 +00:00
*
* FIXME Figure out the category of errors defined by the function and describe
2017-02-19 00:42:11 +00:00
* that category. I've currently named the category fatal because it appears to
* be used in the cases of unrecoverable errors that necessitate a reload.
*
* @param {Object|string} error - The {@code JitsiConnectionErrors} instance to
* categorize/classify or an {@link Error}-like object.
* @returns {boolean} If the specified {@code JitsiConnectionErrors} instance
* indicates a fatal {@code JitsiConnection} error, {@code true}; otherwise,
* {@code false}.
2017-02-19 00:42:11 +00:00
*/
export function isFatalJitsiConnectionError(error: Object | string) {
if (typeof error !== 'string') {
error = error.name; // eslint-disable-line no-param-reassign
}
2017-02-19 00:42:11 +00:00
return (
error === JitsiConnectionErrors.CONNECTION_DROPPED_ERROR
|| error === JitsiConnectionErrors.OTHER_ERROR
|| error === JitsiConnectionErrors.SERVER_ERROR);
}
/**
* Loads config.js from a specific remote server.
*
* @param {string} url - The URL to load.
* @param {number} [timeout] - The timeout for the configuration to be loaded,
* in milliseconds. If not specified, a default value deamed appropriate for the
* purpsoe is used.
* @returns {Promise<Object>}
*/
export function loadConfig(
url: string,
timeout: ?number = 10 /* seconds */ * 1000 /* in milliseconds */
): Promise<Object> {
let promise;
if (typeof APP === 'undefined') {
promise
= loadScript(url, timeout)
.then(() => {
const { config } = window;
2017-08-31 19:16:44 +00:00
// We don't want to pollute the global scope.
window.config = undefined;
if (typeof config !== 'object') {
throw new Error('window.config is not an object');
}
return config;
})
.catch(err => {
console.error(`Failed to load config from ${url}`, err);
throw err;
});
} else {
2017-08-31 19:16:44 +00:00
// Return "the config.js file" from the global scope - that is how the
// Web app on both the client and the server was implemented before the
// React Native app was even conceived.
promise = Promise.resolve(window.config);
}
// FIXME It's neither here nor there at the time of this writing where
// config, interfaceConfig, and loggingConfig should be overwritten by URL
// params.
promise = promise.then(value => {
setConfigFromURLParams();
return value;
});
return promise;
}