[RN] Add a timeout for loading the configuration

This commit is contained in:
Saúl Ibarra Corretgé 2017-12-01 14:03:15 +01:00 committed by Lyubo Marinov
parent 15bf6b9e30
commit 38b645bc27
3 changed files with 39 additions and 4 deletions

View File

@ -10,6 +10,11 @@ import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes';
declare var APP: Object; declare var APP: Object;
/**
* Timeout for loading the configuration.
*/
const LOAD_CONFIG_TIMEOUT = 8000;
/** /**
* Triggers an in-app navigation to a specific route. Allows navigation to be * Triggers an in-app navigation to a specific route. Allows navigation to be
* abstracted between the mobile/React Native and Web/React applications. * abstracted between the mobile/React Native and Web/React applications.
@ -206,7 +211,7 @@ function _loadConfig({ contextRoot, host, protocol, room }) {
const key = `config.js/${baseURL}`; const key = `config.js/${baseURL}`;
return loadConfig(url).then( return loadConfig(url, LOAD_CONFIG_TIMEOUT).then(
/* onFulfilled */ config => { /* onFulfilled */ config => {
// Try to store the configuration in localStorage. If the deployment // Try to store the configuration in localStorage. If the deployment
// specified 'getroom' as a function, for example, it does not make // specified 'getroom' as a function, for example, it does not make

View File

@ -2,7 +2,7 @@
import { setConfigFromURLParams } from '../config'; import { setConfigFromURLParams } from '../config';
import { toState } from '../redux'; import { toState } from '../redux';
import { loadScript } from '../util'; import { loadScript, timeoutPromise } from '../util';
import JitsiMeetJS from './_'; import JitsiMeetJS from './_';
@ -107,9 +107,11 @@ export function isFatalJitsiConnectionError(error: Object | string) {
* Loads config.js from a specific remote server. * Loads config.js from a specific remote server.
* *
* @param {string} url - The URL to load. * @param {string} url - The URL to load.
* @param {number} timeoutMs - The timeout for the configuration to be loaded,
* in milliseconds.
* @returns {Promise<Object>} * @returns {Promise<Object>}
*/ */
export function loadConfig(url: string): Promise<Object> { export function loadConfig(url: string, timeoutMs: number): Promise<Object> {
let promise; let promise;
if (typeof APP === 'undefined') { if (typeof APP === 'undefined') {
@ -148,5 +150,5 @@ export function loadConfig(url: string): Promise<Object> {
return value; return value;
}); });
return promise; return timeoutPromise(promise, timeoutMs);
} }

View File

@ -16,3 +16,31 @@ export function getJitsiMeetGlobalNS() {
return window.JitsiMeetJS.app; return window.JitsiMeetJS.app;
} }
/**
* Makes the given promise fail with a timeout error if it wasn't fulfilled in
* the given timeout.
*
* @param {Promise} promise - The promise which will be wrapped for timeout.
* @param {number} ms - The amount of milliseconds to wait for a response before
* failing with a timeout error.
* @returns {Promise} - The wrapped promise.
*/
export function timeoutPromise(promise, ms) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('timeout'));
}, ms);
promise.then(
res => {
clearTimeout(timeoutId);
resolve(res);
},
err => {
clearTimeout(timeoutId);
reject(err);
}
);
});
}