Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource.asFlow #2677

Merged
merged 4 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -761,3 +763,13 @@ private suspend fun List<suspend (ExitCase) -> Unit>.cancelAll(
if (first != null) Platform.composeErrors(NonEmptyList(first, it))
else Platform.composeErrors(it)
}, { first })

/**
* binds and emits [A] of the resource
*/
public fun <A> Resource<A>.asFlow(): Flow<A> =
flow {
arrow.fx.coroutines.continuations.resource {
emit(bind())
}
}
i-walker marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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.flowOf
import kotlinx.coroutines.flow.map

class ResourceTest : ArrowFxSpec(
spec = {
Expand Down Expand Up @@ -418,6 +420,14 @@ class ResourceTest : ArrowFxSpec(
}
}
}

"resource.asFlow()" {
checkAll(Arb.int()) { n ->
val r = Resource({ n }, { _, _ -> Unit })

r.asFlow().map { it + 1 } shouldBe flowOf(n + 1)
Copy link
Member

@nomisRev nomisRev Mar 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be failing but it passed on Windows 🤔
You cannot compare Flow since they are deferred values, they should be run and calculate a result before you can compare them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree :) I checked in Kotest and I couldn't find an Matcher for flow, that was the only idea I had why this would pass

}
}
}
)

Expand Down