Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question on on_entry and on_exit for states #82

Closed
dkumsh opened this issue Jul 17, 2024 · 3 comments
Closed

Question on on_entry and on_exit for states #82

dkumsh opened this issue Jul 17, 2024 · 3 comments

Comments

@dkumsh
Copy link

dkumsh commented Jul 17, 2024

Hi, this feature has been recently added and I wanted to discuss it:

The current code base implements the following order for transitioning from State1 to State2:

  1. Transition action is executed.
  2. on_exit hook of State1 is called.
  3. on_entry hook of State2 is called.
  4. The state machine transitions to State2.

I think this order should be changed to the following for better consistency and integrity:

  1. on_exit hook of State1 is called.
  2. Transition action is executed.
  3. The state machine transitions to State2.
  4. on_entry hook of State2 is called.

Here are my points to support this:

  • The on_exit is the hook of Current State. It is responsible for finalisation tasks before leaving the current state. It must operate while the state machine is still in the the current state

  • Action is what the state change starts from. It can potentially modify the extended state, towards transitioning. After action is complete the state machine is half way in the transition. It's already not in Current State (as its changed the extended state) and isn't yet in New State (it has not yet assigned the new state to self.state ). The update of the state machine's internal state, completes the transition.

  • The on_entry is the hook of New State is responsible for initialisation tasks for the new state. These tasks must be performed after the state machine has transitioned to the new state.

  • Integrity consideration: If the state machine were to execute the transition action before the on_exit of the current state, it could lead to inconsistencies. For example, an action may return an error. That means that the state machine is not going to leave the current state, hence invoking on_exit in this situation is inconsistent with the definition of on_exit. Another example, resources that should be cleaned up in the on_exit might still be in use during the transition action, leading to potential errors or undefined behaviour.

@ryan-summers
Copy link
Collaborator

The reorder sounds reasonable to me. They're pretty fresh freatures so I don't think anyone would be relying on the exact ordering yet personally.

@dkumsh
Copy link
Author

dkumsh commented Jul 17, 2024

Sounds good, I will add it to my active PR.

@dkumsh
Copy link
Author

dkumsh commented Jul 17, 2024

Pushed to the active PR

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

No branches or pull requests

2 participants