From 63c8d51fc45454bb94810a63617514e43de62fba Mon Sep 17 00:00:00 2001 From: DeepakParamkusam Date: Fri, 14 Jul 2023 17:31:26 +0200 Subject: [PATCH] Added FireAsync(TriggerWithParameters, params object[]) overload This is async counterpart to existing Fire(TriggerWithParameters, params object[]) method. Required for firing triggers with more than 3 parameters asynchronously. --- src/Stateless/StateMachine.Async.cs | 17 ++++++++++++++ test/Stateless.Tests/AsyncActionsFixture.cs | 25 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/Stateless/StateMachine.Async.cs b/src/Stateless/StateMachine.Async.cs index b319813a..c4e61e98 100644 --- a/src/Stateless/StateMachine.Async.cs +++ b/src/Stateless/StateMachine.Async.cs @@ -46,6 +46,23 @@ 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. + /// The current state does + /// not allow the trigger to be fired. + public Task FireAsync(TriggerWithParameters trigger, params object[] args) + { + if (trigger == null) throw new ArgumentNullException(nameof(trigger)); + + return InternalFireAsync(trigger.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 e8dd5dba..79ff95f5 100644 --- a/test/Stateless.Tests/AsyncActionsFixture.cs +++ b/test/Stateless.Tests/AsyncActionsFixture.cs @@ -540,6 +540,31 @@ public async Task OnEntryFromAsync_WhenEnteringByAnotherTrigger_InvokesAction() Assert.False(wasInvoked); } + + [Fact] + public async Task FireAsync_TriggerWithMoreThanThreeParameters() + { + 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; + }); + + var parameterizedX = sm.SetTriggerParameters(Trigger.X, typeof(int), typeof(string), typeof(bool), typeof(double), typeof(Trigger)); + + await sm.FireAsync(parameterizedX, 42, "Stateless", true, 420.69, Trigger.Y); + + Assert.Equal(expectedParam, actualParam); + } } }