https://kotlinlang.org logo
#coroutines
Title
# coroutines
g

gregschlom

06/12/2017, 6:31 PM
I’m doing this right now, not sure if that’s the best way:
Copy code
class CounterActor {
    
    fun start(): ActorJob<CounterMsg> {
        return actor<CounterMsg>(CommonPool, block = receive)
    }
    
    val receive: suspend ActorScope<CounterMsg>.() -> Unit = {
        var counter = 0 // actor state
        for (msg in channel) { // iterate over incoming messages
            when (msg) {
                is IncCounter -> counter++
                is GetCounter -> msg.response.send(counter)
            }
        }
    }
}