Fix Interactions with TreeObserver Manager moved to main thread

Reviewed By: lblasa

Differential Revision: D42608384

fbshipit-source-id: 20c074eeac1372405f44edc8eb8ab41cb7dd2be9
This commit is contained in:
Luke De Feo
2023-01-25 04:47:11 -08:00
committed by Facebook GitHub Bot
parent 73afa391f8
commit f996f90cf4
2 changed files with 8 additions and 9 deletions

View File

@@ -18,9 +18,6 @@ import com.facebook.flipper.plugins.uidebugger.descriptors.MetadataRegister
import com.facebook.flipper.plugins.uidebugger.model.InitEvent import com.facebook.flipper.plugins.uidebugger.model.InitEvent
import com.facebook.flipper.plugins.uidebugger.model.MetadataUpdateEvent import com.facebook.flipper.plugins.uidebugger.model.MetadataUpdateEvent
import com.facebook.flipper.plugins.uidebugger.observers.TreeObserverFactory import com.facebook.flipper.plugins.uidebugger.observers.TreeObserverFactory
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
const val LogTag = "ui-debugger" const val LogTag = "ui-debugger"
@@ -31,7 +28,6 @@ class UIDebuggerFlipperPlugin(
observerFactory: TreeObserverFactory? observerFactory: TreeObserverFactory?
) : FlipperPlugin { ) : FlipperPlugin {
val mainScope = CoroutineScope(Dispatchers.Main)
private val context: Context = private val context: Context =
Context( Context(
ApplicationRef(application), ApplicationRef(application),
@@ -65,7 +61,7 @@ class UIDebuggerFlipperPlugin(
MetadataUpdateEvent.serializer(), MetadataUpdateEvent.serializer(),
MetadataUpdateEvent(MetadataRegister.extractPendingMetadata()))) MetadataUpdateEvent(MetadataRegister.extractPendingMetadata())))
mainScope.launch { context.treeObserverManager.start() } context.treeObserverManager.start()
} }
@Throws(Exception::class) @Throws(Exception::class)
@@ -75,11 +71,9 @@ class UIDebuggerFlipperPlugin(
MetadataRegister.reset() MetadataRegister.reset()
mainScope.launch {
context.treeObserverManager.stop() context.treeObserverManager.stop()
context.bitmapPool.recycleAll() context.bitmapPool.recycleAll()
} }
}
override fun runInBackground(): Boolean { override fun runInBackground(): Boolean {
return false return false

View File

@@ -9,6 +9,7 @@ package com.facebook.flipper.plugins.uidebugger.observers
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Bitmap import android.graphics.Bitmap
import android.os.Looper
import android.util.Base64 import android.util.Base64
import android.util.Base64OutputStream import android.util.Base64OutputStream
import android.util.Log import android.util.Log
@@ -51,6 +52,7 @@ class TreeObserverManager(val context: Context) {
private var job: Job? = null private var job: Job? = null
private val workerScope = CoroutineScope(Dispatchers.IO) private val workerScope = CoroutineScope(Dispatchers.IO)
private val mainScope = CoroutineScope(Dispatchers.Main)
private val txId = AtomicInteger() private val txId = AtomicInteger()
fun enqueueUpdate(update: SubtreeUpdate) { fun enqueueUpdate(update: SubtreeUpdate) {
@@ -68,6 +70,9 @@ class TreeObserverManager(val context: Context) {
@SuppressLint("NewApi") @SuppressLint("NewApi")
fun start() { fun start() {
if (Looper.myLooper() != Looper.getMainLooper()) {
mainScope.launch { start() }
}
batchedUpdates = Channel(Channel.UNLIMITED) batchedUpdates = Channel(Channel.UNLIMITED)
rootObserver.subscribe(context.applicationRef) rootObserver.subscribe(context.applicationRef)