diff --git a/CHANGELOG.md b/CHANGELOG.md index fc6233a42b6..167067c2d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.12.13 +* `Coroutines`: + * Add opportunity to use markers in actors (solution of [#160](https://github.com/InsanusMokrassar/MicroUtils/issues/160)) * `Koin`: * Module inited :) 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 b3e641059b8..01c53023e97 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 @@ -1,28 +1,27 @@ package dev.inmo.micro_utils.coroutines -import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.launch +import kotlinx.coroutines.flow.consumeAsFlow fun CoroutineScope.actor( channelCapacity: Int = Channel.UNLIMITED, + markerFactory: suspend (T) -> Any? = { null }, block: suspend (T) -> Unit ): Channel { val channel = Channel(channelCapacity) - launch { - for (data in channel) { - block(data) - } - } + channel.consumeAsFlow().subscribeAsync(this, markerFactory, block) return channel } inline fun CoroutineScope.safeActor( channelCapacity: Int = Channel.UNLIMITED, noinline onException: ExceptionHandler = defaultSafelyExceptionHandler, + noinline markerFactory: suspend (T) -> Any? = { null }, crossinline block: suspend (T) -> Unit ): Channel = actor( - channelCapacity + channelCapacity, + markerFactory ) { safely(onException) { block(it)