We are using actor, similar to that example: <http...
# coroutines
o
We are using actor, similar to that example: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#actors I would like to "wake" the actor every couple of seconds to do work (ie handle messages it is holding as its state) is there an example for this? what is the best approach?
j
Actor always wake up as soon as possible after receiving an element.
Copy code
// take care of thread safety of the queue
val messages: Queue 

// Create an actor design to receive "wake-up" message (Unit can be used as an element type)
// Conflated buffer is usefull to not buffer "wake-up" messages if the process is longuer than the time between two wake-ups
val actor = actor<Unit>(capacity = Channel.CONFLATED) {
    consumeEach {
        // process pending messages
    }
}

init {
    launch {
        while(isActive) {
		
			// wait 2 seconds
            delay(2, TimeUnit.SECONDS)
			
            // awake the actor
            actor.send(Unit)
        }
    }
}
o
thanks