getOrPut is not thread safe, so ensure that no multiple instance will be created per data store (#950)

This commit is contained in:
Benoit Marty
2023-07-24 15:01:48 +02:00
parent 3e4ff90e99
commit 3457a76446

View File

@@ -41,11 +41,13 @@ class DefaultUserPushStoreFactory @Inject constructor(
// We can have only one class accessing a single data store, so keep a cache of them.
private val cache = mutableMapOf<SessionId, UserPushStore>()
override fun create(userId: SessionId): UserPushStore {
return cache.getOrPut(userId) {
UserPushStoreDataStore(
context = context,
userId = userId
)
return synchronized(cache) {
cache.getOrPut(userId) {
UserPushStoreDataStore(
context = context,
userId = userId
)
}
}
}