Skip to content

Commit 456a2ef

Browse files
committed
Cancel extensions for CoroutineScope and Job
Fixes #1338
1 parent f22604b commit 456a2ef

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

+4
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ public abstract interface class kotlinx/coroutines/CoroutineScope {
202202
public final class kotlinx/coroutines/CoroutineScopeKt {
203203
public static final fun CoroutineScope (Lkotlin/coroutines/CoroutineContext;)Lkotlinx/coroutines/CoroutineScope;
204204
public static final fun MainScope ()Lkotlinx/coroutines/CoroutineScope;
205+
public static final fun cancel (Lkotlinx/coroutines/CoroutineScope;Ljava/lang/String;Ljava/lang/Throwable;)V
205206
public static final fun cancel (Lkotlinx/coroutines/CoroutineScope;Ljava/util/concurrent/CancellationException;)V
207+
public static synthetic fun cancel$default (Lkotlinx/coroutines/CoroutineScope;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)V
206208
public static synthetic fun cancel$default (Lkotlinx/coroutines/CoroutineScope;Ljava/util/concurrent/CancellationException;ILjava/lang/Object;)V
207209
public static final fun coroutineScope (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
208210
public static final fun ensureActive (Lkotlinx/coroutines/CoroutineScope;)V
@@ -352,8 +354,10 @@ public final class kotlinx/coroutines/JobKt {
352354
public static final synthetic fun cancel (Lkotlin/coroutines/CoroutineContext;)V
353355
public static final synthetic fun cancel (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Throwable;)Z
354356
public static final fun cancel (Lkotlin/coroutines/CoroutineContext;Ljava/util/concurrent/CancellationException;)V
357+
public static final fun cancel (Lkotlinx/coroutines/Job;Ljava/lang/String;Ljava/lang/Throwable;)V
355358
public static synthetic fun cancel$default (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Throwable;ILjava/lang/Object;)Z
356359
public static synthetic fun cancel$default (Lkotlin/coroutines/CoroutineContext;Ljava/util/concurrent/CancellationException;ILjava/lang/Object;)V
360+
public static synthetic fun cancel$default (Lkotlinx/coroutines/Job;Ljava/lang/String;Ljava/lang/Throwable;ILjava/lang/Object;)V
357361
public static final fun cancelAndJoin (Lkotlinx/coroutines/Job;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
358362
public static final synthetic fun cancelChildren (Lkotlin/coroutines/CoroutineContext;)V
359363
public static final synthetic fun cancelChildren (Lkotlin/coroutines/CoroutineContext;Ljava/lang/Throwable;)V

kotlinx-coroutines-core/common/src/CoroutineScope.kt

+7
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,13 @@ public fun CoroutineScope.cancel(cause: CancellationException? = null) {
202202
job.cancel(cause)
203203
}
204204

205+
/**
206+
* Cancels this scope, including its job and all its children with a specified diagnostic error [message].
207+
* A [cause] can be specified to provide additional details on a cancellation reason for debugging purposes.
208+
* Throws [IllegalStateException] if the scope does not have a job in it.
209+
*/
210+
public fun CoroutineScope.cancel(message: String, cause: Throwable? = null): Unit = cancel(CancellationException(message, cause))
211+
205212
/**
206213
* Ensures that current scope is [active][CoroutineScope.isActive].
207214
* Throws [IllegalStateException] if the context does not have a job in it.

kotlinx-coroutines-core/common/src/Job.kt

+6
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,12 @@ public fun CoroutineContext.ensureActive(): Unit {
576576
job.ensureActive()
577577
}
578578

579+
/**
580+
* Cancels current job, including all its children with a specified diagnostic error [message].
581+
* A [cause] can be specified to provide additional details on a cancellation reason for debugging purposes.
582+
*/
583+
public fun Job.cancel(message: String, cause: Throwable? = null): Unit = cancel(CancellationException(message, cause))
584+
579585
/**
580586
* @suppress This method has bad semantics when cause is not a [CancellationException]. Use [CoroutineContext.cancel].
581587
*/

0 commit comments

Comments
 (0)