mirror of
https://github.com/vector-im/element-android.git
synced 2024-11-15 01:35:07 +08:00
Create a holder
This commit is contained in:
parent
ecd2d0fb76
commit
dc69d5c68a
@ -84,6 +84,7 @@ import kotlin.coroutines.CoroutineContext
|
|||||||
@SessionScope
|
@SessionScope
|
||||||
internal class DefaultSession @Inject constructor(
|
internal class DefaultSession @Inject constructor(
|
||||||
override val sessionParams: SessionParams,
|
override val sessionParams: SessionParams,
|
||||||
|
private val sessionCoroutineScopeHolder: SessionCoroutineScopeHolder,
|
||||||
private val workManagerProvider: WorkManagerProvider,
|
private val workManagerProvider: WorkManagerProvider,
|
||||||
private val globalErrorHandler: GlobalErrorHandler,
|
private val globalErrorHandler: GlobalErrorHandler,
|
||||||
@SessionId
|
@SessionId
|
||||||
@ -161,13 +162,11 @@ internal class DefaultSession @Inject constructor(
|
|||||||
override val isOpenable: Boolean
|
override val isOpenable: Boolean
|
||||||
get() = sessionParamsStore.get(sessionId)?.isTokenValid ?: false
|
get() = sessionParamsStore.get(sessionId)?.isTokenValid ?: false
|
||||||
|
|
||||||
private var sessionScope: CoroutineScope? = null
|
|
||||||
|
|
||||||
@MainThread
|
@MainThread
|
||||||
override fun open() {
|
override fun open() {
|
||||||
assert(!isOpen)
|
assert(!isOpen)
|
||||||
isOpen = true
|
isOpen = true
|
||||||
sessionScope = CoroutineScope(SupervisorJob())
|
sessionCoroutineScopeHolder.start()
|
||||||
cryptoService.get().ensureDevice()
|
cryptoService.get().ensureDevice()
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
lifecycleObservers.forEach { it.onSessionStarted() }
|
lifecycleObservers.forEach { it.onSessionStarted() }
|
||||||
@ -208,8 +207,7 @@ internal class DefaultSession @Inject constructor(
|
|||||||
|
|
||||||
override fun close() {
|
override fun close() {
|
||||||
assert(isOpen)
|
assert(isOpen)
|
||||||
sessionScope?.coroutineContext?.cancelChildren(CancellationException("Closing session"))
|
sessionCoroutineScopeHolder.stop()
|
||||||
sessionScope = null
|
|
||||||
stopSync()
|
stopSync()
|
||||||
// timelineEventDecryptor.destroy()
|
// timelineEventDecryptor.destroy()
|
||||||
uiHandler.post {
|
uiHandler.post {
|
||||||
@ -315,7 +313,7 @@ internal class DefaultSession @Inject constructor(
|
|||||||
|
|
||||||
override fun launch(context: CoroutineContext,
|
override fun launch(context: CoroutineContext,
|
||||||
block: suspend () -> Unit) {
|
block: suspend () -> Unit) {
|
||||||
sessionScope?.launch(context) {
|
sessionCoroutineScopeHolder.scope?.launch(context) {
|
||||||
block()
|
block()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user