浏览代码

ref(deep-linking): Improve the window loaded detection logic.

master
hristoterezov 6 年前
父节点
当前提交
fccd0d6b29
共有 1 个文件被更改,包括 15 次插入33 次删除
  1. 15
    33
      react/features/deep-linking/functions.js

+ 15
- 33
react/features/deep-linking/functions.js 查看文件

12
     from './shouldShowDeepLinkingDesktopPage';
12
     from './shouldShowDeepLinkingDesktopPage';
13
 
13
 
14
 /**
14
 /**
15
- * Indicates whether the window load event was already received.
15
+ * Promise that resolves when the window load event is received.
16
  *
16
  *
17
- * @type {boolean}
17
+ * @type {Promise<void>}
18
  */
18
  */
19
-let windowIsLoaded = false;
20
-
21
-/**
22
- * Handler for the window load event.
23
- *
24
- * @returns {void}
25
- */
26
-function onWindowLoad() {
27
-    windowIsLoaded = true;
28
-    window.removeEventListener('load', onWindowLoad);
29
-}
19
+const windowLoadedPromise = new Promise(resolve => {
20
+    /**
21
+     * Handler for the window load event.
22
+     *
23
+     * @returns {void}
24
+     */
25
+    function onWindowLoad() {
26
+        resolve();
27
+        window.removeEventListener('load', onWindowLoad);
28
+    }
30
 
29
 
31
-window.addEventListener('load', onWindowLoad);
30
+    window.addEventListener('load', onWindowLoad);
31
+});
32
 
32
 
33
-/**
34
- * Executes the passed function after the window load event was received.
35
- *
36
- * @param {Function} fn - The function that will be executed.
37
- * @returns {void}
38
- */
39
-function executeAfterWindowLoad(fn) {
40
-    if (windowIsLoaded) {
41
-        fn();
42
-    } else {
43
-        const loadHandler = () => {
44
-            fn();
45
-            window.removeEventListener('load', loadHandler);
46
-        };
47
-
48
-        window.addEventListener('load', loadHandler);
49
-    }
50
-}
51
 
33
 
52
 /**
34
 /**
53
  * Generates a deep linking URL based on the current window URL.
35
  * Generates a deep linking URL based on the current window URL.
114
  * @returns {void}
96
  * @returns {void}
115
  */
97
  */
116
 export function openDesktopApp() {
98
 export function openDesktopApp() {
117
-    executeAfterWindowLoad(() => {
99
+    windowLoadedPromise.then(() => {
118
         // If the code for opening the deep link is executed before the window
100
         // If the code for opening the deep link is executed before the window
119
         // load event, something with the internal chrome state goes wrong. The
101
         // load event, something with the internal chrome state goes wrong. The
120
         // result is that no window load event is received which is the cause
102
         // result is that no window load event is received which is the cause

正在加载...
取消
保存