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

feature: add Socket.empty #901

Merged
merged 2 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions zio-http/src/main/scala/zhttp/socket/Socket.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sealed trait Socket[-R, +E, -A, +B] { self =>
case FMerge(sa, sb) => sa(a) merge sb(a)
case Succeed(a) => ZStream.succeed(a)
case Provide(s, r) => s(a).asInstanceOf[ZStream[R, E, B]].provide(r.asInstanceOf[R])
case Empty => ZStream.empty
}

def contramap[Z](za: Z => A): Socket[R, E, Z, B] = Socket.FCMap(self, za)
Expand Down Expand Up @@ -65,6 +66,11 @@ object Socket {
*/
def echo[A]: Socket[Any, Nothing, A, A] = Socket.collect[A] { case a => ZStream.succeed(a) }

/**
* Creates a socket that doesn't do anything.
*/
def empty: Socket[Any, Nothing, Any, Nothing] = Socket.Empty
girdharshubham marked this conversation as resolved.
Show resolved Hide resolved

def end: ZStream[Any, Nothing, Nothing] = ZStream.halt(Cause.empty)

def fromFunction[A]: PartialFromFunction[A] = new PartialFromFunction[A](())
Expand Down Expand Up @@ -108,4 +114,6 @@ object Socket {
private final case class Provide[R, E, A, B](s: Socket[R, E, A, B], r: R) extends Socket[Any, E, A, B]

private case object End extends Socket[Any, Nothing, Any, Nothing]

private case object Empty extends Socket[Any, Nothing, Any, Nothing]
}
3 changes: 3 additions & 0 deletions zio-http/src/test/scala/zhttp/socket/SocketSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ object SocketSpec extends DefaultRunnableSpec {
} +
testM("echo") {
assertM(Socket.echo(1).runCollect)(equalTo(Chunk(1)))
} +
testM("empty") {
assertM(Socket.empty(()).runCollect)(isEmpty)
}
}
}