mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-03 06:01:40 +00:00
Coroutine: create extension method to create childScope
This commit is contained in:
+12
-4
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user