-
Notifications
You must be signed in to change notification settings - Fork 5
/
package.scala
95 lines (71 loc) · 3.14 KB
/
package.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2016-2022 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause
import cats.Eq
import cats.MonadError
import cats.effect.Concurrent
import cats.effect.Deferred
import cats.syntax.all._
import org.typelevel.log4cats.Logger
import scala.concurrent.duration.FiniteDuration
package object clue {
type CloseReason[CE] = Either[Throwable, CE]
// Int = Attempt #. Will only be 0 immediately after a close.
// For first connection, it will be called the first time with 1, after 1st connection attempt.
type ReconnectionStrategy[CE] = (Int, CloseReason[CE]) => Option[FiniteDuration]
type WebSocketReconnectionStrategy = ReconnectionStrategy[WebSocketCloseEvent]
type WebSocketClient[F[_], S] =
PersistentStreamingClient[F, S, WebSocketCloseParams, WebSocketCloseEvent]
type WebSocketBackend[F[_]] = PersistentBackend[F, WebSocketCloseParams, WebSocketCloseEvent]
type WebSocketConnection[F[_]] = PersistentConnection[F, WebSocketCloseParams]
type ApolloWebSocketClient[F[_], S] =
ApolloClient[F, S, WebSocketCloseParams, WebSocketCloseEvent]
protected[clue] type Latch[F[_]] = Deferred[F, Either[Throwable, Unit]]
type WebSocketCloseEvent = Either[Throwable, WebSocketCloseParams]
final implicit class StringOps(val str: String) extends AnyVal {
def error[A]: Either[Throwable, A] =
new Exception(str).asLeft[A]
def raiseError[F[_], A](implicit F: MonadError[F, Throwable], logger: Logger[F]): F[A] =
logger.error(str) >> F.raiseError(new Exception(str))
def errorF[F[_]](implicit logger: Logger[F]): F[Unit] =
logger.error(str)
def warnF[F[_]](implicit logger: Logger[F]): F[Unit] =
logger.warn(str)
def debugF[F[_]](implicit logger: Logger[F]): F[Unit] =
logger.debug(str)
def traceF[F[_]](implicit logger: Logger[F]): F[Unit] =
logger.trace(str)
}
final implicit class ThrowableOps(val t: Throwable) extends AnyVal {
def raiseF[F[_]](
msg: String
)(implicit F: MonadError[F, Throwable], logger: Logger[F]): F[Unit] =
logger.error(t)(msg) >> F.raiseError(t)
def logF[F[_]](
msg: String
)(implicit logger: Logger[F]): F[Unit] =
logger.error(t)(msg)
def warnF[F[_]](msg: String)(implicit logger: Logger[F]): F[Unit] =
logger.warn(t)(msg)
def debugF[F[_]](msg: String)(implicit logger: Logger[F]): F[Unit] =
logger.debug(t)(msg)
}
}
package clue {
object ReconnectionStrategy {
def never[CE]: ReconnectionStrategy[CE] = (_, _) => none
}
object WebSocketReconnectionStrategy {
def never: WebSocketReconnectionStrategy = ReconnectionStrategy.never
}
protected[clue] object Latch {
def apply[F[_]: Concurrent]: F[Latch[F]] =
Deferred[F, Either[Throwable, Unit]]
}
protected[clue] class ConnectionId(val value: Int) extends AnyVal {
def next: ConnectionId = new ConnectionId(value + 1)
}
protected[clue] object ConnectionId {
val Zero: ConnectionId = new ConnectionId(0)
implicit val eqConnectionId: Eq[ConnectionId] = Eq.by(_.value)
}
}