Fix active child

Summary: The active child was only being passed for the native scan full traversal, Now we have it for the partial traversal also

Reviewed By: lblasa

Differential Revision: D39466933

fbshipit-source-id: a0e281e4f9a21bf2edd12f18ecdb68a29ead3476
This commit is contained in:
Luke De Feo
2022-09-13 11:05:42 -07:00
committed by Facebook GitHub Bot
parent 0562178739
commit a0ee774159

View File

@@ -132,6 +132,11 @@ fun Any.identityHashCode(): HashCode {
return System.identityHashCode(this)
}
/**
* This will traverse the layout hierarchy untill it sees a node that has an observer registered for
* it. The first item in the pair is the visited nodes The second item are any observable roots
* discovered
*/
class PartialLayoutTraversal(
private val descriptorRegister: DescriptorRegister,
private val treeObserverfactory: TreeObserverFactory,
@@ -142,7 +147,7 @@ class PartialLayoutTraversal(
fun traverse(root: Any): Pair<MutableList<Node>, List<Any>> {
val visited = mutableListOf<Node>()
val skipped = mutableListOf<Any>()
val observableRoots = mutableListOf<Any>()
val stack = mutableListOf<Any>()
stack.add(root)
@@ -154,7 +159,7 @@ class PartialLayoutTraversal(
// if we encounter a node that has it own observer, dont traverse
if (node != root && treeObserverfactory.hasObserverFor(node)) {
skipped.add(node)
observableRoots.add(node)
continue
}
@@ -164,26 +169,42 @@ class PartialLayoutTraversal(
descriptor.getChildren(node, children)
val childrenIds = mutableListOf<String>()
val activeChild = descriptor.getActiveChild(node)
for (child in children) {
// it might make sense one day to remove id from the descriptor since its always the
// hash code
val childDescriptor =
descriptorRegister.descriptorForClassUnsafe(child::class.java).asAny()
childrenIds.add(childDescriptor.getId(child))
// if there is an active child then dont traverse it
if (activeChild == null) {
stack.add(child)
}
}
var activeChildId: String? = null
if (activeChild != null) {
stack.add(activeChild)
activeChildId =
descriptorRegister.descriptorForClassUnsafe(activeChild.javaClass).getId(activeChild)
}
val attributes = mutableMapOf<String, InspectableObject>()
descriptor.getData(node, attributes)
// NOTE active child null here
visited.add(
Node(descriptor.getId(node), descriptor.getName(node), attributes, childrenIds, null))
Node(
descriptor.getId(node),
descriptor.getName(node),
attributes,
childrenIds,
activeChildId))
} catch (exception: Exception) {
Log.e(LogTag, "Error while processing node ${node.javaClass.name} ${node} ", exception)
}
}
return Pair(visited, skipped)
return Pair(visited, observableRoots)
}
}