Skip to content

Instantly share code, notes, and snippets.

@pedroarthur
Last active January 9, 2016 01:26
Show Gist options
  • Save pedroarthur/e5f82bd1e5d2852ed4ca to your computer and use it in GitHub Desktop.
Save pedroarthur/e5f82bd1e5d2852ed4ca to your computer and use it in GitHub Desktop.
/* imports suppressed */
class MemoizingActor(actual: ActorRef) extends Actor {
case class DropState(message: Any)
override def receive: Actor.Receive = actualReceive(Map())
def actualReceive(data: Map[Any, Future[Any]]): Receive = {
case DropState(any) =>
context become actualReceive(data - any)
case any: Any =>
val future = data getOrElse (any, {
(actual ? any) andThen { case _ => self ! DropState(any) }
}) pipeTo sender
context become actualReceive(data + (any -> future))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment