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