diff --git a/CHANGELOG.md b/CHANGELOG.md index 34159b41688..a8867dfee25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.26.8 +* `Coroutines`: + * Add simple suspend function `suspendPoint` which will ensure that current coroutine is active to let it be + destroyable even in case it have non-suspendable nature + ## 0.26.7 * `Versions`: diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SuspendPoint.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SuspendPoint.kt new file mode 100644 index 00000000000..2954f8e8141 --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/SuspendPoint.kt @@ -0,0 +1,15 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.currentCoroutineContext +import kotlinx.coroutines.ensureActive + +/** + * Ensures that the current coroutine context is still active and throws a [kotlinx.coroutines.CancellationException] + * if the coroutine has been canceled. + * + * This function provides a convenient way to check the active status of a coroutine, which is useful + * to identify cancellation points in long-running or suspendable operations. + * + * @throws kotlinx.coroutines.CancellationException if the coroutine context is no longer active. + */ +suspend fun suspendPoint() = currentCoroutineContext().ensureActive() \ No newline at end of file