From dc69d5c68a900b47b0b98532c78e6430e2e7a0ca Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 19 Apr 2021 16:07:08 +0200 Subject: [PATCH] Create a holder --- .../sdk/internal/session/DefaultSession.kt | 10 ++--- .../session/SessionCoroutineScopeHolder.kt | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionCoroutineScopeHolder.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 58be33460b..3c1a461861 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -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() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionCoroutineScopeHolder.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionCoroutineScopeHolder.kt new file mode 100644 index 0000000000..0d9e31f8cc --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/SessionCoroutineScopeHolder.kt @@ -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 + } +}