What are actions in a state machine?
Actions in state machines are functions that are executed during certain key moments within the state transition. They can be triggered when entering or leaving a state, or even during the transition itself. The particularity of actions is that they are executed only once when they are triggered, concluding immediately afterwards.
How do entry actions work?
Entry actions are executed when a new state is entered. For example, when entering a state called "search", you might need to perform a search for possible countries using an endpoint. In this case, you could execute an input action that makes the necessary request to get those countries.
What are exit actions?
Exit actions, on the other hand, are triggered before changing state. Imagine a form where a user enters their personal data. Before changing to the next state, you can execute an exit action that sends this data to the backend via an endpoint. This ensures that the information is processed before the application state changes.
Implementing actions in code
Implementing actions in a state machine involves defining when and how they are executed. The following describes how to do this with a practical example.
How to define an action during transition?
To create an option to be executed during a transition, we must first modify the start event. We change our current target
to an object that includes the action to be executed:
const target = { target: 'search', actions: 'printStart'};
That printStart
should be defined as a function that logs a console.log
showing "printStart". We make sure to place this function outside the states
of our machine:
const actions = { printStart: () => console.log('Print Start')};
How to define input and output actions for a state?
To define input and output actions, we declare them inside the state, before the on
section. So, for the "search" state, we could define them as follows:
const search = { entry: ['printInput'], exit: ['printOutput']};
These actions conform to functions that must be created appropriately:
const actions = { printInput: () => console.log('Print input to search'), printOutput: () => console.log('Print output from search')};
Practical tips for implementing actions
- Clear code structure: make sure that the corresponding actions and functions are well placed outside the main state objects to avoid errors.
- Testing and validation: After implementing an action, test your state machine and verify that the logs run as expected.
- Maintenance and scalability: Organize functions and actions in a modular fashion to facilitate modification or expansion of functionality.
With these concepts, you now have a solid foundation for implementing and using actions in your JavaScript state machines - continue exploring and implementing more cases to consolidate your learning! We look forward to seeing you in the next class to dive into context and its relationship with actions.
Want to see more contributions, questions and answers from the community?