@@ -13,15 +13,15 @@ import cats.effect.std.Supervisor
1313import cats .syntax .all ._
1414import cats .effect .syntax .all ._
1515import jsonrpclib .internals .MessageDispatcher
16- import jsonrpclib .internals ._
1716
1817import scala .util .Try
1918import java .util .regex .Pattern
2019
2120trait FS2Channel [F [_]] extends Channel [F ] {
2221
23- def input : Pipe [F , Payload , Unit ]
24- def output : Stream [F , Payload ]
22+ def input : Pipe [F , Message , Unit ]
23+ def inputOrBounce : Pipe [F , Either [ProtocolError , Message ], Unit ]
24+ def output : Stream [F , Message ]
2525
2626 def withEndpoint (endpoint : Endpoint [F ])(implicit F : Functor [F ]): Resource [F , FS2Channel [F ]] =
2727 Resource .make(mountEndpoint(endpoint))(_ => unmountEndpoint(endpoint.method)).map(_ => this )
@@ -54,7 +54,7 @@ object FS2Channel {
5454 for {
5555 supervisor <- Stream .resource(Supervisor [F ])
5656 ref <- Ref [F ].of(State [F ](Map .empty, Map .empty, Map .empty, Vector .empty, 0 )).toStream
57- queue <- cats.effect.std.Queue .bounded[F , Payload ](bufferSize).toStream
57+ queue <- cats.effect.std.Queue .bounded[F , Message ](bufferSize).toStream
5858 impl = new Impl (queue, ref, supervisor, cancelTemplate)
5959
6060 // Creating a bespoke endpoint to receive cancelation requests
@@ -116,16 +116,20 @@ object FS2Channel {
116116 }
117117
118118 private class Impl [F [_]](
119- private val queue : cats.effect.std.Queue [F , Payload ],
119+ private val queue : cats.effect.std.Queue [F , Message ],
120120 private val state : Ref [F , FS2Channel .State [F ]],
121121 supervisor : Supervisor [F ],
122122 maybeCancelTemplate : Option [CancelTemplate ]
123123 )(implicit F : Concurrent [F ])
124124 extends MessageDispatcher [F ]
125125 with FS2Channel [F ] {
126126
127- def output : Stream [F , Payload ] = Stream .fromQueueUnterminated(queue)
128- def input : Pipe [F , Payload , Unit ] = _.evalMap(handleReceivedPayload)
127+ def output : Stream [F , Message ] = Stream .fromQueueUnterminated(queue)
128+ def inputOrBounce : Pipe [F , Either [ProtocolError , Message ], Unit ] = _.evalMap {
129+ case Left (error) => sendProtocolError(error)
130+ case Right (message) => handleReceivedMessage(message)
131+ }
132+ def input : Pipe [F , Message , Unit ] = _.evalMap(handleReceivedMessage)
129133
130134 def mountEndpoint (endpoint : Endpoint [F ]): F [Unit ] = state
131135 .modify(s =>
@@ -154,7 +158,7 @@ object FS2Channel {
154158 }
155159 protected def reportError (params : Option [Payload ], error : ProtocolError , method : String ): F [Unit ] = ???
156160 protected def getEndpoint (method : String ): F [Option [Endpoint [F ]]] = state.get.map(_.getEndpoint(method))
157- protected def sendMessage (message : Message ): F [Unit ] = queue.offer(Codec .encode( message) )
161+ protected def sendMessage (message : Message ): F [Unit ] = queue.offer(message)
158162
159163 protected def nextCallId (): F [CallId ] = state.modify(_.nextCallId)
160164 protected def createPromise [A ](callId : CallId ): F [(Try [A ] => F [Unit ], () => F [A ])] = Deferred [F , Try [A ]].map {
0 commit comments