Skip to content

Commit

Permalink
implement channel send all
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgfraser committed Jul 26, 2023
1 parent 6fc3ffe commit 1d54cb1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
9 changes: 9 additions & 0 deletions zio-http/src/main/scala/zio/http/Channel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ trait Channel[-In, +Out] { self =>
*/
def send(in: In): Task[Unit]

/**
* Send all messages to the channel.
*/
def sendAll(in: Iterable[In]): Task[Unit]

/**
* Shut down the channel.
*/
Expand All @@ -57,6 +62,8 @@ trait Channel[-In, +Out] { self =>
self.receive
def send(in: In2): Task[Unit] =
self.send(f(in))
def sendAll(in: Iterable[In2]): Task[Unit] =
self.sendAll(in.map(f))
def shutdown: UIO[Unit] =
self.shutdown
}
Expand All @@ -73,6 +80,8 @@ trait Channel[-In, +Out] { self =>
self.receive.map(f)
def send(in: In): Task[Unit] =
self.send(in)
def sendAll(in: Iterable[In]): Task[Unit] =
self.sendAll(in)
def shutdown: UIO[Unit] =
self.shutdown
}
Expand Down
5 changes: 5 additions & 0 deletions zio-http/src/main/scala/zio/http/WebSocketChannel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ private[http] object WebSocketChannel {
case Read(message) => nettyChannel.writeAndFlush(frameToNetty(message))
case _ => ZIO.unit
}
def sendAll(in: Iterable[WebSocketChannelEvent]): Task[Unit] =
ZIO.foreachDiscard(in) {
case Read(message) => nettyChannel.write(frameToNetty(message))
case _ => ZIO.unit
} *> nettyChannel.flush
def shutdown: UIO[Unit] =
nettyChannel.close(false).orDie
}
Expand Down

0 comments on commit 1d54cb1

Please sign in to comment.