From dccb479c3c0aaf53ea8c615be4aea74904e090b6 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 2 Nov 2020 21:32:04 +0600 Subject: [PATCH] CoroutineScope#safeActor --- CHANGELOG.md | 1 + .../kotlin/dev/inmo/micro_utils/coroutines/Actor.kt | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a99129f2557..c8380af1456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `Coroutines` * Function `safelyWithoutExceptions` + * Extension `CoroutineScope#safeActor` ## 0.2.4 diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/Actor.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/Actor.kt index 310d67c32f1..dbfc4b45254 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/Actor.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/Actor.kt @@ -17,3 +17,15 @@ fun CoroutineScope.actor( return channel } +inline fun CoroutineScope.safeActor( + channelCapacity: Int = Channel.UNLIMITED, + noinline onException: ExceptionHandler = {}, + crossinline block: suspend (T) -> Unit +): Channel = actor( + channelCapacity +) { + safely(onException) { + block(it) + } +} +