IntialEvent triggered multiple times and sets state to the first leaf state
canastro opened this issue · 2 comments
Given a state machine with nested states and coregions
When i boot the state machine
Expected it should set the initial state (in the following case it should be Typing)
Actual it triggers multiple InitialEvent until it reaches the first leaf
I'm not sure if I'm failing to see any specificity on the definition of a state machine, but this seems unexpected
import 'package:fsm2/fsm2.dart';
void main() {
StateMachine.create((g) => g
..initialState<Typing>()
..state<Typing>((b) => b
..onFork<OnValueChange>(
(b) => b..target<Point>()..target<Autocomplete>(),
condition: (e) => true, // Some logic to check if it is a formula
)
..coregion<TypingFormula>((b) => b
..onJoin<OnValueChange, Typing>(
condition: (e) => false, // Some logic to check if it is a formula
)
// Autocomplete state machine
..state<Autocomplete>((b) => b
// Autocomplete events
..on<OnCandidateSelection, AutocompleteList>()
..on<OnFunctionSelection, AutocompleteDetails>()
..on<OnAutocompleteInvalidSelection, AutocompleteUnavailable>()
// Autocomplete states
..state<AutocompleteList>((b) => b)
..state<AutocompleteDetails>((b) => b)
..state<AutocompleteUnavailable>((b) => b))
// Point mode state-mahcine
..state<Point>((b) => b
// Point mode events
..on<OnReferenceSelection, PointReference>()
..on<OnSlotSelection, PointSlot>()
..on<OnDisablePoint, PointDisabled>()
..on<OnPointInvalidSelection, PointUnavailable>()
// Point mode states
..state<PointUnavailable>((b) => b)
..state<PointSlot>((b) => b)
..state<PointReference>((b) => b)
..state<PointDisabled>((b) => b))))
..onTransition((from, e, to) => print(
'Received Event $e in State ${from!.stateType} transitioning to State ${to!.stateType}')));
}
// Definition of states and events, they are all just empty states and events so I removed them from this sample
When I execute this, the InitialEvent gets triggered and takes my machine directly to AutocompleteList
.
Received Event Instance of 'InitialEvent' in State VirtualRoot transitioning to State Typing
Received Event Instance of 'InitialEvent' in State VirtualRoot transitioning to State TypingFormula
Received Event Instance of 'InitialEvent' in State VirtualRoot transitioning to State Autocomplete
Received Event Instance of 'InitialEvent' in State VirtualRoot transitioning to State AutocompleteList
I understand now that a InitialState should be a "leaf" state, and in this case I guess that my state machine should be "invalid".
so cleanup issues and I somehow missed this one (and a couple of others).
You can't directly enter a 'parent state', you can only enter leaf states. The path to the leaf state implies what parent states are entered. The Statemachine MUST always be in one or more leaf states.
So the list of intialEvents is correct as the 'initialState' is 'Typing' which cause the TypingFormual co-region to be initialised (as its the first state in the Typing parent and there is no initialState to override it). Because we are in the TypingFormula co-region the engine then activates each of the nested leaf states 'AutoComplete' and 'AutoCompleteList'.