From 382662894c8149828b394f027357a2595fec5df2 Mon Sep 17 00:00:00 2001 From: rlittlesii <6969701+RLittlesII@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:59:02 -0500 Subject: [PATCH] Add FireAsync(TTrigger, params object[]) overload The ability for this was recently added for TriggersWithParameters. Need similar functionality to reduce the need for reflection to get to the internal method. --- src/Stateless/StateMachine.Async.cs | 13 ++++++++++++ test/Stateless.Tests/AsyncActionsFixture.cs | 23 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Stateless/StateMachine.Async.cs b/src/Stateless/StateMachine.Async.cs index c4e61e98..846842a5 100644 --- a/src/Stateless/StateMachine.Async.cs +++ b/src/Stateless/StateMachine.Async.cs @@ -46,6 +46,19 @@ public Task FireAsync(TTrigger trigger) return InternalFireAsync(trigger, new object[0]); } + /// + /// 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. + /// + /// The trigger to fire. + /// A variable-length parameters list containing arguments. + public Task FireAsync(TTrigger trigger, params object[] args) + { + return InternalFireAsync(trigger, args); + } + /// /// Transition from the current state via the specified trigger in async fashion. /// The target state is determined by the configuration of the current state. diff --git a/test/Stateless.Tests/AsyncActionsFixture.cs b/test/Stateless.Tests/AsyncActionsFixture.cs index 79ff95f5..e74d3e76 100644 --- a/test/Stateless.Tests/AsyncActionsFixture.cs +++ b/test/Stateless.Tests/AsyncActionsFixture.cs @@ -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.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() {