-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpubsub.js
46 lines (36 loc) · 856 Bytes
/
pubsub.js
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
let { Actor, ActorSystem, Topic } = require('../dist/index')
class SpeakingActor extends Actor {
constructor(idx) {
super(idx)
}
say(message) {}
}
class HappyActor extends SpeakingActor {
constructor(idx) {
super(`happy-${idx}`)
}
say(message) {
console.log(this.id, message, ":D")
}
}
class SadActor extends SpeakingActor {
constructor(idx) {
super(`Sad-${idx}`)
}
say(message) {
console.log(this.id, message, ":(")
}
}
let system = ActorSystem.default()
let topic = Topic.for(system, "my-topic", SpeakingActor)
for (let i = 0; i < 100; i++) {
if (i % 2 === 0) {
topic.subscribe(system.actorOf(HappyActor, [i]))
} else {
topic.subscribe(system.actorOf(SadActor, [i]))
}
}
topic.say("Hi")
setTimeout(() => {
system.free()
}, 1000)