using System; using System.Collections.Generic; using System.Threading; using Cysharp.Threading.Tasks; namespace QuizPleaseTest.Common.StateMachine { public class StatesController : IStatesController { private readonly IReadOnlyDictionary _states; private IState _currentState; public StatesController(IReadOnlyDictionary states) { _states = states; } public async UniTask EnterStateAsync(TEnum code, CancellationToken ct) { if (!_states.TryGetValue(code, out IState newState)) { throw new InvalidOperationException($"State is not registered: {code}"); } if (_currentState != null) { await _currentState.ExitAsync(ct); } _currentState = newState; await _currentState.EnterAsync(ct); } } }