diff --git a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api index 1944409ec41..749ee0204ab 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api +++ b/arrow-libs/fx/arrow-fx-coroutines/api/arrow-fx-coroutines.api @@ -354,6 +354,7 @@ public final class arrow/fx/coroutines/ResourceExtensionsKt { } public final class arrow/fx/coroutines/ResourceKt { + public static final fun asFlow (Larrow/fx/coroutines/Resource;)Lkotlinx/coroutines/flow/Flow; public static final fun release (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource; public static final fun release-zgiIeyo (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Larrow/fx/coroutines/Resource; public static final fun releaseCase (Larrow/fx/coroutines/Resource;Lkotlin/jvm/functions/Function3;)Larrow/fx/coroutines/Resource; diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Resource.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Resource.kt index 925ac7aee99..32d41b6e600 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Resource.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonMain/kotlin/arrow/fx/coroutines/Resource.kt @@ -13,6 +13,8 @@ import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CancellationException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow import kotlinx.coroutines.withContext /** @@ -761,3 +763,13 @@ private suspend fun List Unit>.cancelAll( if (first != null) Platform.composeErrors(NonEmptyList(first, it)) else Platform.composeErrors(it) }, { first }) + +/** + * runs [Resource.use] and emits [A] of the resource + */ +public fun Resource.asFlow(): Flow = + flow { + use { + emit(it) + } + } diff --git a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/ResourceTest.kt b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/ResourceTest.kt index 7cf06bdbe6d..bb24150f36f 100644 --- a/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/ResourceTest.kt +++ b/arrow-libs/fx/arrow-fx-coroutines/src/commonTest/kotlin/arrow/fx/coroutines/ResourceTest.kt @@ -16,6 +16,8 @@ import io.kotest.property.arbitrary.string import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.async +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.toList class ResourceTest : ArrowFxSpec( spec = { @@ -269,8 +271,8 @@ class ResourceTest : ArrowFxSpec( { require(started.complete(Unit)); i }, { ii, ex -> require(released.complete(ii to ex)) } ).parZip( - Resource({ started.await(); throw throwable }) { _, _ -> } - ) { _, _ -> } + Resource({ started.await(); throw throwable }) { _, _ -> } + ) { _, _ -> } .use { fail("It should never reach here") } } shouldBe throwable @@ -418,6 +420,14 @@ class ResourceTest : ArrowFxSpec( } } } + + "resource.asFlow()" { + checkAll(Arb.int()) { n -> + val r = Resource({ n }, { _, _ -> Unit }) + + r.asFlow().map { it + 1 }.toList() shouldBe listOf(n + 1) + } + } } )