Updated on 2026-08-14

This commit is contained in:
Tangem 2023-10-18 23:46:37 +08:00
parent 40e24712d5
commit 51d37ed837

View file

@ -5,20 +5,26 @@ import com.tangem.datasource.local.datastore.core.StringKeyDataStoreDecorator
import com.tangem.domain.tokens.model.NetworkStatus
import com.tangem.domain.wallets.models.UserWalletId
import com.tangem.utils.extensions.addOrReplace
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
internal class DefaultNetworksStatusesStore(
dataStore: StringKeyDataStore<Set<NetworkStatus>>,
) : NetworksStatusesStore, StringKeyDataStoreDecorator<UserWalletId, Set<NetworkStatus>>(dataStore) {
private val mutex = Mutex()
override fun provideStringKey(key: UserWalletId): String {
return key.stringValue
}
override suspend fun store(key: UserWalletId, value: NetworkStatus) {
val newValues = getSyncOrNull(key)
?.addOrReplace(value) { it.network == value.network }
?: setOf(value)
mutex.withLock {
val newValues = getSyncOrNull(key)
?.addOrReplace(value) { it.network == value.network }
?: setOf(value)
store(key, newValues)
store(key, newValues)
}
}
}