Browse Source

Added additional height check with delay to fix issues with third party embeds

Vladimir Panov 4 years ago
parent
commit
6b7eacaa51
1 changed files with 24 additions and 0 deletions
  1. 24
    0
      autoHeightWebView/utils.js

+ 24
- 0
autoHeightWebView/utils.js View File

@@ -15,6 +15,13 @@ const updateSizeWithMessage = element =>
15 15
   `
16 16
   var updateSizeInterval = null;
17 17
   var height = 0;
18
+
19
+  var lastHeight = 0;
20
+  var heightTheSameTimes = 0;
21
+  var maxHeightTheSameTimes = 5;
22
+  var forceRefreshDelay = 1000;
23
+  var forceRefreshTimeout;
24
+
18 25
   function updateSize(event) {
19 26
     if (!window.hasOwnProperty('ReactNativeWebView') || !window.ReactNativeWebView.hasOwnProperty('postMessage')) {
20 27
       !updateSizeInterval && (updateSizeInterval = setInterval(updateSize, 200));
@@ -24,6 +31,23 @@ const updateSizeWithMessage = element =>
24 31
     height = ${element}.offsetHeight || window.innerHeight;
25 32
     width = ${element}.offsetWidth || window.innerWidth;
26 33
     window.ReactNativeWebView.postMessage(JSON.stringify({ width: width, height: height }));
34
+
35
+    // Make additional height checks (required to fix issues wit twitter embeds)
36
+    clearTimeout(forceRefreshTimeout);
37
+    if (lastHeight !== height) {
38
+      heightTheSameTimes = 1;
39
+    } else {
40
+      heightTheSameTimes++;
41
+    }
42
+
43
+    lastHeight = height;
44
+
45
+    if (heightTheSameTimes <= maxHeightTheSameTimes) {
46
+      forceRefreshTimeout = setTimeout(
47
+        updateSize,
48
+        heightTheSameTimes * forceRefreshDelay
49
+      );
50
+    }
27 51
   }
28 52
   `;
29 53