fs2 icon indicating copy to clipboard operation
fs2 copied to clipboard

Emit a last stream on cancel

Open crakjie opened this issue 2 months ago • 0 comments

I have a grpc use case with a stream that go to the server let call it input and a stream that come from the server let call it out put.

The protocol want to send a cancel event to the server when cancelling the process, to signifty to the the server that the process finish.

both stream are run like this

output.concurrently(input)

So when client don't want to consume anymore he can interupt the output stream. This will then interput the input stream. But I currently don't find a solution to send a last element in the input when the is interuption. I tryied to have a defered that would be the promise of the last element of the stream. defered that would be completed in a finialize on the output, but this does not works.

val output = fs2.Stream.eval(Deferred[IO, Packet]).flatMap { finalizingPacket =>
     val input  = input0 ++ fs2.Stream.eval(finalizingPacket.get)
    serverCalls(input) // internally do concurrently
    .onFinalizeCase[IO] {
            case ExitCase.Completed =>              
              finalizingPacket.complete(endOfSteam).start.void
            case ExitCase.Error(e) =>
              finalizingPacket.complete(cancelPacket).start.void
            case ExitCase.Canceled =>
              finalizingPacket.complete(cancelPacket).start.void
          }
}

In this case the finalizingPacket.get is interupted so there is not outcome.

something like .onComplete which is call even in case of interuption would be nice for this case.

crakjie avatar Nov 14 '25 16:11 crakjie