How to merge or skip duplicate messages in Scala Actor?

Let's say you have a gui component and 10 threads that say that it is repainted at the same time that they all arrive before one paint operation occurs. Instead of naively wasting resources repainting 10 times, simply merge / ignore everything except the last one, and redraw once (or, more likely, twice - once for the first and once for the last). I understand that the Swing repaint manager is doing this.

Is there a way to perform the same type of behavior in a Scala Actor? Is there a way to look at the queue and merge messages or ignore everything except the last specific type or something else?

+5
source share
1 answer

Something like that?:

act = 
  loop { 
    react {
      case Repaint(a, b) => if (lastRepaint + minInterval < System.currentTimeMillis) {
          lastRepaint = System.currentTimeMillis
          repaint(a, b)
    }
  }

If you want to redraw when the actor’s stream gets a chance, but no more, then: (UPDATE: redrawing using the last message arguments)

act =
  loop {
    react {
      case r@Repaint(_, _) =>  
        var lastMsg = r
        def findLast: Unit = {
          reactWithin(0) {
            case r@Repaint(_, _) => 
              lastMsg = r
            case TIMEOUT => repaint(lastMsg.a, lastMsg.b)
          }
        }
        findLast
    }
  }
+4
source

All Articles