Skip to content

Commit

Permalink
processEvents added to InputProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
chobeat committed Sep 26, 2016
1 parent 4cced72 commit d764de6
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/src/main/scala/sgl/InputProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ trait InputProvider {
if(eventQueue.isEmpty) None else Some(eventQueue.dequeue())
}

/**
* Functional approach to event handling. This method expects a partial function that handles
* the incoming input.
*
* It is possible to provide a filter function through overloading that will dequeue only the Input
* that satisfy the given condition, leaving the other into the queue.
* @param function
* @param filter
*/
def processEvents(function:(InputEvent)=>Unit,filter:(InputEvent)=>Boolean): Unit= synchronized{
eventQueue.dequeueAll(filter).foreach(function)
}

def processEvents(function:(InputEvent)=>Unit): Unit = processEvents(function,(x:InputEvent)=>true)

//TODO: with an Event based architecture with immutable case
// classes, one concern is going to be garbage collecting
// the events. Hopefully, not too many events happen (since
Expand Down

0 comments on commit d764de6

Please sign in to comment.