From 7f103ee293145fe65a601b7f17618c73af8cb771 Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Tue, 20 Sep 2022 05:15:50 -0700 Subject: [PATCH] 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 --- .../uidebugger/observers/TreeObserver.kt | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt index 75a95e9bb..be59666b9 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt @@ -12,18 +12,18 @@ 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 { protected val children: MutableMap> = mutableMapOf() @@ -44,14 +44,14 @@ abstract class TreeObserver { // Add any new observers for (observerRoot in observerRootsNodes) { - if (!children.containsKey(observerRoot.identityHashCode())) { - val childObserver = context.observerFactory.createObserver(observerRoot, context)!! - Log.d( - LogTag, - "For Observer ${this.type} discovered new child of type ${childObserver.type} Node ID ${observerRoot.identityHashCode()}") - childObserver.subscribe(observerRoot) - children[observerRoot.identityHashCode()] = childObserver + context.observerFactory.createObserver(observerRoot, context)?.let { childObserver -> + Log.d( + LogTag, + "Observer ${this.type} discovered new child of type ${childObserver.type} Node ID ${observerRoot.identityHashCode()}") + childObserver.subscribe(observerRoot) + children[observerRoot.identityHashCode()] = childObserver + } } } @@ -59,16 +59,17 @@ abstract class TreeObserver { val observerRootIds = observerRootsNodes.map { it.identityHashCode() } for (childKey in children.keys) { 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( - LogTag, - "For Observer ${this.type} cleaning up child of type ${children[childKey]!!.type} Node ID ${childKey}") - - children[childKey]!!.cleanUpRecursive() + childObserver.cleanUpRecursive() + } } } - Log.d(LogTag, "For Observer ${this.type} Sending ${visitedNodes.size} ") + Log.d(LogTag, "For Observer ${this.type} Sending ${visitedNodes.size}") context.treeObserverManager.send( SubtreeUpdate(type, visitedNodes, start, System.currentTimeMillis())) }