From f4f03fc6140f88dc528da9114866b4200d728c32 Mon Sep 17 00:00:00 2001 From: Daniil Ovchinnikov Date: Mon, 25 Jul 2022 16:16:20 +0200 Subject: [PATCH] add ability to create child coroutine scope GitOrigin-RevId: 4c2035e889b6380e2488f6eecff83b3ec50ac775 --- .../src/com/intellij/util/coroutineScope.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 platform/util-ex/src/com/intellij/util/coroutineScope.kt diff --git a/platform/util-ex/src/com/intellij/util/coroutineScope.kt b/platform/util-ex/src/com/intellij/util/coroutineScope.kt new file mode 100644 index 000000000000..2150d8d10409 --- /dev/null +++ b/platform/util-ex/src/com/intellij/util/coroutineScope.kt @@ -0,0 +1,23 @@ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.util + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.job +import org.jetbrains.annotations.ApiStatus.Experimental +import org.jetbrains.annotations.ApiStatus.Internal +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext + +@Internal +@Experimental +fun CoroutineScope.childScope(context: CoroutineContext = EmptyCoroutineContext, supervisor: Boolean = true): CoroutineScope { + require(context[Job] == null) { + "Found Job in context: $context" + } + val parentContext = coroutineContext + val parentJob = parentContext.job + val job = if (supervisor) SupervisorJob(parent = parentJob) else Job(parent = parentJob) + return CoroutineScope(parentContext + job + context) +}