src/Pure/System/event_bus.scala
changeset 38428 c13c95c97e89
parent 36676 ac7961d42ac3
child 38849 2f198d107aef
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/System/event_bus.scala	Sun Aug 15 23:13:56 2010 +0200
     1.3 @@ -0,0 +1,35 @@
     1.4 +/*  Title:      Pure/System/event_bus.scala
     1.5 +    Author:     Makarius
     1.6 +
     1.7 +Generic event bus with multiple receiving actors.
     1.8 +*/
     1.9 +
    1.10 +package isabelle
    1.11 +
    1.12 +import scala.actors.Actor, Actor._
    1.13 +import scala.collection.mutable.ListBuffer
    1.14 +
    1.15 +
    1.16 +class Event_Bus[Event]
    1.17 +{
    1.18 +  /* receivers */
    1.19 +
    1.20 +  private val receivers = new ListBuffer[Actor]
    1.21 +
    1.22 +  def += (r: Actor) { synchronized { receivers += r } }
    1.23 +  def + (r: Actor): Event_Bus[Event] = { this += r; this }
    1.24 +
    1.25 +  def += (f: Event => Unit) {
    1.26 +    this += actor { loop { react { case x: Event => f(x) } } }
    1.27 +  }
    1.28 +
    1.29 +  def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }
    1.30 +
    1.31 +  def -= (r: Actor) { synchronized { receivers -= r } }
    1.32 +  def - (r: Actor) = { this -= r; this }
    1.33 +
    1.34 +
    1.35 +  /* event invocation */
    1.36 +
    1.37 +  def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
    1.38 +}