fix NoSuchElementException at Div2View.trackChildrenVisibility() call

commit_hash:10a13ec650266f4ccb735838fe6fa57bbb798e97
This commit is contained in:
gulevsky
2026-03-13 15:07:01 +03:00
parent 9cb98b7696
commit c01ca4e85c
2 changed files with 21 additions and 2 deletions
@@ -87,6 +87,7 @@ import com.yandex.div.internal.core.VariableMutationHandler
import com.yandex.div.internal.util.UiThreadHandler.Companion.executeOnMainThreadBlocking
import com.yandex.div.internal.util.hasScrollableChildUnder
import com.yandex.div.internal.util.immutableCopy
import com.yandex.div.internal.util.toMapSafe
import com.yandex.div.internal.widget.FrameContainerLayout
import com.yandex.div.internal.widget.menu.OverflowMenuSubscriber
import com.yandex.div.json.expressions.ExpressionResolver
@@ -636,7 +637,7 @@ class Div2View private constructor(
fun trackChildrenVisibility(): Unit = bindingDispatcher.runWithinBindingContext {
val visibilityActionTracker = div2Component.visibilityActionTracker
val bindingsSnapshot = synchronized(viewToDivBindings) {
viewToDivBindings.toMap()
viewToDivBindings.toMapSafe()
}
bindingsSnapshot.forEach { (view, div) ->
view.bindingContext?.expressionResolver?.let {
@@ -652,7 +653,7 @@ class Div2View private constructor(
private fun discardChildrenVisibility() {
val visibilityActionTracker = div2Component.visibilityActionTracker
val bindingsSnapshot = synchronized(viewToDivBindings) {
viewToDivBindings.toMap()
viewToDivBindings.toMapSafe()
}
bindingsSnapshot.forEach { (view, div) ->
view.bindingContext?.expressionResolver?.let {
@@ -3,6 +3,7 @@ package com.yandex.div.internal.util
import androidx.collection.ArrayMap
import com.yandex.div.core.annotations.InternalApi
import java.util.Collections
import java.util.WeakHashMap
@InternalApi
public fun <K, V> arrayMap(): MutableMap<K, V> = ArrayMap<K, V>()
@@ -63,3 +64,20 @@ public inline fun <T> List<T>?.compareNullableWith(other: List<T>?, comparator:
return compareWith(other, comparator)
}
@InternalApi
public fun <K, V> WeakHashMap<out K, V>.toMapSafe(): Map<K, V> {
if (isEmpty()) {
return emptyMap()
}
val result = mutableMapOf<K, V>()
val iterator = entries.iterator()
try {
while (iterator.hasNext()) {
val entry = iterator.next()
result[entry.key] = entry.value
}
} catch (e: NoSuchElementException) { }
return result
}