Use ConcurrentHashMap to manage synchronization.
This commit is contained in:
parent
754b4647ee
commit
da9f52129f
1 changed files with 7 additions and 8 deletions
|
|
@ -26,6 +26,7 @@ import io.element.android.libraries.pushstore.api.UserPushStore
|
||||||
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
|
||||||
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
|
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
|
||||||
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
|
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@SingleIn(AppScope::class)
|
@SingleIn(AppScope::class)
|
||||||
|
|
@ -39,15 +40,13 @@ 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 = ConcurrentHashMap<SessionId, UserPushStore>()
|
||||||
override fun create(userId: SessionId): UserPushStore {
|
override fun create(userId: SessionId): UserPushStore {
|
||||||
return synchronized(cache) {
|
return cache.getOrPut(userId) {
|
||||||
cache.getOrPut(userId) {
|
UserPushStoreDataStore(
|
||||||
UserPushStoreDataStore(
|
context = context,
|
||||||
context = context,
|
userId = userId
|
||||||
userId = userId
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue