Coroutine: create extension method to create childScope

This commit is contained in:
ganfra
2023-06-28 16:41:59 +02:00
parent e3744636b6
commit b637d4a5ee
3 changed files with 16 additions and 8 deletions

View File

@@ -23,11 +23,19 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.job
import kotlinx.coroutines.plus
fun childScopeOf(
parentScope: CoroutineScope,
/**
* Create a child scope of the current scope.
* The child scope will be cancelled if the parent scope is cancelled.
* The child scope will be cancelled if an exception is thrown in the parent scope.
* The parent scope won't be cancelled when an exception is thrown in the child scope.
*
* @param dispatcher the dispatcher to use for this scope.
* @param name the name of the coroutine.
*/
fun CoroutineScope.childScope(
dispatcher: CoroutineDispatcher,
name: String,
): CoroutineScope = run {
val supervisorJob = SupervisorJob(parent = parentScope.coroutineContext.job)
parentScope + dispatcher + supervisorJob + CoroutineName(name)
val supervisorJob = SupervisorJob(parent = coroutineContext.job)
this + dispatcher + supervisorJob + CoroutineName(name)
}

View File

@@ -19,7 +19,7 @@ package io.element.android.libraries.matrix.impl
import io.element.android.libraries.androidutils.file.getSizeOfFiles
import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.coroutine.childScopeOf
import io.element.android.libraries.core.coroutine.childScope
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.ProgressCallback
import io.element.android.libraries.matrix.api.core.RoomId
@@ -83,7 +83,7 @@ class RustMatrixClient constructor(
override val sessionId: UserId = UserId(client.userId())
private val roomListService = client.roomListService()
private val sessionCoroutineScope = childScopeOf(appCoroutineScope, dispatchers.main, "Session-${sessionId}")
private val sessionCoroutineScope = appCoroutineScope.childScope(dispatchers.main, "Session-${sessionId}")
private val verificationService = RustSessionVerificationService()
private val syncService = RustSyncService(roomListService, sessionCoroutineScope)
private val pushersService = RustPushersService(

View File

@@ -17,7 +17,7 @@
package io.element.android.libraries.matrix.impl.room
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.core.coroutine.childScopeOf
import io.element.android.libraries.core.coroutine.childScope
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.ProgressCallback
import io.element.android.libraries.matrix.api.core.RoomId
@@ -70,7 +70,7 @@ class RustMatrixRoom(
override val roomId = RoomId(innerRoom.id())
private val roomCoroutineScope = childScopeOf(sessionCoroutineScope, coroutineDispatchers.main, "RoomScope-$roomId")
private val roomCoroutineScope = sessionCoroutineScope.childScope(coroutineDispatchers.main, "RoomScope-$roomId")
override val membersStateFlow: StateFlow<MatrixRoomMembersState>
get() = _membersStateFlow