title: Implementing map entry listener
body: <p>I'm trying to implement Map entry listener with Scala. </p>
  
  <p>Idea: </p>
  
  <ol>
  <li>I need to subscribe to the Map from Service. </li>
  <li>I need to notify all subscribers of the Map when entry with specific key is added/updated. </li>
  <li>I need to have access to the Map from other Services to check entry value.</li>
  </ol>
  
  <p>I couldn't find ready solution for this, so I've tried to implement it with Akka:</p>
  
  <pre><code>class TrackingService(system: ActorSystem) extends LazyLogging {
    private val trackingActor = system.actorOf(TrackingActor.props)
    private val duration = Duration(15, TimeUnit.SECONDS)
    private implicit val timeout = Timeout(duration)
  
    def fireEvent(key: String): Unit = {
      TrackingActor ! EventFired(key)
    }
  
    def eventHappened(key: String): Future[Boolean] = {
      TrackingActor ? DoesEventHappened(key)).mapTo[Boolean]
    }
  
    def registerHiddenCardListener(key: String, data: MyData): Unit = {
      TrackingActor ! Subscribe(key, data)
    }
  }
  
  case class EventFired(key: String)
  case class EventHappened(key: String)
  case class EventHappenedResponse(happened: Boolean)
  
  case class Subscribe(key: String, data: Data)
  class TrackingActor extends Actor with LazyLogging {
    var eventMap: Map[String, Boolean] = Map.empty[String, Boolean]
    var dataMap: Map[String, List[Data]] = Map.empty[String, List[Data]]
  
    def receive: Receive = {
      case Subscribe(key, data)       =&gt;
        val currentData: List[Data] = dataMap.getOrElse(key, Nil)
        val newData = data :: currentData
        dataMap = dataMap + (key -&gt; newData)
      case EventHappened(key)         =&gt; sender() ! EventHappenedResponse(eventMap.getOrElse(key, false))
      case e@EventFired(key)          =&gt;
        eventMap = eventMap + (key -&gt; true)
  
        for {
          dataOpt &lt;- dataMap.get(key)
          data &lt;- dataOpt
        } {
          // do callback with data (e.g. send email)
        }
      case x =&gt; logger.warn(s"Received unknown message: $x")
    }
  }
  
  object TrackingActor {
    def props: Props = Props(classOf[TrackingActor])
  }
  </code></pre>
  
  <p>What I don't like in this solution: I don't like ask pattern, but I need to have access to the entries from non-actor classes. Also, I don't like having 2 maps, but I need to store somewhere data, which should be used for the callback.</p>
  
  <p>Any ideas on how can I improve this? </p>