src/Pure/System/event_bus.scala
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--
event_bus.scala rather belongs to system plumbing;

/*  Title:      Pure/System/event_bus.scala
    Author:     Makarius

Generic event bus with multiple receiving actors.
*/

package isabelle

import scala.actors.Actor, Actor._
import scala.collection.mutable.ListBuffer


class Event_Bus[Event]
{
  /* receivers */

  private val receivers = new ListBuffer[Actor]

  def += (r: Actor) { synchronized { receivers += r } }
  def + (r: Actor): Event_Bus[Event] = { this += r; this }

  def += (f: Event => Unit) {
    this += actor { loop { react { case x: Event => f(x) } } }
  }

  def + (f: Event => Unit): Event_Bus[Event] = { this += f; this }

  def -= (r: Actor) { synchronized { receivers -= r } }
  def - (r: Actor) = { this -= r; this }


  /* event invocation */

  def event(x: Event) { synchronized { receivers.foreach(_ ! x) } }
}