Coroutine: create extension method to create childScope
This commit is contained in:
parent
e3744636b6
commit
b637d4a5ee
3 changed files with 16 additions and 8 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue