Add helper for concurrent map (#5086)

This commit is contained in:
David Perez 2025-04-23 14:53:05 -05:00 committed by GitHub
parent 36989875a6
commit 54983bc92e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import com.bitwarden.core.data.util.concurrentMapOf
import com.bitwarden.data.manager.DispatcherManager import com.bitwarden.data.manager.DispatcherManager
import com.x8bit.bitwarden.data.platform.datasource.disk.SettingsDiskSource import com.x8bit.bitwarden.data.platform.datasource.disk.SettingsDiskSource
import com.x8bit.bitwarden.data.platform.datasource.disk.model.FlightRecorderDataSet import com.x8bit.bitwarden.data.platform.datasource.disk.model.FlightRecorderDataSet
@ -23,7 +24,6 @@ import timber.log.Timber
import java.time.Clock import java.time.Clock
import java.time.temporal.ChronoUnit import java.time.temporal.ChronoUnit
import java.util.UUID import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
private const val EXPIRATION_DURATION_DAYS: Long = 30 private const val EXPIRATION_DURATION_DAYS: Long = 30
@ -40,7 +40,7 @@ internal class FlightRecorderManagerImpl(
private val unconfinedScope = CoroutineScope(context = dispatcherManager.unconfined) private val unconfinedScope = CoroutineScope(context = dispatcherManager.unconfined)
private val ioScope = CoroutineScope(context = dispatcherManager.io) private val ioScope = CoroutineScope(context = dispatcherManager.io)
private var cancellationJob: Job = Job().apply { complete() } private var cancellationJob: Job = Job().apply { complete() }
private val expirationJobMap: ConcurrentHashMap<String, Job> = ConcurrentHashMap() private val expirationJobMap: MutableMap<String, Job> = concurrentMapOf()
private val flightRecorderTree = FlightRecorderTree() private val flightRecorderTree = FlightRecorderTree()
override val flightRecorderData: FlightRecorderDataSet override val flightRecorderData: FlightRecorderDataSet

View File

@ -9,6 +9,7 @@ import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.InitUserCryptoRequest import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow import com.bitwarden.core.data.repository.util.bufferedMutableSharedFlow
import com.bitwarden.core.data.util.asSuccess import com.bitwarden.core.data.util.asSuccess
import com.bitwarden.core.data.util.concurrentMapOf
import com.bitwarden.core.data.util.flatMap import com.bitwarden.core.data.util.flatMap
import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.HashPurpose
import com.bitwarden.crypto.Kdf import com.bitwarden.crypto.Kdf
@ -58,7 +59,6 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.time.Clock import java.time.Clock
import java.util.concurrent.ConcurrentHashMap
import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.minutes
/** /**
@ -88,7 +88,7 @@ class VaultLockManagerImpl(
* This [Map] tracks all active timeout [Job]s that are running and their associated data using * This [Map] tracks all active timeout [Job]s that are running and their associated data using
* the user ID as the key. * the user ID as the key.
*/ */
private val userIdTimerJobMap: MutableMap<String, TimeoutJobData> = ConcurrentHashMap() private val userIdTimerJobMap: MutableMap<String, TimeoutJobData> = concurrentMapOf()
private val activeUserId: String? get() = authDiskSource.userState?.activeUserId private val activeUserId: String? get() = authDiskSource.userState?.activeUserId

View File

@ -8,10 +8,10 @@ import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Rect
import com.bitwarden.core.data.util.concurrentMapOf
import com.x8bit.bitwarden.ui.platform.components.coachmark.model.CoachMarkHighlightShape import com.x8bit.bitwarden.ui.platform.components.coachmark.model.CoachMarkHighlightShape
import com.x8bit.bitwarden.ui.platform.components.coachmark.model.CoachMarkHighlightState import com.x8bit.bitwarden.ui.platform.components.coachmark.model.CoachMarkHighlightState
import com.x8bit.bitwarden.ui.platform.components.tooltip.BitwardenToolTipState import com.x8bit.bitwarden.ui.platform.components.tooltip.BitwardenToolTipState
import java.util.concurrent.ConcurrentHashMap
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -33,7 +33,7 @@ open class CoachMarkState<T : Enum<T>>(
initialCoachMarkHighlight: T? = null, initialCoachMarkHighlight: T? = null,
isCoachMarkVisible: Boolean = false, isCoachMarkVisible: Boolean = false,
) { ) {
private val highlights: MutableMap<T, CoachMarkHighlightState<T>?> = ConcurrentHashMap() private val highlights: MutableMap<T, CoachMarkHighlightState<T>?> = concurrentMapOf()
private val mutableCurrentHighlight = mutableStateOf(initialCoachMarkHighlight) private val mutableCurrentHighlight = mutableStateOf(initialCoachMarkHighlight)
val currentHighlight: State<T?> = mutableCurrentHighlight val currentHighlight: State<T?> = mutableCurrentHighlight
private val mutableCurrentHighlightBounds = mutableStateOf(Rect.Zero) private val mutableCurrentHighlightBounds = mutableStateOf(Rect.Zero)

View File

@ -0,0 +1,13 @@
@file:OmitFromCoverage
package com.bitwarden.core.data.util
import com.bitwarden.core.annotation.OmitFromCoverage
import java.util.concurrent.ConcurrentHashMap
/**
* Creates a thread-safe [MutableMap].
*/
fun <T, R> concurrentMapOf(
vararg items: Pair<T, R>,
): MutableMap<T, R> = ConcurrentHashMap<T, R>().apply { this.putAll(items) }