private async Task <StateRepresentation> EnterStateAsync(StateRepresentation representation, Transition transition, object[] args) { // Enter the new state await representation.EnterAsync(transition, args); if (FiringMode.Immediate.Equals(_firingMode) && !State.Equals(transition.Destination)) { // This can happen if triggers are fired in OnEntry // Must update current representation with updated State representation = GetRepresentation(State); transition = new Transition(transition.Source, State, transition.Trigger, args); } // Recursively enter substates that have an initial transition if (representation.HasInitialTransition) { // Verify that the target state is a substate // Check if state has substate(s), and if an initial transition(s) has been set up. if (!representation.GetSubstates().Any(s => s.UnderlyingState.Equals(representation.InitialTransitionTarget))) { throw new InvalidOperationException($"The target ({representation.InitialTransitionTarget}) for the initial transition is not a substate."); } var initialTransition = new InitialTransition(transition.Source, representation.InitialTransitionTarget, transition.Trigger, args); representation = GetRepresentation(representation.InitialTransitionTarget); // Alert all listeners of initial state transition await _onTransitionedEvent.InvokeAsync(new Transition(transition.Destination, initialTransition.Destination, transition.Trigger, transition.Parameters)); representation = await EnterStateAsync(representation, initialTransition, args); } return(representation); }