Fix flipper connect / disconnect cycle

Summary: Previously we were cancelling the entire context which meant after reconnect nothing was sent. Additionally we now close / reinitiaze the channel so that any old events are not sent on reconnect

Reviewed By: lblasa

Differential Revision: D39658945

fbshipit-source-id: bb02724434aa820d811b49ab799a4643ab7e785a
This commit is contained in:
Luke De Feo
2022-09-21 07:02:48 -07:00
committed by Facebook GitHub Bot
parent cb75be7495
commit 46824f3369
2 changed files with 43 additions and 38 deletions

View File

@@ -46,6 +46,7 @@ class UIDebuggerFlipperPlugin(
@Throws(Exception::class)
override fun onConnect(connection: FlipperConnection) {
Log.i(LogTag, "Connected")
this.context.connectionRef.connection = connection
val rootDescriptor =
@@ -57,15 +58,14 @@ class UIDebuggerFlipperPlugin(
InitEvent.serializer(), InitEvent(rootDescriptor.getId(context.applicationRef))))
context.treeObserverManager.start()
// nativeScanScheduler.start()
}
@Throws(Exception::class)
override fun onDisconnect() {
this.context.connectionRef.connection = null
Log.e(LogTag, "disconnected")
Log.i(LogTag, "Disconnected")
this.nativeScanScheduler.stop()
context.treeObserverManager.stop()
}
override fun runInBackground(): Boolean {

View File

@@ -29,7 +29,8 @@ data class SubtreeUpdate(
class TreeObserverManager(val context: Context) {
private val rootObserver = ApplicationTreeObserver(context)
private val treeUpdates = Channel<SubtreeUpdate>(Channel.UNLIMITED)
private lateinit var treeUpdates: Channel<SubtreeUpdate>
private var job: Job? = null
private val workerScope = CoroutineScope(Dispatchers.IO)
private val txId = AtomicInteger()
@@ -43,8 +44,10 @@ class TreeObserverManager(val context: Context) {
*/
fun start() {
treeUpdates = Channel(Channel.UNLIMITED)
rootObserver.subscribe(context.applicationRef)
job =
workerScope.launch {
while (isActive) {
try {
@@ -63,7 +66,9 @@ class TreeObserverManager(val context: Context) {
context.connectionRef.connection?.send(SubtreeUpdateEvent.name, serialized)
val socketEnd = System.currentTimeMillis()
Log.i(LogTag, "Sent event for ${treeUpdate.observerType} nodes ${treeUpdate.nodes.size}")
Log.i(
LogTag,
"Sent event for ${treeUpdate.observerType} nodes ${treeUpdate.nodes.size}")
val perfStats =
PerfStatsEvent(
@@ -77,18 +82,18 @@ class TreeObserverManager(val context: Context) {
nodesCount = treeUpdate.nodes.size)
context.connectionRef.connection?.send(
PerfStatsEvent.name, Json.encodeToString(PerfStatsEvent.serializer(), perfStats))
} catch (e: java.lang.Exception) {
Log.e(LogTag, "Error in channel ", e)
} catch (e: CancellationException) {} catch (e: java.lang.Exception) {
Log.e(LogTag, "Unexpected Error in channel ", e)
}
}
Log.i(LogTag, "shutting down worker")
Log.i(LogTag, "Shutting down worker")
}
}
fun stop() {
rootObserver.cleanUpRecursive()
treeUpdates.close()
workerScope.cancel()
job?.cancel()
treeUpdates.cancel()
}
}