author | wenzelm |
Sun, 15 Aug 2010 23:13:56 +0200 | |
changeset 38428 | c13c95c97e89 |
parent 36676 | src/Pure/PIDE/event_bus.scala@ac7961d42ac3 |
child 38849 | 2f198d107aef |
permissions | -rw-r--r-- |
38428
c13c95c97e89
event_bus.scala rather belongs to system plumbing;
wenzelm
parents:
36676
diff
changeset
|
1 |
/* Title: Pure/System/event_bus.scala |
29190 | 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 |
} |