July 2018
Intermediate to advanced
266 pages
7h 11m
English
Because actors are so flexible, we will be able to add the reset functionality easily. Let's go back to ResultsCounter and create an enum for the action to be performed:
enum class Action { INCREASE, RESET}
Then we can update the definition of the actor and the implementation of the coroutine to use an Action channel:
private val actor = actor<Action>(context) { for (msg in channel) { when (msg) { Action.INCREASE -> counter++ Action.RESET -> counter = 0 } notifications.send(counter) }}
Now we are receiving an Action, and either increasing or resetting the counter accordingly. The next step is to update our public API so that increase() uses the channel correctly:
suspend fun increment ...
Read now
Unlock full access