TreeObserver tidy up

Summary:
Remove usage of '!!', it is generally discourages even though instances are guaranteed to exist.

Adjust comments

Reviewed By: LukeDefeo

Differential Revision: D39575368

fbshipit-source-id: a159a0411a913de3d1ae6236c41ea15255687433
This commit is contained in:
Lorenzo Blasa
2022-09-20 05:15:50 -07:00
committed by Facebook GitHub Bot
parent 83f8a0675c
commit 7f103ee293

View File

@@ -12,18 +12,18 @@ import com.facebook.flipper.plugins.uidebugger.LogTag
import com.facebook.flipper.plugins.uidebugger.core.Context import com.facebook.flipper.plugins.uidebugger.core.Context
/* /*
Stateful class that manages some subtree in the UI Hierarchy. * Represents a stateful observer that manages some subtree in the UI Hierarchy.
It is responsible for: * It is responsible for:
1. listening to the relevant framework events * 1. Listening to the relevant framework events
2. Traversing the hierarchy of the managed nodes * 2. Traversing the hierarchy of the managed nodes
3. Diffing to previous state (optional) * 3. Diffing to previous state (optional)
4. Pushing out updates for its entire set of managed nodes * 4. Pushing out updates for its entire set of managed nodes
*
If while traversing it encounters a node type which has its own TreeObserver, it * If while traversing it encounters a node type which has its own TreeObserver, it
does not traverse that, instead it sets up a Tree observer responsible for that subtree * does not traverse that, instead it sets up a Tree observer responsible for that subtree
*
The parent is responsible for detecting when a child observer needs to be cleaned up * The parent is responsible for detecting when a child observer needs to be cleaned up
*/ */
abstract class TreeObserver<T> { abstract class TreeObserver<T> {
protected val children: MutableMap<Int, TreeObserver<*>> = mutableMapOf() protected val children: MutableMap<Int, TreeObserver<*>> = mutableMapOf()
@@ -44,14 +44,14 @@ abstract class TreeObserver<T> {
// Add any new observers // Add any new observers
for (observerRoot in observerRootsNodes) { for (observerRoot in observerRootsNodes) {
if (!children.containsKey(observerRoot.identityHashCode())) { if (!children.containsKey(observerRoot.identityHashCode())) {
val childObserver = context.observerFactory.createObserver(observerRoot, context)!! context.observerFactory.createObserver(observerRoot, context)?.let { childObserver ->
Log.d( Log.d(
LogTag, LogTag,
"For Observer ${this.type} discovered new child of type ${childObserver.type} Node ID ${observerRoot.identityHashCode()}") "Observer ${this.type} discovered new child of type ${childObserver.type} Node ID ${observerRoot.identityHashCode()}")
childObserver.subscribe(observerRoot) childObserver.subscribe(observerRoot)
children[observerRoot.identityHashCode()] = childObserver children[observerRoot.identityHashCode()] = childObserver
}
} }
} }
@@ -59,16 +59,17 @@ abstract class TreeObserver<T> {
val observerRootIds = observerRootsNodes.map { it.identityHashCode() } val observerRootIds = observerRootsNodes.map { it.identityHashCode() }
for (childKey in children.keys) { for (childKey in children.keys) {
if (!observerRootIds.contains(childKey)) { if (!observerRootIds.contains(childKey)) {
children[childKey]?.let { childObserver ->
Log.d(
LogTag,
"Observer ${this.type} cleaning up child of type ${childObserver.type} Node ID $childKey")
Log.d( childObserver.cleanUpRecursive()
LogTag, }
"For Observer ${this.type} cleaning up child of type ${children[childKey]!!.type} Node ID ${childKey}")
children[childKey]!!.cleanUpRecursive()
} }
} }
Log.d(LogTag, "For Observer ${this.type} Sending ${visitedNodes.size} ") Log.d(LogTag, "For Observer ${this.type} Sending ${visitedNodes.size}")
context.treeObserverManager.send( context.treeObserverManager.send(
SubtreeUpdate(type, visitedNodes, start, System.currentTimeMillis())) SubtreeUpdate(type, visitedNodes, start, System.currentTimeMillis()))
} }