Composing actors

Posted by Brian Heylin on Stack Overflow See other posts from Stack Overflow or by Brian Heylin
Published on 2010-03-09T20:31:29Z Indexed on 2010/03/13 17:35 UTC
Read the original article Hit count: 470

Filed under:
|
|

I've implemented a Listenable/Listener trait that can be added to Actors. I'm wondering if it's possible to attach this style of trait to an actor without it having to explicitly call the listenerHandler method?

Also I was expecting to find this functionality in the Akka library. Am I missing something or is there some reason that Akka would not not include this?

trait Listenable { this: Actor =>
    private var listeners: List[Actor] = Nil

    protected def listenerHandler: PartialFunction[Any, Unit] = {
        case AddListener(who) => listeners = who :: listeners
    }

    protected def notifyListeners(event: Any) = {
        listeners.foreach(_.send(event))
    }
}

class SomeActor extends Actor with Listenable
{
    def receive = listenerHandler orElse {
        case Start => notifyListeners(Started())
        case Stop => notifyListeners(Stopped())
    }
}

© Stack Overflow or respective owner

Related posts about scala

Related posts about akka