Skip to content

Conversation

JeroenSoeters
Copy link

@JeroenSoeters JeroenSoeters commented Apr 13, 2025

Example for the usage of act.StateMachine. This example is inspired by the code lock example from the gen_statem documentation.

We model a code lock as a state machine. This example demonstrates the following features:

  • StateMessageHandler allows you do register a message for a specific state. Here for example we register the handleButtonPress handler that receives the ButtonPress message in the open state.
  • StateCallHandler similar to StateMessageHandler but represents a synchronous call with a return value. In this example we register a StateCallHandler to query the length of the code.
  • StateEnterCallback allows for a callback that is triggered on every state transition. This is useful for orthogonal concerns like logging, auditing etc.
  • StateTimeout provides a timer that sends a message to the state machine on timeout. If the state machine transitions to another state before the timeout, the timer is canceled. In this example we set a state timeout for the open state. After 10 seconds in this state the state machine automatically moves to the locked state.
  • MessageTimeout is useful for measuring inactivity. We set up a message timeout after pressing each button. After 30 seconds of inactivity the buttons are cleared. A MessageTimeout timer is canceled by any type of message or event.

ergo-codelock-example

See ergo-services/ergo#218

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant