StateMachine, processes OnTriggers before transistioning.
MODIFY
WalletWasabi.Fluent/State/StateMachine.cs
WalletWasabi.Fluent/State/StateMachine.cs
@@ -73,6 +73,8 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
public void Fire(TTrigger trigger)
{
+ _currentState.Process(trigger);
+
if (_currentState.CanTransit(trigger))
{
var destination = _currentState.GetDestination(trigger);
@@ -89,10 +91,6 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
Goto(trigger, _currentState.Parent.StateId, true, false);
Goto(trigger, _currentState.GetDestination(trigger));
}
- else
- {
- _currentState.Process(trigger);
- }
}
public void Start()

MODIFY
WalletWasabi.Tests/UnitTests/StateMachineTests.cs
WalletWasabi.Tests/UnitTests/StateMachineTests.cs
@@ -260,6 +260,33 @@ public class StateMachineTests
Assert.False(sut.IsInState(JukeBoxState.PausedChild));
}
+ [Fact]
+ public void OnTriggers_Are_Handled_Before_Transitions()
+ {
+ StateMachine<JukeBoxState, JukeBoxTrigger> sut =
+ new StateMachine<JukeBoxState, JukeBoxTrigger>(JukeBoxState.Paused);
+
+ sut.Configure(JukeBoxState.Playing)
+ .Permit(JukeBoxTrigger.Pause, JukeBoxState.PausedChild);
+
+ bool onTriggerCalled = false;
+
+ sut.Configure(JukeBoxState.Paused)
+ .Permit(JukeBoxTrigger.Play, JukeBoxState.Playing)
+ .OnTrigger(JukeBoxTrigger.Play, () =>
+ {
+ onTriggerCalled = true;
+ });
+
+ sut.Start();
+
+ sut.Fire(JukeBoxTrigger.Play);
+
+ Assert.Equal(JukeBoxState.Playing, sut.State);
+ Assert.True(sut.IsInState(JukeBoxState.Playing));
+ Assert.True(onTriggerCalled);
+ }
+
private enum PhoneState
{
Disconnected,

StateMachine, processes OnTriggers before transistioning.