Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for argumented guards in InternalTransitionAsyncIf #534

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions src/Stateless/EntryActionBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public override void Execute(Transition transition, object[] args)
base.Execute(transition, args);
}

public override Task ExecuteAsync(Transition transition, object[] args)
{
Execute(transition, args);
return TaskResult.Done;
}
}

public class Async : EntryActionBehavior
Expand Down Expand Up @@ -93,15 +88,10 @@ public AsyncFrom(TTriggerType trigger, Func<Transition, object[], Task> action,
Trigger = trigger;
}

public override void Execute(Transition transition, object[] args)
{
if (transition.Trigger.Equals(Trigger))
base.Execute(transition, args);
}

public override Task ExecuteAsync(Transition transition, object[] args)
{
Execute(transition, args);
if (transition.Trigger.Equals(Trigger))
return base.ExecuteAsync(transition, args);
return TaskResult.Done;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stateless/InternalTriggerBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Async : InternalTriggerBehaviour
{
readonly Func<Transition, object[], Task> InternalAction;

public Async(TTrigger trigger, Func<bool> guard,Func<Transition, object[], Task> internalAction, string guardDescription = null) : base(trigger, new TransitionGuard(guard, guardDescription))
public Async(TTrigger trigger, Func<object[], bool> guard, Func<Transition, object[], Task> internalAction, string guardDescription = null) : base(trigger, new TransitionGuard(guard, guardDescription))
{
InternalAction = internalAction;
}
Expand Down
58 changes: 51 additions & 7 deletions src/Stateless/StateConfiguration.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public StateConfiguration InternalTransitionAsyncIf(TTrigger trigger, Func<bool>
{
if (entryAction == null) throw new ArgumentNullException(nameof(entryAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, guard, (t, args) => entryAction(t)));
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, (args) => guard(), (t, args) => entryAction(t)));
return this;
}


/// <summary>
/// Add an internal transition to the state machine. An internal action does not cause the Exit and Entry actions to be triggered, and does not change the state of the state machine
/// </summary>
Expand All @@ -35,7 +36,7 @@ public StateConfiguration InternalTransitionAsyncIf(TTrigger trigger, Func<bool>
{
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, guard, (t, args) => internalAction()));
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, (args) => guard(), (t, args) => internalAction()));
return this;
}

Expand All @@ -51,10 +52,11 @@ public StateConfiguration InternalTransitionAsyncIf<TArg0>(TTrigger trigger, Fun
{
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, guard, (t, args) => internalAction(t)));
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, (args) => guard(), (t, args) => internalAction(t)));
return this;
}


/// <summary>
/// Add an internal transition to the state machine. An internal action does not cause the Exit and Entry actions to be triggered, and does not change the state of the state machine
/// </summary>
Expand All @@ -65,10 +67,23 @@ public StateConfiguration InternalTransitionAsyncIf<TArg0>(TTrigger trigger, Fun
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0>(TriggerWithParameters<TArg0> trigger, Func<bool> guard, Func<TArg0, Transition, Task> internalAction)
{
if (trigger == null) throw new ArgumentNullException(nameof(trigger));
return InternalTransitionAsyncIf<TArg0>(trigger, (args) => guard(), internalAction);
}


/// <summary>
/// Add an internal transition to the state machine. An internal action does not cause the Exit and Entry actions to be triggered, and does not change the state of the state machine
/// </summary>
/// <typeparam name="TArg0"></typeparam>
/// <param name="trigger">The accepted trigger</param>
/// <param name="guard">Function that must return true in order for the trigger to be accepted.</param>
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0>(TriggerWithParameters<TArg0> trigger, Func<TArg0, bool> guard, Func<TArg0, Transition, Task> internalAction)
{
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, guard, (t, args) => internalAction(ParameterConversion.Unpack<TArg0>(args, 0), t)));
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, TransitionGuard.ToPackedGuard(guard), (t, args) => internalAction(ParameterConversion.Unpack<TArg0>(args, 0), t)));
return this;
}

Expand All @@ -82,11 +97,25 @@ public StateConfiguration InternalTransitionAsyncIf<TArg0>(TriggerWithParameters
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1>(TriggerWithParameters<TArg0, TArg1> trigger, Func<bool> guard, Func<TArg0, TArg1, Transition, Task> internalAction)
{
return InternalTransitionAsyncIf<TArg0, TArg1>(trigger, (arg0, arg1) => guard(), internalAction);
}

/// <summary>
/// Add an internal transition to the state machine. An internal action does not cause the Exit and Entry actions to be triggered, and does not change the state of the state machine
/// </summary>
/// <typeparam name="TArg0"></typeparam>
/// <typeparam name="TArg1"></typeparam>
/// <param name="trigger">The accepted trigger</param>
/// <param name="guard">Function that must return true in order for the trigger to be accepted.</param>
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1>(TriggerWithParameters<TArg0, TArg1> trigger, Func<TArg0, TArg1, bool> guard, Func<TArg0, TArg1, Transition, Task> internalAction)
{
if (trigger == null) throw new ArgumentNullException(nameof(trigger));
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, guard, (t, args) => internalAction(
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, TransitionGuard.ToPackedGuard(guard), (t, args) => internalAction(
ParameterConversion.Unpack<TArg0>(args, 0),
ParameterConversion.Unpack<TArg1>(args, 1), t)));
return this;
Expand All @@ -103,11 +132,26 @@ public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1>(TriggerWithPar
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1, TArg2>(TriggerWithParameters<TArg0, TArg1, TArg2> trigger, Func<bool> guard, Func<TArg0, TArg1, TArg2, Transition, Task> internalAction)
{
return InternalTransitionAsyncIf<TArg0, TArg1, TArg2>(trigger, (arg0, arg1, arg2) => guard(), internalAction);
}

/// <summary>
/// Add an internal transition to the state machine. An internal action does not cause the Exit and Entry actions to be triggered, and does not change the state of the state machine
/// </summary>
/// <typeparam name="TArg0"></typeparam>
/// <typeparam name="TArg1"></typeparam>
/// <typeparam name="TArg2"></typeparam>
/// <param name="trigger">The accepted trigger</param>
/// <param name="guard">Function that must return true in order for the trigger to be accepted.</param>
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1, TArg2>(TriggerWithParameters<TArg0, TArg1, TArg2> trigger, Func<TArg0, TArg1, TArg2, bool> guard, Func<TArg0, TArg1, TArg2, Transition, Task> internalAction)
{
if (trigger == null) throw new ArgumentNullException(nameof(trigger));
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, guard, (t, args) => internalAction(
_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger.Trigger, TransitionGuard.ToPackedGuard(guard), (t, args) => internalAction(
ParameterConversion.Unpack<TArg0>(args, 0),
ParameterConversion.Unpack<TArg1>(args, 1),
ParameterConversion.Unpack<TArg2>(args, 2), t)));
Expand Down
39 changes: 39 additions & 0 deletions test/Stateless.Tests/InternalTransitionAsyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@ namespace Stateless.Tests
{
public class InternalTransitionAsyncFixture
{

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task InternalTransitionAsyncIf_NoArgumentGuard(bool expected)
{
bool? actual = null;
var stateMachine = new StateMachine<State, Trigger>(State.A);

var triggerX = stateMachine.SetTriggerParameters<bool>(Trigger.X);

stateMachine.Configure(State.A)
.InternalTransitionAsyncIf(triggerX, () => expected, (arg, s) => Task.Run(() => actual = true ))
.InternalTransitionAsyncIf(triggerX, () => !expected, (arg, s) => Task.Run(() => actual = false));

await stateMachine.FireAsync(triggerX, expected);

Assert.Equal(expected, actual);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task InternalTransitionAsyncIf_WithArgumentGuard(bool expected)
{
bool? actual = null;
var stateMachine = new StateMachine<State, Trigger>(State.A);

var triggerX = stateMachine.SetTriggerParameters<bool>(Trigger.X);

stateMachine.Configure(State.A)
.InternalTransitionAsyncIf(triggerX, (arg) => arg, (arg, s) => Task.Run(() => actual = true))
.InternalTransitionAsyncIf(triggerX, (arg) => !arg, (arg, s) => Task.Run(() => actual = false));

await stateMachine.FireAsync(triggerX, expected);

Assert.Equal(expected, actual);
}

/// <summary>
/// This unit test demonstrated bug report #417
/// </summary>
Expand Down