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

Expose StateConfiguration.SaveTriggerConfiguration #533

Open
wants to merge 2 commits 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
11 changes: 1 addition & 10 deletions src/Stateless/Reflection/DynamicTransitionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,7 @@ public class DynamicTransitionInfo : TransitionInfo
/// </summary>
public DynamicStateInfos PossibleDestinationStates { get; private set; }

/// <summary>
/// Creates a new instance of <see cref="DynamicTransitionInfo"/>.
/// </summary>
/// <typeparam name="TTrigger">The trigger type.</typeparam>
/// <param name="trigger">The trigger associated with this transition.</param>
/// <param name="guards">The guard conditions associated with this transition.</param>
/// <param name="selector">The destination selector associated with this transition.</param>
/// <param name="possibleStates">The possible destination states.</param>
/// <returns></returns>
public static DynamicTransitionInfo Create<TTrigger>(TTrigger trigger, IEnumerable<InvocationInfo> guards,
internal static DynamicTransitionInfo Create<TTrigger>(TTrigger trigger, IEnumerable<InvocationInfo> guards,
InvocationInfo selector, DynamicStateInfos possibleStates)
{
var transition = new DynamicTransitionInfo
Expand Down
30 changes: 0 additions & 30 deletions src/Stateless/StateConfiguration.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,6 @@ public partial class StateMachine<TState, TTrigger>
{
public partial class StateConfiguration
{
/// <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>
[Obsolete("Use InternalTransitionAsyncIf(TTrigger, Func<bool>, Func<Task>) instead.")]
public StateConfiguration InternalTransitionAsyncIf<TArg0>(TTrigger trigger, Func<bool> guard, Func<Transition, Task> internalAction)
{
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Async(trigger, 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 Down Expand Up @@ -88,19 +71,6 @@ public StateConfiguration InternalTransitionAsyncIf<TArg0, TArg1, TArg2>(Trigger
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>
/// <typeparam name="TArg0"></typeparam>
/// <param name="trigger">The accepted trigger</param>
/// <param name="internalAction">The asynchronous action performed by the internal transition</param>
/// <returns></returns>
[Obsolete("Use InternalTransitionAsync(TTrigger, Func<Transition, Task>) instead.")]
public StateConfiguration InternalTransitionAsync<TArg0>(TTrigger trigger, Func<Transition, Task> internalAction)
{
return InternalTransitionAsyncIf(trigger, () => true, 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>
Expand Down
47 changes: 9 additions & 38 deletions src/Stateless/StateConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ public StateConfiguration Permit(TTrigger trigger, TState destinationState)
/// <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>
/// <param name="trigger"></param>
/// <param name="entryAction"></param>
/// <param name="trigger">The accepted trigger</param>
/// <param name="internalAction">The action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransition(TTrigger trigger, Action<Transition> entryAction)
public StateConfiguration InternalTransition(TTrigger trigger, Action<Transition> internalAction)
{
return InternalTransitionIf(trigger, t => true, entryAction);
return InternalTransitionIf(trigger, t => true, 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>
/// <param name="trigger"></param>
/// <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="entryAction"></param>
/// <param name="internalAction">The action performed by the internal transition</param>
/// <param name="guardDescription">A description of the guard condition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionIf(TTrigger trigger, Func<object[], bool> guard, Action<Transition> entryAction, string guardDescription = null)
public StateConfiguration InternalTransitionIf(TTrigger trigger, Func<object[], bool> guard, Action<Transition> internalAction, string guardDescription = null)
{
if (entryAction == null) throw new ArgumentNullException(nameof(entryAction));
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

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

Expand Down Expand Up @@ -99,35 +99,6 @@ public StateConfiguration InternalTransitionIf(TTrigger trigger, Func<object[],
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>
/// <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 action performed by the internal transition</param>
/// <param name="guardDescription">A description of the guard condition</param>
/// <returns></returns>
public StateConfiguration InternalTransitionIf<TArg0>(TTrigger trigger, Func<object[], bool> guard, Action<Transition> internalAction, string guardDescription = null)
{
if (internalAction == null) throw new ArgumentNullException(nameof(internalAction));

_representation.AddTriggerBehaviour(new InternalTriggerBehaviour.Sync(trigger, guard, (t, args) => internalAction(t), guardDescription));
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>
/// <typeparam name="TArg0"></typeparam>
/// <param name="trigger">The accepted trigger</param>
/// <param name="internalAction">The action performed by the internal transition</param>
/// <returns></returns>
public StateConfiguration InternalTransition<TArg0>(TTrigger trigger, Action<Transition> internalAction)
{
return InternalTransitionIf(trigger, t => true, 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>
Expand Down
14 changes: 9 additions & 5 deletions src/Stateless/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public void Fire(TriggerWithParameters trigger, params object[] args)
public TriggerWithParameters SetTriggerParameters(TTrigger trigger, params Type[] argumentTypes)
{
var configuration = new TriggerWithParameters(trigger, argumentTypes);
SaveTriggerConfiguration(configuration);
SetTriggerParameters(configuration);
return configuration;
}

Expand Down Expand Up @@ -600,7 +600,7 @@ public override string ToString()
public TriggerWithParameters<TArg0> SetTriggerParameters<TArg0>(TTrigger trigger)
{
var configuration = new TriggerWithParameters<TArg0>(trigger);
SaveTriggerConfiguration(configuration);
SetTriggerParameters(configuration);
return configuration;
}

Expand All @@ -615,7 +615,7 @@ public TriggerWithParameters<TArg0> SetTriggerParameters<TArg0>(TTrigger trigger
public TriggerWithParameters<TArg0, TArg1> SetTriggerParameters<TArg0, TArg1>(TTrigger trigger)
{
var configuration = new TriggerWithParameters<TArg0, TArg1>(trigger);
SaveTriggerConfiguration(configuration);
SetTriggerParameters(configuration);
return configuration;
}

Expand All @@ -631,11 +631,15 @@ public TriggerWithParameters<TArg0, TArg1> SetTriggerParameters<TArg0, TArg1>(TT
public TriggerWithParameters<TArg0, TArg1, TArg2> SetTriggerParameters<TArg0, TArg1, TArg2>(TTrigger trigger)
{
var configuration = new TriggerWithParameters<TArg0, TArg1, TArg2>(trigger);
SaveTriggerConfiguration(configuration);
SetTriggerParameters(configuration);
return configuration;
}

void SaveTriggerConfiguration(TriggerWithParameters trigger)
/// <summary>
/// Specify the arguments that must be supplied when a specific trigger is fired.
/// </summary>
/// <param name="trigger">The underlying trigger value and the argument types expected by the trigger.</param>
public void SetTriggerParameters(TriggerWithParameters trigger)
{
if (_triggerConfiguration.ContainsKey(trigger.Trigger))
throw new InvalidOperationException(
Expand Down
21 changes: 0 additions & 21 deletions test/Stateless.Tests/InternalTransitionAsyncFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,6 @@ public async Task InternalTransitionAsyncIf_DeprecatedOverload_AllowGuardWithThr
Assert.True(callbackInvoked);
}

[Fact]
[Obsolete]
public async Task InternalTransitionAsyncIf_DeprecatedOverload_GuardExecutedOnlyOnce()
{
var guardCalls = 0;
var order = new Order
{
Status = OrderStatus.OrderPlaced,
PaymentStatus = PaymentStatus.Pending,
};
var stateMachine = new StateMachine<OrderStatus, OrderStateTrigger>(order.Status);
stateMachine.Configure(OrderStatus.OrderPlaced)
.InternalTransitionAsyncIf<int>(OrderStateTrigger.PaymentCompleted,
() => PreCondition(ref guardCalls),
_ => ChangePaymentState(order, PaymentStatus.Completed));

await stateMachine.FireAsync(OrderStateTrigger.PaymentCompleted);

Assert.Equal(1, guardCalls);
}

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