Example #1
0
        /// <summary>
        /// Initializes the StateBase instance.
        /// </summary>
        /// <param name="token">The token that identifies the current state.</param>
        public StateBase(StateToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            Token = token;
        }
        /// <summary>
        /// Sets the initial state of the current state machine, and resets its internal state.
        /// </summary>
        /// <param name="initialState">The initial state.</param>
        /// <param name="data">The data to be provided to the initial state.</param>
        public void SetInitialState(StateToken initialState, object data)
        {
            if (initialState == null)
            {
                throw new ArgumentNullException(nameof(initialState));
            }

            RaiseOnExitEvent(null, null);

            CurrentState = null;
            PerformTransitionTo(initialState, data);
        }
Example #3
0
 private void RunCallback(Action <StateToken, object> callback, StateToken stateToken, object data)
 {
     try
     {
         callback(stateToken, data);
     }
     finally
     {
         // ensure isHandlingAsync flag is properly restored
         isHandlingAsync = false;
     }
 }
        private void PerformTransitionTo(StateToken stateToken, object data)
        {
            StateToken targetStateToken = stateToken;
            object     targetData       = data;

            while (true)
            {
                TransitionInfo transition = TransitionTo(targetStateToken, targetData);
                if (transition.TargetStateToken == null)
                {
                    break;
                }

                targetStateToken = transition.TargetStateToken;
                targetData       = transition.TargetStateData;
            }
        }
        private void RaiseOnExitEvent(StateToken stateToken, object data)
        {
            if (CurrentState != null)
            {
                var stateExitEventArgs = new StateExitEventArgs(stateToken, data);

                isPerformActionLocked = true;
                try
                {
                    CurrentState.OnExit(stateExitEventArgs);
                }
                finally
                {
                    isPerformActionLocked = false;
                }
            }
        }
        private TransitionInfo TransitionTo(StateToken stateToken, object data)
        {
            if (stateToken == null)
            {
                throw new ArgumentNullException(nameof(stateToken));
            }

            StateBase state = states.FirstOrDefault(s => s.Token == stateToken);

            if (state == null)
            {
                throw new UnknownStateException(CurrentStateToken, stateToken);
            }

            RaiseOnExitEvent(stateToken, data);

            StateBase oldState = CurrentState;

            CurrentState = state;

            var stateEnterEventArgs = new StateEnterEventArgs(oldState?.Token, data);

            isPerformActionLocked = true;
            try
            {
                OnStateChanged(new StateChangedEventArgs(oldState, CurrentState));

                CurrentState.OnEnter(stateEnterEventArgs);
            }
            finally
            {
                isPerformActionLocked = false;
            }

            return(stateEnterEventArgs.Redirect);
        }
Example #7
0
 /// <summary>
 /// Initializes the StateExitEventArgs instance.
 /// </summary>
 /// <param name="to">The target state of the transition.</param>
 /// <param name="data">The data provided to the target state.</param>
 public StateExitEventArgs(StateToken to, object data)
 {
     To   = to;
     Data = data;
 }
Example #8
0
 /// <summary>
 /// Initializes the StateEnterEventArgs instance.
 /// </summary>
 /// <param name="from">The source state of the transition.</param>
 /// <param name="data">The data provided from the source state, for the target state.</param>
 public StateEnterEventArgs(StateToken from, object data)
 {
     Redirect = new TransitionInfo();
     From     = from;
     Data     = data;
 }
 /// <summary>
 /// Sets the initial state of the current state machine, and resets its internal state.
 /// </summary>
 /// <param name="initialState">The initial state.</param>
 public void SetInitialState(StateToken initialState)
 {
     SetInitialState(initialState, null);
 }
Example #10
0
 /// <summary>
 /// Initializes the UnknownActionException instance.
 /// </summary>
 /// <param name="actionToken">The token of the action that produced the error.</param>
 /// <param name="stateToken">The token of the state that was active when the error has been produced.</param>
 public UnknownActionException(ActionToken actionToken, StateToken stateToken)
     : base(actionToken, stateToken)
 {
 }
Example #11
0
 /// <summary>
 /// Initializes the IllegalActionException instance.
 /// </summary>
 /// <param name="actionToken">The token of the action that produced the error.</param>
 /// <param name="stateToken">The token of the state that was active when the error has been produced.</param>
 /// <param name="message">Custom message explaining the error.</param>
 public IllegalActionException(ActionToken actionToken, StateToken stateToken, string message)
     : base(actionToken, stateToken, message)
 {
 }
Example #12
0
 /// <summary>
 /// Initializes the IllegalActionException instance.
 /// </summary>
 /// <param name="actionToken">The token of the action that produced the error.</param>
 /// <param name="stateToken">The token of the state that was active when the error has been produced.</param>
 public IllegalActionException(ActionToken actionToken, StateToken stateToken)
     : base(actionToken, stateToken)
 {
 }
Example #13
0
 /// <summary>
 /// Initializes the ActionExceptionBase instance.
 /// </summary>
 /// <param name="actionToken">The token of the action that produced the error.</param>
 /// <param name="stateToken">The token of the state that was active when the error has been produced.</param>
 /// <param name="message">Custom message explaining the error.</param>
 protected ActionExceptionBase(ActionToken actionToken, StateToken stateToken, string message)
     : base((message ?? string.Empty) + $" (action: {actionToken}, state: {stateToken})")
 {
     ActionToken = actionToken;
     StateToken  = stateToken;
 }
Example #14
0
 /// <summary>
 /// Initializes the ActionExceptionBase instance.
 /// </summary>
 /// <param name="actionToken">The token of the action that produced the error.</param>
 /// <param name="stateToken">The token of the state that was active when the error has been produced.</param>
 protected ActionExceptionBase(ActionToken actionToken, StateToken stateToken)
     : this(actionToken, stateToken, null)
 {
 }
Example #15
0
 /// <summary>
 /// Initializes the UnknownStateException instance.
 /// </summary>
 /// <param name="sourceStateToken">The token of the source state.</param>
 /// <param name="unknownStateToken">The undeclared token that was targeting the new state.</param>
 public UnknownStateException(StateToken sourceStateToken, StateToken unknownStateToken)
     : base($"(source state: {sourceStateToken}, unknown state: {unknownStateToken})")
 {
     SourceStateToken  = sourceStateToken;
     UnknownStateToken = unknownStateToken;
 }