Skip to content

Instantly share code, notes, and snippets.

@abhimuktheeswarar
Last active August 1, 2021 11:22
Show Gist options
  • Save abhimuktheeswarar/5361902c6f58ca2beffcf7d5580e33cf to your computer and use it in GitHub Desktop.
Save abhimuktheeswarar/5361902c6f58ca2beffcf7d5580e33cf to your computer and use it in GitHub Desktop.
customCounterStateMachine for blog
fun CoroutineScope.customCounterStateMachine(channel : ReceiveChannel<CounterMessage>)
= launch {
var counter = 0 // actor state
channel.consumeEach { message ->
ensureActive()
when (message) {
is IncrementCounter -> counter++
is GetCounter -> message.response.complete(counter)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment