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.job
import kotlinx.coroutines.plus 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, dispatcher: CoroutineDispatcher,
name: String, name: String,
): CoroutineScope = run { ): CoroutineScope = run {
val supervisorJob = SupervisorJob(parent = parentScope.coroutineContext.job) val supervisorJob = SupervisorJob(parent = coroutineContext.job)
parentScope + dispatcher + supervisorJob + CoroutineName(name) 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.getSizeOfFiles
import io.element.android.libraries.androidutils.file.safeDelete import io.element.android.libraries.androidutils.file.safeDelete
import io.element.android.libraries.core.coroutine.CoroutineDispatchers 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.MatrixClient
import io.element.android.libraries.matrix.api.core.ProgressCallback import io.element.android.libraries.matrix.api.core.ProgressCallback
import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.RoomId
@ -83,7 +83,7 @@ class RustMatrixClient constructor(
override val sessionId: UserId = UserId(client.userId()) override val sessionId: UserId = UserId(client.userId())
private val roomListService = client.roomListService() 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 verificationService = RustSessionVerificationService()
private val syncService = RustSyncService(roomListService, sessionCoroutineScope) private val syncService = RustSyncService(roomListService, sessionCoroutineScope)
private val pushersService = RustPushersService( private val pushersService = RustPushersService(

View file

@ -17,7 +17,7 @@
package io.element.android.libraries.matrix.impl.room package io.element.android.libraries.matrix.impl.room
import io.element.android.libraries.core.coroutine.CoroutineDispatchers 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.EventId
import io.element.android.libraries.matrix.api.core.ProgressCallback import io.element.android.libraries.matrix.api.core.ProgressCallback
import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.RoomId
@ -70,7 +70,7 @@ class RustMatrixRoom(
override val roomId = RoomId(innerRoom.id()) 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> override val membersStateFlow: StateFlow<MatrixRoomMembersState>
get() = _membersStateFlow get() = _membersStateFlow