Skip to content

Commit

Permalink
Merge pull request #575 from HenningNT/chore/refactoring
Browse files Browse the repository at this point in the history
Remove getDestination, and use Destination property instead.
  • Loading branch information
HenningNT authored May 21, 2024
2 parents 2e79d56 + 4a98da1 commit 232b3ee
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 48 deletions.
3 changes: 1 addition & 2 deletions src/Stateless/DynamicTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ public DynamicTriggerBehaviour(TTrigger trigger, Func<object[], TState> destinat
TransitionInfo = info ?? throw new ArgumentNullException(nameof(info));
}

public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
public void GetDestinationState(TState source, object[] args, out TState destination)
{
destination = _destination(args);
return true;
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/Stateless/IgnoredTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ public IgnoredTriggerBehaviour(TTrigger trigger, TransitionGuard transitionGuard
: base(trigger, transitionGuard)
{
}

public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
{
destination = default(TState);
return false;
}
}
}
}
6 changes: 0 additions & 6 deletions src/Stateless/InternalTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ protected InternalTriggerBehaviour(TTrigger trigger, TransitionGuard guard) : ba
public abstract void Execute(Transition transition, object[] args);
public abstract Task ExecuteAsync(Transition transition, object[] args);

public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
{
destination = source;
return false;
}

public class Sync : InternalTriggerBehaviour
{
public Action<Transition, object[]> InternalAction { get; }
Expand Down
7 changes: 0 additions & 7 deletions src/Stateless/ReentryTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ public ReentryTriggerBehaviour(TTrigger trigger, TState destination, TransitionG
{
_destination = destination;
}

public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
{
destination = _destination;
return true;
}
}

}
}
9 changes: 5 additions & 4 deletions src/Stateless/StateMachine.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,22 +219,23 @@ async Task InternalFireOneAsync(TTrigger trigger, params object[] args)
await HandleReentryTriggerAsync(args, representativeState, transition);
break;
}
case DynamicTriggerBehaviour _ when result.Handler.ResultsInTransitionFrom(source, args, out var destination):
case DynamicTriggerBehaviour handler:
{
handler.GetDestinationState(source, args, out var destination);
// Handle transition, and set new state; reentry is permitted from dynamic trigger behaviours.
var transition = new Transition(source, destination, trigger, args);
await HandleTransitioningTriggerAsync(args, representativeState, transition);

break;
}
case TransitioningTriggerBehaviour _ when result.Handler.ResultsInTransitionFrom(source, args, out var destination):
case TransitioningTriggerBehaviour handler:
{
// If a trigger was found on a superstate that would cause unintended reentry, don't trigger.
if (source.Equals(destination))
if (source.Equals(handler.Destination))
break;

// Handle transition, and set new state
var transition = new Transition(source, destination, trigger, args);
var transition = new Transition(source, handler.Destination, trigger, args);
await HandleTransitioningTriggerAsync(args, representativeState, transition);

break;
Expand Down
13 changes: 8 additions & 5 deletions src/Stateless/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,13 @@ private void InternalFireQueued(TTrigger trigger, params object[] args)
/// </summary>
/// <param name="trigger"></param>
/// <param name="args"></param>
void InternalFireOne(TTrigger trigger, params object[] args)
private void InternalFireOne(TTrigger trigger, params object[] args)
{
// If this is a trigger with parameters, we must validate the parameter(s)
if (_triggerConfiguration.TryGetValue(trigger, out TriggerWithParameters configuration))
{
configuration.ValidateParameters(args);
}

var source = State;
var representativeState = GetRepresentation(source);
Expand All @@ -420,22 +422,23 @@ void InternalFireOne(TTrigger trigger, params object[] args)
HandleReentryTrigger(args, representativeState, transition);
break;
}
case DynamicTriggerBehaviour _ when result.Handler.ResultsInTransitionFrom(source, args, out var destination):
case DynamicTriggerBehaviour handler:
{
handler.GetDestinationState(source, args, out var destination);
// Handle transition, and set new state; reentry is permitted from dynamic trigger behaviours.
var transition = new Transition(source, destination, trigger, args);
HandleTransitioningTrigger(args, representativeState, transition);

break;
}
case TransitioningTriggerBehaviour _ when result.Handler.ResultsInTransitionFrom(source, args, out var destination):
case TransitioningTriggerBehaviour handler:
{
// If a trigger was found on a superstate that would cause unintended reentry, don't trigger.
if (source.Equals(destination))
if (source.Equals(handler.Destination))
break;

// Handle transition, and set new state
var transition = new Transition(source, destination, trigger, args);
var transition = new Transition(source, handler.Destination, trigger, args);
HandleTransitioningTrigger(args, representativeState, transition);

break;
Expand Down
6 changes: 0 additions & 6 deletions src/Stateless/TransitioningTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ public TransitioningTriggerBehaviour(TTrigger trigger, TState destination, Trans
{
Destination = destination;
}

public override bool ResultsInTransitionFrom(TState source, object[] args, out TState destination)
{
destination = Destination;
return true;
}
}
}
}
4 changes: 1 addition & 3 deletions src/Stateless/TriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected TriggerBehaviour(TTrigger trigger, TransitionGuard guard)
internal ICollection<Func<object[], bool>> Guards =>_guard.Guards;

/// <summary>
/// GuardConditionsMet is true if all of the guard functions return true
/// GuardConditionsMet is true if all the guard functions return true
/// or if there are no guard functions
/// </summary>
public bool GuardConditionsMet(params object[] args) => _guard.GuardConditionsMet(args);
Expand All @@ -47,8 +47,6 @@ protected TriggerBehaviour(TTrigger trigger, TransitionGuard guard)
/// whose guard function returns false
/// </summary>
public ICollection<string> UnmetGuardConditions(object[] args) => _guard.UnmetGuardConditions(args);

public abstract bool ResultsInTransitionFrom(TState source, object[] args, out TState destination);
}
}
}
13 changes: 9 additions & 4 deletions test/Stateless.Tests/IgnoredTriggerBehaviourFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ public class IgnoredTriggerBehaviourFixture
[Fact]
public void StateRemainsUnchanged()
{
var ignored = new StateMachine<State, Trigger>.IgnoredTriggerBehaviour(Trigger.X, null);
Assert.False(ignored.ResultsInTransitionFrom(State.B, new object[0], out _));
var sm = new StateMachine<State, Trigger>(State.A);
sm.Configure(State.A).Ignore(Trigger.X);

sm.Fire(Trigger.X);

Assert.Equal(State.A, sm.State);
}

[Fact]
Expand All @@ -21,7 +25,7 @@ public void ExposesCorrectUnderlyingTrigger()
Assert.Equal(Trigger.X, ignored.Trigger);
}

protected bool False(params object[] args)
private bool False(params object[] args)
{
return false;
}
Expand All @@ -35,7 +39,7 @@ public void WhenGuardConditionFalse_IsGuardConditionMetIsFalse()
Assert.False(ignored.GuardConditionsMet());
}

protected bool True(params object[] args)
private bool True(params object[] args)
{
return true;
}
Expand All @@ -48,6 +52,7 @@ public void WhenGuardConditionTrue_IsGuardConditionMetIsTrue()

Assert.True(ignored.GuardConditionsMet());
}

[Fact]
public void IgnoredTriggerMustBeIgnoredSync()
{
Expand Down
5 changes: 2 additions & 3 deletions test/Stateless.Tests/SynchronizationContextFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -90,7 +89,7 @@ public async Task Activation_of_state_with_superstate_should_retain_SyncContext(
sm.Configure(State.A)
.OnActivateAsync(CaptureThenLoseSyncContext)
.SubstateOf(State.B);
;

sm.Configure(State.B)
.OnActivateAsync(CaptureThenLoseSyncContext);

Expand All @@ -110,7 +109,7 @@ public async Task Deactivation_of_state_with_superstate_should_retain_SyncContex
sm.Configure(State.A)
.OnDeactivateAsync(CaptureThenLoseSyncContext)
.SubstateOf(State.B);
;

sm.Configure(State.B)
.OnDeactivateAsync(CaptureThenLoseSyncContext);

Expand Down
3 changes: 1 addition & 2 deletions test/Stateless.Tests/TransitioningTriggerBehaviourFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public class TransitioningTriggerBehaviourFixture
public void TransitionsToDestinationState()
{
var transitioning = new StateMachine<State, Trigger>.TransitioningTriggerBehaviour(Trigger.X, State.C, null);
Assert.True(transitioning.ResultsInTransitionFrom(State.B, new object[0], out State destination));
Assert.Equal(State.C, destination);
Assert.Equal(State.C, transitioning.Destination);
}
}
}

0 comments on commit 232b3ee

Please sign in to comment.