mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 20:06:51 +03:00
replacing single context thread with semaphore
- avoids the need for a dedicated long living thread instance
This commit is contained in:
parent
3725921400
commit
9114630bba
1 changed files with 4 additions and 6 deletions
|
@ -16,15 +16,13 @@
|
||||||
|
|
||||||
package im.vector.app.core.di
|
package im.vector.app.core.di
|
||||||
|
|
||||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
import kotlinx.coroutines.newSingleThreadContext
|
import kotlinx.coroutines.sync.withPermit
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import org.matrix.android.sdk.api.auth.AuthenticationService
|
import org.matrix.android.sdk.api.auth.AuthenticationService
|
||||||
import org.matrix.android.sdk.api.session.Session
|
import org.matrix.android.sdk.api.session.Session
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@OptIn(DelicateCoroutinesApi::class)
|
private val initializerSemaphore = Semaphore(permits = 1)
|
||||||
private val INITIALIZER_CONTEXT = newSingleThreadContext("session-initializer")
|
|
||||||
|
|
||||||
class SessionInitializer @Inject constructor(
|
class SessionInitializer @Inject constructor(
|
||||||
private val authenticationService: AuthenticationService,
|
private val authenticationService: AuthenticationService,
|
||||||
|
@ -38,7 +36,7 @@ class SessionInitializer @Inject constructor(
|
||||||
* @return the initialized Session or null when no authenticated sessions are available.
|
* @return the initialized Session or null when no authenticated sessions are available.
|
||||||
*/
|
*/
|
||||||
suspend fun tryInitialize(readCurrentSession: () -> Session?, initializer: (Session) -> Unit): Session? {
|
suspend fun tryInitialize(readCurrentSession: () -> Session?, initializer: (Session) -> Unit): Session? {
|
||||||
return withContext(INITIALIZER_CONTEXT) {
|
return initializerSemaphore.withPermit {
|
||||||
val currentInMemorySession = readCurrentSession()
|
val currentInMemorySession = readCurrentSession()
|
||||||
when {
|
when {
|
||||||
currentInMemorySession != null -> currentInMemorySession
|
currentInMemorySession != null -> currentInMemorySession
|
||||||
|
|
Loading…
Reference in a new issue