How it works...
In this recipe, we covered how to take advantage of the FSM capabilities that Akka provides. To bring this functionality, we mix in the FSM[S, D] trait, where S represents the super type of the possible states and D represents the super type of the possible data values. In our case, we define a trait TrafficLightState trait, which is extended by Green, Yellow, and Red. For data, we just have Countdown, which we use to count down to change to the next state. The next thing we do is define our states. We do this by calling the when method. This happens inside trafficLightState since the code for the three states is pretty similar and we don't want to repeat ourselves.
When defining a state, you need to provide a partial function ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access