Skip to content

Commit

Permalink
Optimize firing group entry/exit handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
canton7 committed Sep 1, 2015
1 parent 562d8b9 commit 4de7e5b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/StateMechanic/StateMachineKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public void CoordinateTransition(TState from, TState to, IEvent @event, bool isI
private void ExitChildStateMachine(IStateMachine<TState> childStateMachine, TState to, IEvent @event)
{
if (childStateMachine.CurrentState != null && childStateMachine.CurrentState.ChildStateMachine != null)
{
this.ExitChildStateMachine(childStateMachine.CurrentState.ChildStateMachine, to, @event);
}

this.ExitState(new StateHandlerInfo<TState>(childStateMachine.CurrentState, to, @event));

Expand All @@ -73,9 +71,7 @@ private void EnterChildStateMachine(IStateMachine<TState> childStateMachine, TSt
this.EnterState(new StateHandlerInfo<TState>(from, childStateMachine.InitialState, @event));

if (childStateMachine.InitialState.ChildStateMachine != null)
{
this.EnterChildStateMachine(childStateMachine.InitialState.ChildStateMachine, from, @event);
}
}

private void ExitState(StateHandlerInfo<TState> info)
Expand All @@ -89,8 +85,12 @@ private void ExitState(StateHandlerInfo<TState> info)
throw new InternalTransitionFaultException(info.From, info.To, info.Event, FaultedComponent.ExitHandler, e);
}

foreach (var group in info.From.Groups.Reverse().Except(info.To.Groups))
foreach (var group in info.From.Groups.Reverse())
{
// We could use .Except, but that uses a HashSet which is complete overkill here
if (info.To.Groups.Contains(group))
continue;

try
{
group.FireExitHandler(info);
Expand All @@ -113,8 +113,12 @@ private void EnterState(StateHandlerInfo<TState> info)
throw new InternalTransitionFaultException(info.From, info.To, info.Event, FaultedComponent.EntryHandler, e);
}

foreach (var group in info.To.Groups.Except(info.From.Groups))
foreach (var group in info.To.Groups)
{
// We could use .Except, but that uses a HashSet which is complete overkill here
if (info.From.Groups.Contains(group))
continue;

try
{
group.FireEntryHandler(info);
Expand Down

0 comments on commit 4de7e5b

Please sign in to comment.