Advertisement
Guest User

Actor Callbacks

a guest
May 15th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.46 KB | None | 0 0
  1. class Callback(f: => Unit) {
  2.    def apply() : Unit = f
  3. }
  4.  
  5. trait CallbackHandler {
  6.   self : Actor =>
  7.  
  8.   def handleCallback : Receive = {
  9.     case callback: Callback =>
  10.       callback()
  11.   }
  12.  
  13.   override def unhandled(msg : Any) :Unit = {
  14.      handleCallback.applyOrElse(msg, super.unhandled)
  15.   }
  16.  
  17.   def onSuccess(fut: Future[T])(f : T => Unit) : Unit = {
  18.   fut.onSuccess {
  19.     t => self ! new Callback(f(t))
  20.   }
  21.  }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement