| author | wenzelm | 
| Sat, 01 Jun 2013 14:26:04 +0200 | |
| changeset 52285 | da42b500a6aa | 
| parent 45673 | cd41e3903fbf | 
| child 55618 | 995162143ef4 | 
| permissions | -rw-r--r-- | 
| 38428 
c13c95c97e89
event_bus.scala rather belongs to system plumbing;
 wenzelm parents: 
36676diff
changeset | 1 | /* Title: Pure/System/event_bus.scala | 
| 45673 
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
 wenzelm parents: 
45667diff
changeset | 2 | Module: PIDE | 
| 29190 | 3 | Author: Makarius | 
| 4 | ||
| 32539 | 5 | Generic event bus with multiple receiving actors. | 
| 29190 | 6 | */ | 
| 7 | ||
| 8 | package isabelle | |
| 9 | ||
| 32539 | 10 | import scala.actors.Actor, Actor._ | 
| 29191 | 11 | import scala.collection.mutable.ListBuffer | 
| 29190 | 12 | |
| 13 | ||
| 32539 | 14 | class Event_Bus[Event] | 
| 29200 | 15 | {
 | 
| 32539 | 16 | /* receivers */ | 
| 17 | ||
| 18 | private val receivers = new ListBuffer[Actor] | |
| 19 | ||
| 20 |   def += (r: Actor) { synchronized { receivers += r } }
 | |
| 21 |   def + (r: Actor): Event_Bus[Event] = { this += r; this }
 | |
| 29200 | 22 | |
| 32539 | 23 |   def += (f: Event => Unit) {
 | 
| 43406 
40c67d894be4
avoid compiler warning -- this is unchecked anyway;
 wenzelm parents: 
38849diff
changeset | 24 |     this += actor { loop { react { case x => f(x.asInstanceOf[Event]) } } }
 | 
| 32539 | 25 | } | 
| 29190 | 26 | |
| 32539 | 27 |   def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
 | 
| 29191 | 28 | |
| 32539 | 29 |   def -= (r: Actor) { synchronized { receivers -= r } }
 | 
| 30 |   def - (r: Actor) = { this -= r; this }
 | |
| 29190 | 31 | |
| 29200 | 32 | |
| 33 | /* event invocation */ | |
| 34 | ||
| 32539 | 35 |   def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
 | 
| 29190 | 36 | } |