From 38b645bc27df71cc905839baedca72c98660395e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 1 Dec 2017 14:03:15 +0100 Subject: [PATCH] [RN] Add a timeout for loading the configuration --- react/features/app/actions.js | 7 ++++- .../features/base/lib-jitsi-meet/functions.js | 8 ++++-- react/features/base/util/helpers.js | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/react/features/app/actions.js b/react/features/app/actions.js index edc03d079..10be99372 100644 --- a/react/features/app/actions.js +++ b/react/features/app/actions.js @@ -10,6 +10,11 @@ import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from './actionTypes'; 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 * 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}`; - return loadConfig(url).then( + return loadConfig(url, LOAD_CONFIG_TIMEOUT).then( /* onFulfilled */ config => { // Try to store the configuration in localStorage. If the deployment // specified 'getroom' as a function, for example, it does not make diff --git a/react/features/base/lib-jitsi-meet/functions.js b/react/features/base/lib-jitsi-meet/functions.js index 38ce6c0e5..0707b2946 100644 --- a/react/features/base/lib-jitsi-meet/functions.js +++ b/react/features/base/lib-jitsi-meet/functions.js @@ -2,7 +2,7 @@ import { setConfigFromURLParams } from '../config'; import { toState } from '../redux'; -import { loadScript } from '../util'; +import { loadScript, timeoutPromise } from '../util'; import JitsiMeetJS from './_'; @@ -107,9 +107,11 @@ export function isFatalJitsiConnectionError(error: Object | string) { * Loads config.js from a specific remote server. * * @param {string} url - The URL to load. + * @param {number} timeoutMs - The timeout for the configuration to be loaded, + * in milliseconds. * @returns {Promise} */ -export function loadConfig(url: string): Promise { +export function loadConfig(url: string, timeoutMs: number): Promise { let promise; if (typeof APP === 'undefined') { @@ -148,5 +150,5 @@ export function loadConfig(url: string): Promise { return value; }); - return promise; + return timeoutPromise(promise, timeoutMs); } diff --git a/react/features/base/util/helpers.js b/react/features/base/util/helpers.js index 2391e30f9..e151ef2b7 100644 --- a/react/features/base/util/helpers.js +++ b/react/features/base/util/helpers.js @@ -16,3 +16,31 @@ export function getJitsiMeetGlobalNS() { 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); + } + ); + }); +}