Files
QuizPlease/Assets/Scripts/Common/StateMachine/StatesController.cs
T
horooko 2f745ba328 feat(task-0002): implement state controller interface with cancellation support and update task status
- Refactor BootStatesController to implement IStatesController interface
- Move state dictionary creation into constructor, remove static helper method
- Add CancellationToken validation before state transitions in StatesController
- Track current state presence with _hasCurrentState flag for safety
- Update TASK-0002 with Ready status

Выполнена задача TASK-0002 и обновлён статус:
- Рефакторинг BootStatesController для реализации интерфейса IStatesController
- Перемещено создание словаря состояний в конструктор, удалён статический вспомогательный метод
- Добавлена валидация CancellationToken перед переходами между состояниями в StatesController
- Добавлен флаг _hasCurrentState для отслеживания текущего состояния
- Обновлён статус TASK-0002 до Ready
2026-05-27 04:04:59 +07:00

43 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
namespace QuizPleaseTest.Common.StateMachine
{
public class StatesController<TEnum> : IStatesController<TEnum>
{
private readonly IReadOnlyDictionary<TEnum, IState> _states;
private IState _currentState;
private bool _hasCurrentState;
public StatesController(IReadOnlyDictionary<TEnum, IState> states)
{
_states = states;
}
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 (_hasCurrentState)
{
await _currentState.ExitAsync(ct);
_currentState = null;
_hasCurrentState = false;
}
ct.ThrowIfCancellationRequested();
await newState.EnterAsync(ct);
_currentState = newState;
_hasCurrentState = true;
}
}
}