Skip to content

Commit

Permalink
New simpler AddTransition method
Browse files Browse the repository at this point in the history
  • Loading branch information
drittich committed Nov 10, 2024
1 parent 1ec4971 commit 337c89e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
35 changes: 34 additions & 1 deletion StateMachine.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,39 @@ public async Task ShouldExecuteActionsInOrder()
Assert.Equal(new[] { "FirstAction", "SecondAction" }, callOrder);
}

/// <summary>
/// Ensures that actions associated with transitions are executed in the correct order.
/// </summary>
[Fact]
public async Task ShouldExecuteActionsInOrderWithSimpleAddTransitionMethod()
{
// Arrange
var logger = new NullLogger<StateMachine<MyStates, MyEvents, MyCustomDto>>();
var callOrder = new List<string>();

var stateMachine = new StateMachine<MyStates, MyEvents, MyCustomDto>(MyStates.Initial, logger);
stateMachine.AddTransition(MyStates.Initial, MyEvents.DoStuff, MyStates.SomeState, async (data, cancellationToken) =>
{
callOrder.Add("FirstAction");
await Task.Delay(1, cancellationToken);
});

stateMachine.AddTransition(MyStates.SomeState, MyEvents.DoOtherStuff, MyStates.Complete, async (data, cancellationToken) =>
{
callOrder.Add("SecondAction");
await Task.Delay(1, cancellationToken);
});

var dto = new MyCustomDto { Prop1 = 1 };

// Act
await stateMachine.GetNextAsync(MyEvents.DoStuff, dto);
await stateMachine.GetNextAsync(MyEvents.DoOtherStuff, dto);

// Assert
Assert.Equal(new[] { "FirstAction", "SecondAction" }, callOrder);
}

/// <summary>
/// Dummy asynchronous operation used as the action delegate in several tests.
/// </summary>
Expand All @@ -715,7 +748,7 @@ private async Task SomeMethodToExecuteAsync(MyCustomDto obj, CancellationToken c
public class MyCustomDto
{
public int Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop2 { get; set; } = string.Empty;
}

/// <summary>
Expand Down
13 changes: 13 additions & 0 deletions StateMachine/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ public void AddTransition(Transition<TStateEnum, TEventEnum, TEventData> transit
_transitions.Add(key, transition);
}

// New simplified AddTransition method
public void AddTransition(
TStateEnum currentState,
TEventEnum evt,
TStateEnum nextState,
Func<TEventData, CancellationToken, Task> action,
Func<TEventData, bool>? guard = null)
{
var transition = new Transition<TStateEnum, TEventEnum, TEventData>(currentState, evt, nextState, action, guard);
AddTransition(transition);
}


/// <summary>
/// Handles an event and transitions to the next state if possible.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion StateMachine/Transition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Transition(
TEventEnum evt,
TStateEnum nextState,
Func<TEventData, CancellationToken, Task> action,
Func<TEventData, bool> guard = null)
Func<TEventData, bool>? guard = null)
{
CurrentState = currentState;
Event = evt;
Expand Down

0 comments on commit 337c89e

Please sign in to comment.