Skip to content

Commit

Permalink
Merge pull request dotnet-state-machine#570 from RLittlesII/feature/f…
Browse files Browse the repository at this point in the history
…ireasync/trigger

feature: Add FireAsync(TTrigger, params object[]) overload
  • Loading branch information
mclift authored Apr 19, 2024
2 parents 66eee8f + 3826628 commit a7b0f09
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Stateless/StateMachine.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public Task FireAsync(TTrigger trigger)
return InternalFireAsync(trigger, new object[0]);
}

/// <summary>
/// Transition from the current state via the specified trigger in async fashion.
/// The target state is determined by the configuration of the current state.
/// Actions associated with leaving the current state and entering the new one
/// will be invoked.
/// </summary>
/// <param name="trigger">The trigger to fire.</param>
/// <param name="args">A variable-length parameters list containing arguments. </param>
public Task FireAsync(TTrigger trigger, params object[] args)
{
return InternalFireAsync(trigger, args);
}

/// <summary>
/// Transition from the current state via the specified trigger in async fashion.
/// The target state is determined by the configuration of the current state.
Expand Down
23 changes: 23 additions & 0 deletions test/Stateless.Tests/AsyncActionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,29 @@ public async Task OnEntryFromAsync_WhenEnteringByAnotherTrigger_InvokesAction()
Assert.False(wasInvoked);
}

[Fact]
public async Task FireAsyncTriggerWithParametersArray()
{
const string expectedParam = "42-Stateless-True-420.69-Y";
string actualParam = null;

var sm = new StateMachine<State, Trigger>(State.A);

sm.Configure(State.A)
.Permit(Trigger.X, State.B);

sm.Configure(State.B)
.OnEntryAsync(t =>
{
actualParam = string.Join("-", t.Parameters);
return Task.CompletedTask;
});

await sm.FireAsync(Trigger.X, 42, "Stateless", true, 420.69, Trigger.Y);

Assert.Equal(expectedParam, actualParam);
}

[Fact]
public async Task FireAsync_TriggerWithMoreThanThreeParameters()
{
Expand Down

0 comments on commit a7b0f09

Please sign in to comment.