Skip to content

Commit

Permalink
Added FireAsync(TriggerWithParameters, params object[]) overload
Browse files Browse the repository at this point in the history
This is async counterpart to existing Fire(TriggerWithParameters, params object[]) method. Required for firing triggers with more than 3 parameters asynchronously.
  • Loading branch information
DeepakParamkusam committed Jul 14, 2023
1 parent dc70a9d commit 63c8d51
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Stateless/StateMachine.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ 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>
/// <exception cref="System.InvalidOperationException">The current state does
/// not allow the trigger to be fired.</exception>
public Task FireAsync(TriggerWithParameters trigger, params object[] args)
{
if (trigger == null) throw new ArgumentNullException(nameof(trigger));

return InternalFireAsync(trigger.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
25 changes: 25 additions & 0 deletions test/Stateless.Tests/AsyncActionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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;
});

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);
}
}
}

Expand Down

0 comments on commit 63c8d51

Please sign in to comment.