allow processing of events in each state.
MODIFY
WalletWasabi.Fluent/State/StateMachine.cs
WalletWasabi.Fluent/State/StateMachine.cs
@@ -47,6 +47,10 @@ 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()
@@ -54,11 +58,6 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
_currentState.Enter();
}
- public void Process()
- {
- _currentState.Process();
- }
-
private void RegisterState(TState state)
{
if (!_states.ContainsKey(state))
@@ -88,8 +87,6 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
{
_currentState.Enter();
}
-
- _currentState.Process();
}
}
@@ -101,7 +98,7 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
private StateContext? _parent;
private List<Action> _entryActions;
private List<Action> _exitActions;
- private List<Action> _onProcessActions;
+ private Dictionary<TTrigger, List<Action>?> _triggerActions;
public TState StateId { get; }
@@ -114,7 +111,7 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
_entryActions = new();
_exitActions = new();
- _onProcessActions = new();
+ _triggerActions = new();
_permittedTransitions = new();
}
@@ -139,9 +136,14 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
return this;
}
- public StateContext OnProcess(Action action)
+ public StateContext OnTrigger(TTrigger trigger, Action action)
{
- _onProcessActions.Add(action);
+ if (_triggerActions[trigger] is null)
+ {
+ _triggerActions[trigger] = new();
+ }
+
+ _triggerActions[trigger]?.Add(action);
return this;
}
@@ -170,13 +172,16 @@ public class StateMachine<TState, TTrigger> where TTrigger : Enum where TState :
}
}
- internal void Process()
+ internal void Process(TTrigger trigger)
{
- foreach (var action in _onProcessActions)
+ if (_triggerActions.ContainsKey(trigger) && _triggerActions[trigger] is { } actions)
+ {
+ foreach (var action in actions)
{
action();
}
}
+ }
internal bool CanTransit(TTrigger trigger)
{

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/CoinJoinStateViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/CoinJoinStateViewModel.cs
@@ -43,7 +43,9 @@ public partial class CoinJoinStateViewModel : ViewModelBase
Stop,
PlebStop,
RoundStartFailed,
- RoundStart
+ RoundStart,
+ BalanceChanged,
+ Timer
}
private readonly StateMachine<State, Trigger> _stateMachine;
@@ -111,7 +113,7 @@ public partial class CoinJoinStateViewModel : ViewModelBase
ConfigureStateMachine(coinJoinManager);
- balanceChanged.Subscribe(_ => _stateMachine.Process());
+ balanceChanged.Subscribe(_ => _stateMachine.Fire(Trigger.BalanceChanged));
PlayCommand = ReactiveCommand.Create(() => _stateMachine.Fire(Trigger.Play));
@@ -144,7 +146,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
_stateMachine.Fire(Trigger.ManualCoinJoinEntered);
})
- .OnProcess(UpdateWalletMixedProgress);
+ .OnEntry(UpdateWalletMixedProgress)
+ .OnTrigger(Trigger.BalanceChanged, UpdateWalletMixedProgress);
_stateMachine.Configure(State.Stopped)
.SubstateOf(State.ManualCoinJoin)
@@ -158,7 +161,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
CurrentStatus = _stoppedMessage;
coinJoinManager.Stop(_wallet);
})
- .OnProcess(UpdateWalletMixedProgress);
+ .OnEntry(UpdateWalletMixedProgress)
+ .OnTrigger(Trigger.BalanceChanged, UpdateWalletMixedProgress);
_stateMachine.Configure(State.ManualPlaying)
.SubstateOf(State.ManualCoinJoin)
@@ -171,7 +175,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
CurrentStatus = _coinJoiningMessage;
coinJoinManager.Start(_wallet);
})
- .OnProcess(UpdateWalletMixedProgress);
+ .OnEntry(UpdateWalletMixedProgress)
+ .OnTrigger(Trigger.BalanceChanged, UpdateWalletMixedProgress);
_stateMachine.Configure(State.ManualFinished)
.SubstateOf(State.ManualCoinJoin)
@@ -216,15 +221,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
IsAutoWaiting = true;
CurrentStatus = _countDownMessage;
})
- .OnProcess(() =>
- {
- ElapsedTime = $"{DateTime.Now - _countDownStarted:mm\\:ss}";
- RemainingTime = $"-{_autoStartTime - DateTime.Now:mm\\:ss}";
-
- var total = (_autoStartTime - _countDownStarted).TotalSeconds;
- var percentage = (DateTime.Now - _countDownStarted).TotalSeconds * 100 / total;
- ProgressValue = percentage;
- })
+ .OnEntry(UpdateCountDown)
+ .OnTrigger(Trigger.Timer, UpdateCountDown)
.OnExit(() =>
{
IsAutoWaiting = false;
@@ -245,7 +243,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
coinJoinManager.Stop(_wallet);
})
- .OnProcess(UpdateWalletMixedProgress);
+ .OnEntry(UpdateWalletMixedProgress)
+ .OnTrigger(Trigger.BalanceChanged, UpdateWalletMixedProgress);
_stateMachine.Configure(State.AutoPlaying)
.SubstateOf(State.AutoCoinJoin)
@@ -261,7 +260,8 @@ public partial class CoinJoinStateViewModel : ViewModelBase
CurrentStatus = _coinJoiningMessage;
coinJoinManager.Start(_wallet);
})
- .OnProcess(UpdateWalletMixedProgress);
+ .OnEntry(UpdateWalletMixedProgress)
+ .OnTrigger(Trigger.BalanceChanged, UpdateWalletMixedProgress);
_stateMachine.Configure(State.AutoFinished)
.SubstateOf(State.AutoCoinJoin)
@@ -278,12 +278,19 @@ public partial class CoinJoinStateViewModel : ViewModelBase
});
}
- private void TimerOnTick()
- {
- if (_stateMachine.State == State.AutoStarting)
+ private void UpdateCountDown()
{
- _stateMachine.Process();
+ ElapsedTime = $"{DateTime.Now - _countDownStarted:mm\\:ss}";
+ RemainingTime = $"-{_autoStartTime - DateTime.Now:mm\\:ss}";
+
+ var total = (_autoStartTime - _countDownStarted).TotalSeconds;
+ var percentage = (DateTime.Now - _countDownStarted).TotalSeconds * 100 / total;
+ ProgressValue = percentage;
}
+
+ private void TimerOnTick()
+ {
+ _stateMachine.Fire(Trigger.Timer);
}
private void UpdateWalletMixedProgress()

allow processing of events in each state.