瀏覽代碼

[RN] Add a timeout for loading the configuration

master
Saúl Ibarra Corretgé 8 年之前
父節點
當前提交
38b645bc27
共有 3 個檔案被更改,包括 39 行新增4 行删除
  1. 6
    1
      react/features/app/actions.js
  2. 5
    3
      react/features/base/lib-jitsi-meet/functions.js
  3. 28
    0
      react/features/base/util/helpers.js

+ 6
- 1
react/features/app/actions.js 查看文件

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

+ 5
- 3
react/features/base/lib-jitsi-meet/functions.js 查看文件

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

+ 28
- 0
react/features/base/util/helpers.js 查看文件

@@ -16,3 +16,31 @@ export function getJitsiMeetGlobalNS() {
16 16
 
17 17
     return window.JitsiMeetJS.app;
18 18
 }
19
+
20
+/**
21
+ * Makes the given promise fail with a timeout error if it wasn't fulfilled in
22
+ * the given timeout.
23
+ *
24
+ * @param {Promise} promise - The promise which will be wrapped for timeout.
25
+ * @param {number} ms - The amount of milliseconds to wait for a response before
26
+ * failing with a timeout error.
27
+ * @returns {Promise} - The wrapped promise.
28
+ */
29
+export function timeoutPromise(promise, ms) {
30
+    return new Promise((resolve, reject) => {
31
+        const timeoutId = setTimeout(() => {
32
+            reject(new Error('timeout'));
33
+        }, ms);
34
+
35
+        promise.then(
36
+            res => {
37
+                clearTimeout(timeoutId);
38
+                resolve(res);
39
+            },
40
+            err => {
41
+                clearTimeout(timeoutId);
42
+                reject(err);
43
+            }
44
+        );
45
+    });
46
+}

Loading…
取消
儲存