This repo is related to the Erlang/ Elixir meetup on 05/05/2020.
🙏 Organized by https://erlang-connection.eu | twitter: @ErlangConnect
📺 You can find the slides here 👇.
This repo contains the code, the tests and the Liveview demo app shown during the meetup.
There is also some reusable boilerplate (code & tests) to start playing with gen_statem.
The boilerplates, lives in lib/fsm/door
with their related tests in test/fsm/door
.
To start your Phoenix server:
- Setup the project with
mix setup
- Start Phoenix endpoint with
mix phx.server
Now you can visit localhost:4000
from your browser.
code: lib/fsm/light/light_fsm.ex | tests: test/fsm/light/light_fsm_test.exs
- Light is turned off by default
- When the "on" button is pressed, light is turned on
- When the "off" button is pressed, light is turned off
- After a given time, the light turns off itself
- how to write your first finite state machine using the simple
state_function
syntax - the
state_timeout
feature: after a given time in state X, do something - the
state_enter
feature: do something anytime the current state changes - how to write tests for your FSM
code: lib/fsm/payphone/payphone_fsm.ex | tests: test/fsm/payphone/payphone_fsm_test.exs
- pick up the phone
- add some credit
- dial a number
- speak
- hang up the phone
- User can add credit when both dialing and calling.
- User can hang up at any time.
- When user runs out of credit, the phone hangs up itself.
- When the credit is low, the user gets a voice notification
- the other syntax:
handle_event_function
which can be more flexible - the
generic_timeout
feature - how to use absolute time for timeouts
code: lib/fsm/traffic_light/traffic_light_manager_fsm.ex | tests: test/fsm/traffic_light/traffic_light_manager_fsm_test.exs
- PeopleFsm: the FSM for the people's traffic light
- CarFsm: the FSM for the car's traffic light
- ManagerFsm: the FSM that orchestrate the two others in order to achieve your goal
TrafficLight.PeopleFsm:
TrafficLight.CarFsm:
TrafficLight.ManagerFsm (the one that manage the two others):
Crossing the road.
- By default, the traffic lights let the cars pass.
- Occasionally, people ask to cross the road using a button.
- Traffic light color changes to let the people pass.
- After a given time, traffic lights let the cars pass again
- how to decompose your domain in several FSMs
- how to coordinate those FSMs with ... another FSMs :-)
- the
postpone
feature - the
internal_event
feature