Skip to content

Add method on FS2Channel: resource #80

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

Merged
merged 2 commits into from
May 6, 2025
Merged
Changes from all commits
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
17 changes: 11 additions & 6 deletions modules/fs2/src/main/scala/jsonrpclib/fs2/FS2Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ trait FS2Channel[F[_]] extends Channel[F] {

object FS2Channel {

def apply[F[_]: Concurrent](
def resource[F[_]: Concurrent](
bufferSize: Int = 2048,
cancelTemplate: Option[CancelTemplate] = None
): Stream[F, FS2Channel[F]] = {
): Resource[F, FS2Channel[F]] = {
for {
supervisor <- Stream.resource(Supervisor[F])
ref <- Ref[F].of(State[F](Map.empty, Map.empty, Map.empty, Vector.empty, 0)).toStream
queue <- cats.effect.std.Queue.bounded[F, Message](bufferSize).toStream
supervisor <- Supervisor[F]
ref <- Resource.eval(Ref[F].of(State[F](Map.empty, Map.empty, Map.empty, Vector.empty, 0)))
queue <- Resource.eval(cats.effect.std.Queue.bounded[F, Message](bufferSize))
impl = new Impl(queue, ref, supervisor, cancelTemplate)

// Creating a bespoke endpoint to receive cancelation requests
Expand All @@ -66,10 +66,15 @@ object FS2Channel {
}
}
// mounting the cancelation endpoint
_ <- maybeCancelEndpoint.traverse_(ep => impl.mountEndpoint(ep)).toStream
_ <- Resource.eval(maybeCancelEndpoint.traverse_(ep => impl.mountEndpoint(ep)))
} yield impl
}

def apply[F[_]: Concurrent](
bufferSize: Int = 2048,
cancelTemplate: Option[CancelTemplate] = None
): Stream[F, FS2Channel[F]] = Stream.resource(resource(bufferSize, cancelTemplate))

private case class State[F[_]](
runningCalls: Map[CallId, Fiber[F, Throwable, Unit]],
pendingCalls: Map[CallId, OutputMessage => F[Unit]],
Expand Down