Use bufferedMutableSharedFlow where appropriate (#476)

This commit is contained in:
Brian Yencho
2024-01-02 22:08:06 -06:00
committed by Álison Fernandes
parent b3bee9ae20
commit 0d3038a717
34 changed files with 92 additions and 170 deletions

View File

@@ -4,6 +4,7 @@ import android.content.SharedPreferences
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
import com.x8bit.bitwarden.data.platform.datasource.disk.BaseDiskSource
import com.x8bit.bitwarden.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.vault.datasource.network.model.SyncResponseJson
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
@@ -58,10 +59,7 @@ class AuthDiskSourceImpl(
get() = mutableUserStateFlow
.onSubscription { emit(userState) }
private val mutableUserStateFlow = MutableSharedFlow<UserStateJson?>(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
private val mutableUserStateFlow = bufferedMutableSharedFlow<UserStateJson?>(replay = 1)
override fun getUserKey(userId: String): String? =
getString(key = "${MASTER_KEY_ENCRYPTION_USER_KEY}_$userId")
@@ -132,9 +130,6 @@ class AuthDiskSourceImpl(
userId: String,
): MutableSharedFlow<List<SyncResponseJson.Profile.Organization>?> =
mutableOrganizationsFlowMap.getOrPut(userId) {
MutableSharedFlow(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
bufferedMutableSharedFlow(replay = 1)
}
}

View File

@@ -33,6 +33,7 @@ import com.x8bit.bitwarden.data.auth.util.KdfParamsConstants.DEFAULT_PBKDF2_ITER
import com.x8bit.bitwarden.data.auth.util.toSdkParams
import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.platform.util.asFailure
import com.x8bit.bitwarden.data.platform.util.flatMap
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
@@ -40,7 +41,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
@@ -124,7 +124,7 @@ class AuthRepositoryImpl constructor(
)
private val mutableCaptchaTokenFlow =
MutableSharedFlow<CaptchaCallbackTokenResult>(extraBufferCapacity = Int.MAX_VALUE)
bufferedMutableSharedFlow<CaptchaCallbackTokenResult>()
override val captchaTokenResultFlow: Flow<CaptchaCallbackTokenResult> =
mutableCaptchaTokenFlow.asSharedFlow()

View File

@@ -3,8 +3,8 @@ package com.x8bit.bitwarden.data.platform.datasource.disk
import android.content.SharedPreferences
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
import com.x8bit.bitwarden.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.onSubscription
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
@@ -33,8 +33,6 @@ class EnvironmentDiskSourceImpl(
get() = mutableEnvironmentUrlDataFlow
.onSubscription { emit(preAuthEnvironmentUrlData) }
private val mutableEnvironmentUrlDataFlow = MutableSharedFlow<EnvironmentUrlDataJson?>(
replay = 1,
extraBufferCapacity = Int.MAX_VALUE,
)
private val mutableEnvironmentUrlDataFlow =
bufferedMutableSharedFlow<EnvironmentUrlDataJson?>(replay = 1)
}

View File

@@ -3,7 +3,12 @@ package com.x8bit.bitwarden.data.platform.repository.util
import kotlinx.coroutines.flow.MutableSharedFlow
/**
* Creates a [MutableSharedFlow] with a buffer of [Int.MAX_VALUE].
* Creates a [MutableSharedFlow] with a buffer of [Int.MAX_VALUE] and the given [replay] count.
*/
fun <T> bufferedMutableSharedFlow(): MutableSharedFlow<T> =
MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE)
fun <T> bufferedMutableSharedFlow(
replay: Int = 0,
): MutableSharedFlow<T> =
MutableSharedFlow(
replay = replay,
extraBufferCapacity = Int.MAX_VALUE,
)