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,17 +12,17 @@ import com.facebook.flipper.plugins.uidebugger.LogTag
import com.facebook.flipper.plugins.uidebugger.core.Context
/*
Stateful class that manages some subtree in the UI Hierarchy.
It is responsible for:
1. listening to the relevant framework events
2. Traversing the hierarchy of the managed nodes
3. Diffing to previous state (optional)
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
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
* Represents a stateful observer that manages some subtree in the UI Hierarchy.
* It is responsible for:
* 1. Listening to the relevant framework events
* 2. Traversing the hierarchy of the managed nodes
* 3. Diffing to previous state (optional)
* 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
* 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
*/
abstract class TreeObserver<T> {
@@ -44,27 +44,28 @@ abstract class TreeObserver<T> {
// Add any new observers
for (observerRoot in observerRootsNodes) {
if (!children.containsKey(observerRoot.identityHashCode())) {
val childObserver = context.observerFactory.createObserver(observerRoot, context)!!
context.observerFactory.createObserver(observerRoot, context)?.let { childObserver ->
Log.d(
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)
children[observerRoot.identityHashCode()] = childObserver
}
}
}
// Remove any old observers
val observerRootIds = observerRootsNodes.map { it.identityHashCode() }
for (childKey in children.keys) {
if (!observerRootIds.contains(childKey)) {
children[childKey]?.let { childObserver ->
Log.d(
LogTag,
"For Observer ${this.type} cleaning up child of type ${children[childKey]!!.type} Node ID ${childKey}")
"Observer ${this.type} cleaning up child of type ${childObserver.type} Node ID $childKey")
children[childKey]!!.cleanUpRecursive()
childObserver.cleanUpRecursive()
}
}
}