Ejemplo n.º 1
0
        public virtual State <TTrig, TName>?Next(TTrig triggerValue)
        {
            State <TTrig, TName>?nextState = CurrentState.Next(triggerValue);

            if (nextState == null)
            {
                return(null);
            }


            var enterArgs = new StateEnterArgs <TTrig, TName>()
            {
                Trigger      = triggerValue,
                CurrentState = CurrentState,
                NextState    = nextState,
                Context      = _context,
            };

            if (CurrentState != nextState)
            {
                var exitArgs = new StateExitArgs <TTrig, TName>()
                {
                    Trigger      = triggerValue,
                    CurrentState = CurrentState,
                    NextState    = nextState,
                };

                if (CurrentState.IsAsyncExit)
                {
                    CurrentState.ExitAsync(exitArgs).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                else
                {
                    CurrentState.Exit(exitArgs);
                }
            }


            PreviousState = CurrentState;
            CurrentState  = nextState;
            OnStateChanged?.Invoke(PreviousState, CurrentState, triggerValue);


            if (CurrentState.IsAsyncEnter)
            {
                CurrentState.ActivateAsync(enterArgs).ConfigureAwait(false).GetAwaiter().GetResult();
            }
            else
            {
                CurrentState.Activate(enterArgs);
            }

            State <TTrig, TName>?toReturn = CurrentState;

            for (int i = 0; i < _context.NextTriggers.Count; i++)
            {
                var next = _context.NextTriggers[i];
                _context.NextTriggers.RemoveAt(i);
                if (next.isAsync)
                {
                    toReturn = NextAsync(next.trig).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                else
                {
                    toReturn = Next(next.trig);
                }
            }

            return(toReturn);
        }