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

Refactor: Remove error type from SocketApp #769

Merged
merged 1 commit into from
Jan 6, 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: 4 additions & 4 deletions zio-http/src/main/scala/zhttp/http/Response.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ object Response {
/**
* Creates a socket response using an app
*/
def socket[R, E](app: SocketApp[R, E]): Response[R, E] =
def socket[R, E](app: SocketApp[R]): Response[R, E] =
Response(Status.SWITCHING_PROTOCOLS, Headers.empty, HttpData.empty, Attribute(socketApp = Option(app)))

/**
* Creates a new WebSocket Response
*/
def socket[R, E](ss: Socket[R, E, WebSocketFrame, WebSocketFrame]): Response[R, E] =
def socket[R](ss: Socket[R, Throwable, WebSocketFrame, WebSocketFrame]): Response[R, Nothing] =
SocketApp(ss).asResponse

/**
Expand All @@ -198,7 +198,7 @@ object Response {
*/

private[zhttp] final case class Attribute[-R, +E](
socketApp: Option[SocketApp[R, E]] = None,
socketApp: Option[SocketApp[R]] = None,
memoize: Boolean = false,
serverTime: Boolean = false,
encoded: Option[(Response[R, E], HttpResponse)] = None,
Expand All @@ -210,7 +210,7 @@ object Response {

def withServerTime: Attribute[R, E] = self.copy(serverTime = true)

def withSocketApp[R1 <: R, E1 >: E](app: SocketApp[R1, E1]): Attribute[R1, E1] = self.copy(socketApp = Option(app))
def withSocketApp[R1 <: R](app: SocketApp[R1]): Attribute[R1, E] = self.copy(socketApp = Option(app))
}

object Attribute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import zio.stream.ZStream
*/
final case class ServerSocketHandler[R](
zExec: HttpRuntime[R],
ss: SocketApp[R, Throwable],
ss: SocketApp[R],
) extends SimpleChannelInboundHandler[JWebSocketFrame] {

/**
Expand Down
2 changes: 1 addition & 1 deletion zio-http/src/main/scala/zhttp/socket/Conversion.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import zhttp.http.Response
trait Conversion {
import scala.language.implicitConversions

implicit def asResponse[R, E](app: SocketApp[R, E]): Response[R, E] = app.asResponse
implicit def asResponse[R, E](app: SocketApp[R]): Response[R, E] = app.asResponse
}
46 changes: 23 additions & 23 deletions zio-http/src/main/scala/zhttp/socket/SocketApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import zio.{NeedsEnv, ZIO}

import java.net.SocketAddress

final case class SocketApp[-R, +E](
final case class SocketApp[-R](
timeout: Option[ZIO[R, Nothing, Any]] = None,
open: Option[Handle[R, E]] = None,
message: Option[Socket[R, E, WebSocketFrame, WebSocketFrame]] = None,
open: Option[Handle[R]] = None,
message: Option[Socket[R, Throwable, WebSocketFrame, WebSocketFrame]] = None,
error: Option[Throwable => ZIO[R, Nothing, Any]] = None,
close: Option[Connection => ZIO[R, Nothing, Any]] = None,
decoder: SocketDecoder = SocketDecoder.default,
Expand All @@ -21,12 +21,12 @@ final case class SocketApp[-R, +E](
/**
* Creates a new WebSocket Response
*/
def asResponse: Response[R, E] = Response.socket(self)
def asResponse: Response[R, Nothing] = Response.socket(self)

/**
* Called when the websocket connection is closed successfully.
*/
def onClose[R1 <: R](close: Connection => ZIO[R1, Nothing, Any]): SocketApp[R1, E] =
def onClose[R1 <: R](close: Connection => ZIO[R1, Nothing, Any]): SocketApp[R1] =
copy(close = self.close match {
case Some(value) => Some((connection: Connection) => value(connection) *> close(connection))
case None => Some(close)
Expand All @@ -35,7 +35,7 @@ final case class SocketApp[-R, +E](
/**
* Called whenever there is an error on the channel after a successful upgrade to websocket.
*/
def onError[R1 <: R](error: Throwable => ZIO[R1, Nothing, Any]): SocketApp[R1, E] =
def onError[R1 <: R](error: Throwable => ZIO[R1, Nothing, Any]): SocketApp[R1] =
copy(error = self.error match {
case Some(value) => Some((cause: Throwable) => value(cause) *> error(cause))
case None => Some(error)
Expand All @@ -45,7 +45,7 @@ final case class SocketApp[-R, +E](
* Called on every incoming WebSocketFrame. In case of a failure on the returned stream, the socket is forcefully
* closed.
*/
def onMessage[R1 <: R, E1 >: E](message: Socket[R1, E1, WebSocketFrame, WebSocketFrame]): SocketApp[R1, E1] =
def onMessage[R1 <: R](message: Socket[R1, Throwable, WebSocketFrame, WebSocketFrame]): SocketApp[R1] =
copy(message = self.message match {
case Some(other) => Some(other merge message)
case None => Some(message)
Expand All @@ -55,20 +55,20 @@ final case class SocketApp[-R, +E](
* Called when the connection is successfully upgraded to a websocket one. In case of a failure, the socket is
* forcefully closed.
*/
def onOpen[R1 <: R, E1 >: E](open: Socket[R1, E1, Connection, WebSocketFrame]): SocketApp[R1, E1] =
def onOpen[R1 <: R](open: Socket[R1, Throwable, Connection, WebSocketFrame]): SocketApp[R1] =
onOpen(Handle(open))

/**
* Called when the connection is successfully upgraded to a websocket one. In case of a failure, the socket is
* forcefully closed.
*/
def onOpen[R1 <: R, E1 >: E](open: Connection => ZIO[R1, E1, Any]): SocketApp[R1, E1] =
def onOpen[R1 <: R](open: Connection => ZIO[R1, Throwable, Any]): SocketApp[R1] =
onOpen(Handle(open))

/**
* Called when the websocket handshake gets timeout.
*/
def onTimeout[R1 <: R](timeout: ZIO[R1, Nothing, Any]): SocketApp[R1, E] =
def onTimeout[R1 <: R](timeout: ZIO[R1, Nothing, Any]): SocketApp[R1] =
copy(timeout = self.timeout match {
case Some(other) => Some(timeout *> other)
case None => Some(timeout)
Expand All @@ -77,7 +77,7 @@ final case class SocketApp[-R, +E](
/**
* Provides the socket app with its required environment, which eliminates its dependency on `R`.
*/
def provide(env: R)(implicit ev: NeedsEnv[R]): SocketApp[Any, E] =
def provide(env: R)(implicit ev: NeedsEnv[R]): SocketApp[Any] =
self.copy(
timeout = self.timeout.map(_.provide(env)),
open = self.open.map(_.provide(env)),
Expand All @@ -89,57 +89,57 @@ final case class SocketApp[-R, +E](
/**
* Frame decoder configuration
*/
def withDecoder(decoder: SocketDecoder): SocketApp[R, E] =
def withDecoder(decoder: SocketDecoder): SocketApp[R] =
copy(decoder = self.decoder ++ decoder)

/**
* Server side websocket configuration
*/
def withProtocol(protocol: SocketProtocol): SocketApp[R, E] =
def withProtocol(protocol: SocketProtocol): SocketApp[R] =
copy(protocol = self.protocol ++ protocol)

/**
* Called when the connection is successfully upgraded to a websocket one. In case of a failure, the socket is
* forcefully closed.
*/
private def onOpen[R1 <: R, E1 >: E](open: Handle[R1, E1]): SocketApp[R1, E1] =
private def onOpen[R1 <: R](open: Handle[R1]): SocketApp[R1] =
copy(open = self.open.fold(Some(open))(other => Some(other merge open)))
}

object SocketApp {
type Connection = SocketAddress

def apply[R, E](socket: Socket[R, E, WebSocketFrame, WebSocketFrame]): SocketApp[R, E] =
def apply[R](socket: Socket[R, Throwable, WebSocketFrame, WebSocketFrame]): SocketApp[R] =
SocketApp(message = Some(socket))

private[zhttp] sealed trait Handle[-R, +E] { self =>
def merge[R1 <: R, E1 >: E](other: Handle[R1, E1]): Handle[R1, E1] = {
private[zhttp] sealed trait Handle[-R] { self =>
def merge[R1 <: R](other: Handle[R1]): Handle[R1] = {
(self, other) match {
case (WithSocket(s0), WithSocket(s1)) => WithSocket(s0 merge s1)
case (WithEffect(f0), WithEffect(f1)) => WithEffect(c => f0(c) <&> f1(c))
case (a, b) => a.sock merge b.sock
}
}

def provide(r: R)(implicit ev: NeedsEnv[R]): Handle[Any, E] =
def provide(r: R)(implicit ev: NeedsEnv[R]): Handle[Any] =
self match {
case WithEffect(f) => WithEffect(c => f(c).provide(r))
case WithSocket(s) => WithSocket(s.provide(r))
}

private def sock: Handle[R, E] = self match {
private def sock: Handle[R] = self match {
case WithEffect(f) => WithSocket(Socket.fromFunction(c => ZStream.fromEffect(f(c)) *> ZStream.empty))
case s @ WithSocket(_) => s
}
}

private[zhttp] object Handle {
def apply[R, E](onOpen: Socket[R, E, Connection, WebSocketFrame]): Handle[R, E] = WithSocket(onOpen)
def apply[R](onOpen: Socket[R, Throwable, Connection, WebSocketFrame]): Handle[R] = WithSocket(onOpen)

def apply[R, E](onOpen: Connection => ZIO[R, E, Any]): Handle[R, E] = WithEffect(onOpen)
def apply[R](onOpen: Connection => ZIO[R, Throwable, Any]): Handle[R] = WithEffect(onOpen)

final case class WithEffect[R, E](f: Connection => ZIO[R, E, Any]) extends Handle[R, E]
final case class WithEffect[R](f: Connection => ZIO[R, Throwable, Any]) extends Handle[R]

final case class WithSocket[R, E](s: Socket[R, E, Connection, WebSocketFrame]) extends Handle[R, E]
final case class WithSocket[R](s: Socket[R, Throwable, Connection, WebSocketFrame]) extends Handle[R]
}
}