|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.uimanager |
| 9 | + |
| 10 | +import android.util.SparseArray |
| 11 | +import android.util.SparseBooleanArray |
| 12 | +import android.view.View |
| 13 | +import com.facebook.react.common.SingleThreadAsserter |
| 14 | +import com.facebook.react.common.annotations.internal.LegacyArchitecture |
| 15 | +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogLevel |
| 16 | +import com.facebook.react.common.annotations.internal.LegacyArchitectureLogger |
| 17 | + |
| 18 | +/** |
| 19 | + * Simple container class to keep track of [ReactShadowNode]s associated with a particular |
| 20 | + * UIManagerModule instance. |
| 21 | + */ |
| 22 | +@LegacyArchitecture |
| 23 | +internal class ShadowNodeRegistry { |
| 24 | + private val tagsToCSSNodes = SparseArray<ReactShadowNode<*>>() |
| 25 | + private val rootTags = SparseBooleanArray() |
| 26 | + private val threadAsserter = SingleThreadAsserter() |
| 27 | + |
| 28 | + fun addRootNode(node: ReactShadowNode<*>) { |
| 29 | + threadAsserter.assertNow() |
| 30 | + val tag = node.reactTag |
| 31 | + tagsToCSSNodes.put(tag, node) |
| 32 | + rootTags.put(tag, true) |
| 33 | + } |
| 34 | + |
| 35 | + fun removeRootNode(tag: Int) { |
| 36 | + threadAsserter.assertNow() |
| 37 | + if (tag == View.NO_ID) { |
| 38 | + // This root node has already been removed (likely due to a threading issue caused by async js |
| 39 | + // execution). Ignore this root removal. |
| 40 | + return |
| 41 | + } |
| 42 | + if (!rootTags[tag]) { |
| 43 | + throw IllegalViewOperationException("View with tag $tag is not registered as a root view") |
| 44 | + } |
| 45 | + |
| 46 | + tagsToCSSNodes.remove(tag) |
| 47 | + rootTags.delete(tag) |
| 48 | + } |
| 49 | + |
| 50 | + fun addNode(node: ReactShadowNode<*>) { |
| 51 | + threadAsserter.assertNow() |
| 52 | + tagsToCSSNodes.put(node.reactTag, node) |
| 53 | + } |
| 54 | + |
| 55 | + fun removeNode(tag: Int) { |
| 56 | + threadAsserter.assertNow() |
| 57 | + if (rootTags[tag]) { |
| 58 | + throw IllegalViewOperationException( |
| 59 | + "Trying to remove root node $tag without using removeRootNode!") |
| 60 | + } |
| 61 | + tagsToCSSNodes.remove(tag) |
| 62 | + } |
| 63 | + |
| 64 | + fun getNode(tag: Int): ReactShadowNode<*>? { |
| 65 | + threadAsserter.assertNow() |
| 66 | + return tagsToCSSNodes[tag] |
| 67 | + } |
| 68 | + |
| 69 | + fun isRootNode(tag: Int): Boolean { |
| 70 | + threadAsserter.assertNow() |
| 71 | + return rootTags[tag] |
| 72 | + } |
| 73 | + |
| 74 | + val rootNodeCount: Int |
| 75 | + get() { |
| 76 | + threadAsserter.assertNow() |
| 77 | + return rootTags.size() |
| 78 | + } |
| 79 | + |
| 80 | + fun getRootTag(index: Int): Int { |
| 81 | + threadAsserter.assertNow() |
| 82 | + return rootTags.keyAt(index) |
| 83 | + } |
| 84 | + |
| 85 | + private companion object { |
| 86 | + init { |
| 87 | + LegacyArchitectureLogger.assertWhenLegacyArchitectureMinifyingEnabled( |
| 88 | + "ShadowNodeRegistry", LegacyArchitectureLogLevel.WARNING) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments