// Recursively goes up the the state hierarchy until a state is found // that will handle the event. private TransitionResult Dispatch(State origin, int eventID, object[] args) { TransitionResult transResult = notFiredResult; // If there are any Transitions for this event. if (transitions[eventID] != null) { // Iterate through the Transitions until one of them fires. foreach (Transition trans in transitions[eventID]) { transResult = trans.Fire(origin, args); if (transResult.HasFired) { // Break out of loop. We're finished. break; } } } // Else if there are no Transitions for this event and there is a // superstate. else if (Superstate != null) { // Dispatch the event to the superstate. transResult = Superstate.Dispatch(origin, eventID, args); } return(transResult); }
/// <summary> /// Fires the transition. /// </summary> /// <param name="origin"> /// The State that originally received the event. /// </param> /// <param name="args"> /// The arguments accompanying the event. /// </param> /// <returns> /// A TransitionResult object representing the results of the transition. /// </returns> internal TransitionResult Fire(State origin, object[] args) { TransitionResult result; // If the transition should fire. if (ShouldFire(args)) { State newState = origin; // If this is not an internal transition. if (Target != null) { State o = origin; // Unwind up from the state that originally received the event // to the source state. while (o != Source) { o.Exit(); o = o.Superstate; } Fire(Source, Target, args); newState = Target.EnterByHistory(); } // Else if this is an internal transition. else { PerformActions(args); } result = new TransitionResult(true, newState, exceptionResult); } // Else the transition should not fire. else { result = notFiredResult; } return(result); }