Files
divkit/client/ios/DivKit/DivLastVisibleBoundsCache.swift
T
pkurchatov 5d2074dce9 Public API clean up, added @_spi annotations
commit_hash:8f0bdad94d4fabf3f89fb4bcb319a2cda916f679
2025-01-16 17:54:32 +03:00

61 lines
1.2 KiB
Swift

import CoreGraphics
import LayoutKit
import VGSL
@_spi(Internal)
public final class DivLastVisibleBoundsCache {
private let lock = AllocatedUnfairLock()
private var storage: [UIElementPath: Int] = [:]
private var invisibleElements = Set<UIElementPath>()
init() {}
func lastVisibleArea(for path: UIElementPath) -> Int {
lock.withLock {
storage[path] ?? .zero
}
}
func updateLastVisibleArea(for path: UIElementPath, area: Int) {
lock.withLock {
storage[path] = area
}
}
func dropVisibleBounds(prefix: UIElementPath) {
lock.withLock {
_dropVisibleBounds(prefix: prefix)
}
}
func onBecomeVisible(_ path: UIElementPath) {
lock.withLock {
_ = invisibleElements.remove(path)
}
}
func onBecomeInvisible(_ path: UIElementPath) {
lock.withLock {
if !invisibleElements.contains(path) {
_dropVisibleBounds(prefix: path)
invisibleElements.insert(path)
}
}
}
func reset() {
lock.withLock {
storage.removeAll()
}
}
private func _dropVisibleBounds(prefix: UIElementPath) {
for (path, _) in storage {
if path.starts(with: prefix) {
storage[path] = nil
}
}
}
}