diff --git a/Agent/Task/TASK-0002.md b/Agent/Task/TASK-0002.md index 666aed1..6323788 100644 --- a/Agent/Task/TASK-0002.md +++ b/Agent/Task/TASK-0002.md @@ -1,5 +1,9 @@ # TASK-0002: Базовая архитектура Boot Flow +## Статус + +Ready + ## Цель Создать минимальную архитектурную основу для boot flow: сервисный lifecycle, state-контракты и generic state controller. diff --git a/Assets/Scripts/Boot/States/BootStatesController.cs b/Assets/Scripts/Boot/States/BootStatesController.cs index 2a4efd0..30fc49f 100644 --- a/Assets/Scripts/Boot/States/BootStatesController.cs +++ b/Assets/Scripts/Boot/States/BootStatesController.cs @@ -1,29 +1,31 @@ using System.Collections.Generic; +using System.Threading; +using Cysharp.Threading.Tasks; using QuizPleaseTest.Common.StateMachine; namespace QuizPleaseTest.Boot.States { - public class BootStatesController : StatesController + public class BootStatesController : IStatesController { + private readonly StatesController _statesController; + public BootStatesController( SplashState splashState, LoadState loadState, MenuState menuState) - : base(CreateStates(splashState, loadState, menuState)) { + _statesController = new StatesController( + new Dictionary + { + { BootStateCode.Splash, splashState }, + { BootStateCode.Load, loadState }, + { BootStateCode.Menu, menuState } + }); } - private static IReadOnlyDictionary CreateStates( - SplashState splashState, - LoadState loadState, - MenuState menuState) + public UniTask EnterStateAsync(BootStateCode code, CancellationToken ct) { - return new Dictionary - { - { BootStateCode.Splash, splashState }, - { BootStateCode.Load, loadState }, - { BootStateCode.Menu, menuState } - }; + return _statesController.EnterStateAsync(code, ct); } } } diff --git a/Assets/Scripts/Common/StateMachine/StatesController.cs b/Assets/Scripts/Common/StateMachine/StatesController.cs index a76ff93..714d400 100644 --- a/Assets/Scripts/Common/StateMachine/StatesController.cs +++ b/Assets/Scripts/Common/StateMachine/StatesController.cs @@ -9,6 +9,7 @@ namespace QuizPleaseTest.Common.StateMachine { private readonly IReadOnlyDictionary _states; private IState _currentState; + private bool _hasCurrentState; public StatesController(IReadOnlyDictionary states) { @@ -17,18 +18,25 @@ namespace QuizPleaseTest.Common.StateMachine public async UniTask EnterStateAsync(TEnum code, CancellationToken ct) { + ct.ThrowIfCancellationRequested(); + if (!_states.TryGetValue(code, out IState newState)) { throw new InvalidOperationException($"State is not registered: {code}"); } - if (_currentState != null) + if (_hasCurrentState) { await _currentState.ExitAsync(ct); + _currentState = null; + _hasCurrentState = false; } + ct.ThrowIfCancellationRequested(); + + await newState.EnterAsync(ct); _currentState = newState; - await _currentState.EnterAsync(ct); + _hasCurrentState = true; } } }