appccelerate / statemachine

A .net library that lets you build state machines (hierarchical, async with fluent definition syntax and reporting capabilities).
Apache License 2.0
481 stars 128 forks source link

What is supposed to happen when an event is received in the middle of a state transition? #75

Closed mdwhitten closed 3 years ago

mdwhitten commented 3 years ago

I am curious what the intended behavior is for an event that is fired during a state transition. Both of my states define how that event should be handled. However, when that event occurs during transitioning between states (and the state machine is busy executing transition actions), it seems like that action is being ignored. In other words, neither the action for the previous nor target state is invoked for the event.

Is this expected behavior? It's very likely that this will occur due in my application as the state transition is also likely to cause the system to send events, and I'd like to ensure that they are routed properly. My expectation would be that they are queued up and handled in the new state.

I'm using the async active machine. Thanks!

ursenzler commented 3 years ago

Your assumption is correct. Events are always queued. So if you fire an event onto the state machine during a transition action, the event gets queued and once the current transition is finished, the new event is processed. That holds for all kinds of state machines (sync/async, passive/active).

If this is not the observed behaviour in your case then maybe it helps when you add an extension so that you can see what is going on (either with the debugger or with some log statements). A bit more info about extensions can be found here: https://github.com/appccelerate/statemachine/blob/nullable_reference_types/documentation/extensions.md

Using the async active state machine is the correct option in the scenario that you describe.

Let me know if this helps, otherwise, we look further.