Create a holder

This commit is contained in:
Benoit Marty 2021-04-19 16:07:08 +02:00 committed by Benoit Marty
parent ecd2d0fb76
commit dc69d5c68a
2 changed files with 42 additions and 6 deletions

View File

@ -84,6 +84,7 @@ import kotlin.coroutines.CoroutineContext
@SessionScope
internal class DefaultSession @Inject constructor(
override val sessionParams: SessionParams,
private val sessionCoroutineScopeHolder: SessionCoroutineScopeHolder,
private val workManagerProvider: WorkManagerProvider,
private val globalErrorHandler: GlobalErrorHandler,
@SessionId
@ -161,13 +162,11 @@ internal class DefaultSession @Inject constructor(
override val isOpenable: Boolean
get() = sessionParamsStore.get(sessionId)?.isTokenValid ?: false
private var sessionScope: CoroutineScope? = null
@MainThread
override fun open() {
assert(!isOpen)
isOpen = true
sessionScope = CoroutineScope(SupervisorJob())
sessionCoroutineScopeHolder.start()
cryptoService.get().ensureDevice()
uiHandler.post {
lifecycleObservers.forEach { it.onSessionStarted() }
@ -208,8 +207,7 @@ internal class DefaultSession @Inject constructor(
override fun close() {
assert(isOpen)
sessionScope?.coroutineContext?.cancelChildren(CancellationException("Closing session"))
sessionScope = null
sessionCoroutineScopeHolder.stop()
stopSync()
// timelineEventDecryptor.destroy()
uiHandler.post {
@ -315,7 +313,7 @@ internal class DefaultSession @Inject constructor(
override fun launch(context: CoroutineContext,
block: suspend () -> Unit) {
sessionScope?.launch(context) {
sessionCoroutineScopeHolder.scope?.launch(context) {
block()
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
@SessionScope
internal class SessionCoroutineScopeHolder {
var scope: CoroutineScope? = null
private set
fun start() {
scope = CoroutineScope(SupervisorJob())
}
fun stop() {
scope?.coroutineContext?.cancelChildren(CancellationException("Closing session"))
scope = null
}
}