@onekeyfe/react-native-chart-webview 3.0.50 → 3.0.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,6 +97,7 @@ class PooledChartWebView private constructor(
97
97
  // How long the snapshot overlay stays up after a reparent, giving the WebView
98
98
  // time to draw its first frame in the new container (~5 frames).
99
99
  private val overlayHideDelayMs = 80L
100
+ private val attachGeneration = AtomicInteger(0)
100
101
 
101
102
  val webView: WebView = WebView(context).apply {
102
103
  settings.javaScriptEnabled = true
@@ -154,21 +155,41 @@ class PooledChartWebView private constructor(
154
155
 
155
156
  /** Move the WebView into [container], detaching it from any previous parent. */
156
157
  fun attachTo(container: ViewGroup) {
158
+ val generation = attachGeneration.incrementAndGet()
157
159
  runOnUiThread {
158
- if (webView.parent === container) return@runOnUiThread
159
- (webView.parent as? ViewGroup)?.removeView(webView)
160
- container.addView(
161
- webView,
162
- FrameLayout.LayoutParams(
163
- FrameLayout.LayoutParams.MATCH_PARENT,
164
- FrameLayout.LayoutParams.MATCH_PARENT,
165
- ),
166
- )
167
- // Mask the reparent's blank frame with the last captured frame, then
168
- // refresh the cache (async) so the next move has a current frame.
169
- showSnapshotOverlay(container)
170
- refreshSnapshotSoon()
160
+ attachToContainer(container, generation, canRetry = true)
161
+ }
162
+ }
163
+
164
+ private fun attachToContainer(container: ViewGroup, generation: Int, canRetry: Boolean) {
165
+ if (generation != attachGeneration.get()) return
166
+ if (webView.parent === container) return
167
+
168
+ (webView.parent as? ViewGroup)?.removeView(webView)
169
+ val currentParent = webView.parent
170
+ if (currentParent != null) {
171
+ if (canRetry) {
172
+ webView.post { attachToContainer(container, generation, canRetry = false) }
173
+ } else {
174
+ android.util.Log.w(
175
+ "ChartWebviewPool",
176
+ "Skip attach key=$key because WebView parent was not cleared: $currentParent",
177
+ )
178
+ }
179
+ return
171
180
  }
181
+
182
+ container.addView(
183
+ webView,
184
+ FrameLayout.LayoutParams(
185
+ FrameLayout.LayoutParams.MATCH_PARENT,
186
+ FrameLayout.LayoutParams.MATCH_PARENT,
187
+ ),
188
+ )
189
+ // Mask the reparent's blank frame with the last captured frame, then
190
+ // refresh the cache (async) so the next move has a current frame.
191
+ showSnapshotOverlay(container)
192
+ refreshSnapshotSoon()
172
193
  }
173
194
 
174
195
  /** Remove the WebView from its current parent (keeps it alive, warm). */
@@ -330,6 +330,7 @@ final class PooledChartWebView {
330
330
  private var webView: WKWebView!
331
331
  private var proxy: ChartWebViewProxy!
332
332
  private var lastLoadedUrl: String?
333
+ private var attachGeneration = 0
333
334
 
334
335
  /// Read by the scheme handler to resolve offline files.
335
336
  fileprivate var currentLocalBundle: String?
@@ -405,8 +406,11 @@ final class PooledChartWebView {
405
406
 
406
407
  /// Move the WebView into `container`, detaching it from any previous parent.
407
408
  func attach(to container: UIView) {
409
+ attachGeneration += 1
410
+ let generation = attachGeneration
408
411
  runOnMain { [weak self] in
409
412
  guard let self = self, let webView = self.webView else { return }
413
+ guard generation == self.attachGeneration else { return }
410
414
  guard webView.superview !== container else { return }
411
415
  webView.removeFromSuperview()
412
416
  webView.frame = container.bounds
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-chart-webview",
3
- "version": "3.0.50",
3
+ "version": "3.0.52",
4
4
  "description": "react-native-chart-webview",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",