29190
|
1 |
/* Title: Pure/General/event_bus.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
32539
|
4 |
Generic event bus with multiple receiving actors.
|
29190
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
32539
|
9 |
import scala.actors.Actor, Actor._
|
29191
|
10 |
import scala.collection.mutable.ListBuffer
|
29190
|
11 |
|
|
12 |
|
32539
|
13 |
class Event_Bus[Event]
|
29200
|
14 |
{
|
32539
|
15 |
/* receivers */
|
|
16 |
|
|
17 |
private val receivers = new ListBuffer[Actor]
|
|
18 |
|
|
19 |
def += (r: Actor) { synchronized { receivers += r } }
|
|
20 |
def + (r: Actor): Event_Bus[Event] = { this += r; this }
|
29200
|
21 |
|
32539
|
22 |
def += (f: Event => Unit) {
|
|
23 |
this += actor { loop { react { case x: Event => f(x) } } }
|
|
24 |
}
|
29190
|
25 |
|
32539
|
26 |
def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
|
29191
|
27 |
|
32539
|
28 |
def -= (r: Actor) { synchronized { receivers -= r } }
|
|
29 |
def - (r: Actor) = { this -= r; this }
|
29190
|
30 |
|
29200
|
31 |
|
|
32 |
/* event invocation */
|
|
33 |
|
32539
|
34 |
def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
|
29190
|
35 |
}
|