Reuse coroutine in DefaultAnalyticsRoomListStateWatcher instead of creating and cancelling the same one

This commit is contained in:
Jorge Martín
2025-11-28 08:22:36 +01:00
committed by Jorge Martin Espinosa
parent f098c5e027
commit 568c1ae90d

View File

@@ -20,7 +20,7 @@ import io.element.android.services.analytics.api.finishLongRunningTransaction
import io.element.android.services.analytics.api.watchers.AnalyticsRoomListStateWatcher import io.element.android.services.analytics.api.watchers.AnalyticsRoomListStateWatcher
import io.element.android.services.appnavstate.api.AppNavigationStateService import io.element.android.services.appnavstate.api.AppNavigationStateService
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@@ -33,19 +33,19 @@ class DefaultAnalyticsRoomListStateWatcher(
private val appNavigationStateService: AppNavigationStateService, private val appNavigationStateService: AppNavigationStateService,
private val roomListService: RoomListService, private val roomListService: RoomListService,
private val analyticsService: AnalyticsService, private val analyticsService: AnalyticsService,
@SessionCoroutineScope private val sessionCoroutineScope: CoroutineScope, @SessionCoroutineScope sessionCoroutineScope: CoroutineScope,
private val dispatchers: CoroutineDispatchers, dispatchers: CoroutineDispatchers,
) : AnalyticsRoomListStateWatcher { ) : AnalyticsRoomListStateWatcher {
private var currentCoroutineScope: CoroutineScope? = null private val coroutineScope: CoroutineScope = sessionCoroutineScope.childScope(dispatchers.computation, "AnalyticsRoomListStateWatcher")
private val isStarted = AtomicBoolean(false)
private val isWarmState = AtomicBoolean(false) private val isWarmState = AtomicBoolean(false)
override fun start() { override fun start() {
if (currentCoroutineScope != null) { if (isStarted.getAndSet(true)) {
Timber.w("Can't start RoomListStateWatcher, it's already running.") Timber.w("Can't start RoomListStateWatcher, it's already running.")
return return
} }
val coroutineScope = sessionCoroutineScope.childScope(dispatchers.computation, "AnalyticsRoomListStateWatcher")
appNavigationStateService.appNavigationState appNavigationStateService.appNavigationState
.map { it.isInForeground } .map { it.isInForeground }
.distinctUntilChanged() .distinctUntilChanged()
@@ -68,12 +68,11 @@ class DefaultAnalyticsRoomListStateWatcher(
} }
} }
.launchIn(coroutineScope) .launchIn(coroutineScope)
currentCoroutineScope = coroutineScope
} }
override fun stop() { override fun stop() {
currentCoroutineScope?.cancel() if (isStarted.getAndSet(false)) {
currentCoroutineScope = null coroutineScope.coroutineContext.cancelChildren()
}
} }
} }