From fb8eab13d0f4eac6b0496be35b4dc0cbba6fde0c Mon Sep 17 00:00:00 2001 From: Brian Yencho Date: Mon, 18 Dec 2023 11:54:04 -0600 Subject: [PATCH] BIT-1229: Use Unconfined dispatcher when collecting flows in AuthRepository (#410) --- .../data/auth/repository/AuthRepositoryImpl.kt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt index 2863273a3c..99662e5799 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/repository/AuthRepositoryImpl.kt @@ -34,6 +34,7 @@ import com.x8bit.bitwarden.data.platform.util.asFailure import com.x8bit.bitwarden.data.platform.util.flatMap import com.x8bit.bitwarden.data.vault.repository.VaultRepository import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -62,7 +63,13 @@ class AuthRepositoryImpl constructor( ) : AuthRepository { private val mutableSpecialCircumstanceStateFlow = MutableStateFlow(null) - private val scope = CoroutineScope(dispatcherManager.io) + + /** + * A scope intended for use when simply collecting multiple flows in order to combine them. The + * use of [Dispatchers.Unconfined] allows for this to happen synchronously whenever any of + * these flows changes. + */ + private val collectionScope = CoroutineScope(dispatcherManager.unconfined) override val activeUserId: String? get() = authDiskSource.userState?.activeUserId @@ -81,7 +88,7 @@ class AuthRepositoryImpl constructor( ?: AuthState.Unauthenticated } .stateIn( - scope = scope, + scope = collectionScope, started = SharingStarted.Eagerly, initialValue = AuthState.Uninitialized, ) @@ -98,7 +105,7 @@ class AuthRepositoryImpl constructor( ) } .stateIn( - scope = scope, + scope = collectionScope, started = SharingStarted.Eagerly, initialValue = authDiskSource .userState