-
Notifications
You must be signed in to change notification settings - Fork 5
/
bindings.scala
133 lines (93 loc) · 3.39 KB
/
bindings.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
package org.akkajs.bindings
import akka.{actor => akkaactor}
import org.akkajs.shocon
import com.typesafe.config.Config
import scalajs.js
import scala.scalajs.js.annotation.{JSExportTopLevel, ScalaJSDefined}
import akka.actor._
@JSExportTopLevel("ConfigFactory")
object ConfigFactory extends js.Object {
def default(): Config = akkajs.Config.default
def parse(str: String): Config = Config(shocon.Config(str))
}
@JSExportTopLevel("Configuration")
class Configuration(str: String = "") extends js.Object {
var conf = ConfigFactory.default()
def add(str: String): Unit = {
val tmp = conf
conf = ConfigFactory.parse(str).withFallback(tmp)
}
def get() = conf
if (str.trim() != "")
add(str)
}
@JSExportTopLevel("ActorSystem")
object ActorSystem extends js.Object {
def create(name: String, config: Config): ActorSystemImpl =
new ActorSystemImpl(
akkaactor.ActorSystem(name, config)
)
def create(name: String): ActorSystemImpl =
create(name, akkajs.Config.default)
def create(): ActorSystemImpl =
create("default", akkajs.Config.default)
}
class ActorRef(val actorRef: akkaactor.ActorRef, val sourceRef: akkaactor.ActorRef = null) extends js.Object {
def path(): String = actorRef.path.toString
def tell(msg: Any) = actorRef.tell(msg, sourceRef)
def tell(msg: Any, sender: ActorRef) = actorRef.tell(msg, sender.actorRef)
def kill() = tell(akkaactor.PoisonPill)
}
class ActorSelection(val actorSel: akkaactor.ActorSelection) extends js.Object {
def path(): String = actorSel.anchorPath.toString
def tell(msg: Any) = actorSel.tell(msg, null)
def tell(msg: Any, sender: akkaactor.ActorRef) = actorSel.tell(msg, sender)
def kill() = tell(akkaactor.PoisonPill)
}
class ActorSystemImpl(val system: akkaactor.ActorSystem) extends js.Object {
def select(path: String) = new ActorSelection(
system.actorSelection(path)
)
def spawn(actor: Actor) = new ActorRef({
if (actor.name != null) system.actorOf(Props(actor.actor), actor.name)
else system.actorOf(Props(actor.actor))
})
def terminate() = system.terminate()
}
@JSExportTopLevel("Actor")
class Actor() extends js.Object {
jsActor =>
// Functions intended to be overloaded
var name: String = null
def receive(any: Any): Unit = ()
def preStart(): Unit = ()
def postStop(): Unit = ()
// API
def path(): String = ar.path.toString
def parent(): ActorRef = newAR(innerContext.parent)
def children(): js.Array[ActorRef] =
js.Array(innerContext.children.map(newAR).toSeq: _*)
def become(behavior: js.Function1[Any, Unit]) =
innerContext.become{case any => behavior(any)}
def sender() = newAR(innerContext.sender())
def self() = newAR(ar)
def system() = new ActorSystemImpl(innerContext.system)
def spawn(actor: Actor) = new ActorRef({
if (actor.name != null) innerContext.actorOf(Props(actor.actor), actor.name)
else innerContext.actorOf(Props(actor.actor))
})
//Inner implementation
var ar: akkaactor.ActorRef = _
var innerContext: akkaactor.ActorContext = _
lazy val actor: akkaactor.Actor = new akkaactor.Actor {
name = jsActor.name
override def preStart() = {
jsActor.ar = self
jsActor.innerContext = context
jsActor.preStart()
}
override def postStop() = jsActor.postStop()
def receive = { case any => jsActor.receive(any) }
}
private def newAR(dest: akkaactor.ActorRef) = new ActorRef(dest, ar)
}