kotlin coroutine actor code example
Example: kotlin coroutine actor
sealed class Msg
object IncCounter: Msg()
object PrintCounter: Msg()
class GetCounter(val resp: CompletableDeferred<Int>):Msg()
fun CoroutineScope.counterActor() = actor<Msg> {
var counter = 0 // Actor state
for (msg in channel) {
when (msg) {
is IncCounter -> counter++
is PrintCounter -> print(counter)
is GetCounter -> msg.resp.complete(counter)
}
}
}