-
Notifications
You must be signed in to change notification settings - Fork 5
/
ActorCtx.scala
174 lines (115 loc) · 4.41 KB
/
ActorCtx.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.evolutiongaming.akkaeffect
import akka.actor.{ActorContext, ActorRef, ActorRefFactory}
import cats.effect.Sync
import cats.syntax.all.*
import cats.{Monad, ~>}
import scala.concurrent.ExecutionContextExecutor
import scala.concurrent.duration.Duration
/** Refined for [[ActorContext]]. Unlike the original ActorContext, all methods of ActorCtx are thread-safe
*
* @see
* [[akka.actor.ActorContext]]
*/
trait ActorCtx[F[_]] {
/** @see
* [[akka.actor.ActorContext.self]]
*/
def self: ActorRef
/** @see
* [[akka.actor.ActorContext.parent]]
*/
def parent: ActorRef
/** @see
* [[akka.actor.ActorContext.dispatcher]]
*/
def executor: ExecutionContextExecutor
/** @see
* [[akka.actor.ActorContext.setReceiveTimeout]]
*/
def setReceiveTimeout(timeout: Duration): F[Unit]
/** @see
* [[akka.actor.ActorContext.child]]
*/
def child(name: String): F[Option[ActorRef]]
/** @see
* [[akka.actor.ActorContext.children]]
*/
def children: F[List[ActorRef]]
/** @see
* [[akka.actor.ActorContext.actorOf]]
*/
def actorRefFactory: ActorRefFactory
/** @see
* [[akka.actor.ActorContext.watchWith]]
*/
def watch[A](actorRef: ActorRef, msg: A): F[Unit]
/** @see
* [[akka.actor.ActorContext.unwatch]]
*/
def unwatch(actorRef: ActorRef): F[Unit]
/** @see
* [[akka.actor.ActorContext.stop]]
*/
def stop: F[Unit]
}
object ActorCtx {
def apply[F[_]](act: Act[F], actorContext: ActorContext): ActorCtx[F] = {
class Main
new Main with ActorCtx[F] {
val self = actorContext.self
val parent = actorContext.parent
def executor = actorContext.dispatcher
def setReceiveTimeout(timeout: Duration) = act(actorContext.setReceiveTimeout(timeout))
def child(name: String) = act(actorContext.child(name))
def children = act(actorContext.children.toList)
def actorRefFactory: ActorRefFactory = actorContext
def watch[A](actorRef: ActorRef, msg: A) = act { actorContext.watchWith(actorRef, msg); () }
def unwatch(actorRef: ActorRef) = act { actorContext.unwatch(actorRef); () }
def stop = act(actorContext.stop(actorContext.self))
}
}
def apply[F[_]: Sync](actorContext: ActorContext): ActorCtx[F] = {
class Main
new Main with ActorCtx[F] {
val self = actorContext.self
val parent = actorContext.parent
def executor = actorContext.dispatcher
def setReceiveTimeout(timeout: Duration) = Sync[F].delay(actorContext.setReceiveTimeout(timeout))
def child(name: String) = none[ActorRef].pure[F]
def children = List.empty[ActorRef].pure[F]
def actorRefFactory: ActorRefFactory = actorContext
def watch[A](actorRef: ActorRef, msg: A) = Sync[F].delay { actorContext.watchWith(actorRef, msg); () }
def unwatch(actorRef: ActorRef) = Sync[F].delay { actorContext.unwatch(actorRef); () }
def stop = Sync[F].delay(actorContext.stop(actorContext.self))
}
}
def flatten[F[_]: Monad](actorContext: ActorContext, actorCtx: F[ActorCtx[F]]): ActorCtx[F] = {
class Flatten
new Flatten with ActorCtx[F] {
def self = actorContext.self
def parent = actorContext.parent
def executor = actorContext.dispatcher
def setReceiveTimeout(timeout: Duration) = actorCtx.flatMap(_.setReceiveTimeout(timeout))
def child(name: String) = actorCtx.flatMap(_.child(name))
def children = actorCtx.flatMap(_.children)
def actorRefFactory: ActorRefFactory = actorContext
def watch[A](actorRef: ActorRef, msg: A) = actorCtx.flatMap(_.watch(actorRef, msg))
def unwatch(actorRef: ActorRef) = actorCtx.flatMap(_.unwatch(actorRef))
def stop = actorCtx.flatMap(_.stop)
}
}
implicit class ActorCtxOps[F[_]](val actorCtx: ActorCtx[F]) extends AnyVal {
def mapK[G[_]](f: F ~> G): ActorCtx[G] = new ActorCtx[G] {
def self = actorCtx.self
def parent = actorCtx.parent
def executor = actorCtx.executor
def setReceiveTimeout(timeout: Duration) = f(actorCtx.setReceiveTimeout(timeout))
def child(name: String) = f(actorCtx.child(name))
def children = f(actorCtx.children)
def actorRefFactory = actorCtx.actorRefFactory
def watch[A](actorRef: ActorRef, msg: A) = f(actorCtx.watch(actorRef, msg))
def unwatch(actorRef: ActorRef) = f(actorCtx.unwatch(actorRef))
def stop = f(actorCtx.stop)
}
}
}