< Summary

Information
Class: TeleFlow.Framework.States.UpdateState
Assembly: TeleFlow.Framework.Core
File(s): /_/src/TeleFlow.Framework.Core/States/UpdateState.cs
Line coverage
92%
Covered lines: 47
Uncovered lines: 4
Coverable lines: 51
Total lines: 144
Line coverage: 92.1%
Branch coverage
93%
Covered branches: 15
Total branches: 16
Branch coverage: 93.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_CurrentStateSnapshot()100%22100%
get_Data()100%66100%
get_Wizard()100%44100%
GetAsync()100%11100%
SetAsync()100%11100%
SetAsync(...)100%11100%
IsAsync(...)50%2266.66%
ClearAsync()100%11100%
ResetAsync()100%11100%
EnsureHydratedAsync()100%22100%
IsAsyncSlowAsync()100%210%

File(s)

/_/src/TeleFlow.Framework.Core/States/UpdateState.cs

#LineLine coverage
 1namespace TeleFlow.Framework.States;
 2
 3public sealed class UpdateState
 4{
 5    private readonly IStateStore _stateStore;
 6    private readonly IStateDataStore? _dataStore;
 7    private readonly IStateDataSerializer? _dataSerializer;
 8    private readonly IStateHistoryStore? _historyStore;
 9    private UpdateStateData? _data;
 10    private UpdateWizard? _wizard;
 11    private string? _currentState;
 12    private bool _isHydrated;
 13
 14    public UpdateState(
 15        IStateStore stateStore,
 16        StateKey key,
 17        IStateDataStore? dataStore = null,
 18        IStateDataSerializer? dataSerializer = null,
 19        IStateHistoryStore? historyStore = null)
 20    {
 6221        ArgumentNullException.ThrowIfNull(stateStore);
 22
 6223        _stateStore = stateStore;
 6224        _dataStore = dataStore;
 6225        _dataSerializer = dataSerializer;
 6226        _historyStore = historyStore;
 27        Key = key;
 6228    }
 29
 30    public StateKey Key { get; }
 31
 32    internal string? CurrentStateSnapshot
 33    {
 34        get
 35        {
 836            if (!_isHydrated)
 37            {
 338                throw new InvalidOperationException(
 339                    "State snapshot is not hydrated for the current update. Call GetAsync, SetAsync, ClearAsync, Wizard.
 40            }
 41
 542            return _currentState;
 43        }
 44    }
 45
 46    public UpdateStateData Data
 47    {
 48        get
 49        {
 4150            if (_data is not null)
 51            {
 2252                return _data;
 53            }
 54
 1955            if (_dataStore is null || _dataSerializer is null)
 56            {
 357                throw new InvalidOperationException(
 358                    "State data is not available for the current update. Register state data storage and serializer befo
 59            }
 60
 1661            _data = new UpdateStateData(_dataStore, _dataSerializer, Key);
 1662            return _data;
 63        }
 64    }
 65
 66    public UpdateWizard Wizard
 67    {
 68        get
 69        {
 3170            if (_wizard is not null)
 71            {
 1372                return _wizard;
 73            }
 74
 1875            if (_historyStore is null)
 76            {
 177                throw new InvalidOperationException(
 178                    "Wizard is not available for the current update. Register state history storage before using ctx.Sta
 79            }
 80
 1781            _wizard = new UpdateWizard(this, _historyStore);
 1782            return _wizard;
 83        }
 84    }
 85
 86    public async ValueTask<string?> GetAsync(CancellationToken cancellationToken = default)
 87    {
 6088        await EnsureHydratedAsync(cancellationToken).ConfigureAwait(false);
 5989        return _currentState;
 5990    }
 91
 92    public async ValueTask SetAsync(string state, CancellationToken cancellationToken = default)
 93    {
 3094        ArgumentException.ThrowIfNullOrWhiteSpace(state);
 3095        await _stateStore.SetStateAsync(Key, state, cancellationToken).ConfigureAwait(false);
 2796        _currentState = state;
 2797        _isHydrated = true;
 2798    }
 99
 100    public ValueTask SetAsync(State state, CancellationToken cancellationToken = default)
 101    {
 12102        return SetAsync(state.Id, cancellationToken);
 103    }
 104
 105    public ValueTask<bool> IsAsync(State state, CancellationToken cancellationToken = default)
 106    {
 1107        if (_isHydrated)
 108        {
 1109            return ValueTask.FromResult(string.Equals(_currentState, state.Id, StringComparison.Ordinal));
 110        }
 111
 0112        return IsAsyncSlowAsync(state, cancellationToken);
 113    }
 114
 115    public async ValueTask ClearAsync(CancellationToken cancellationToken = default)
 116    {
 12117        await _stateStore.ClearStateAsync(Key, cancellationToken).ConfigureAwait(false);
 11118        _currentState = null;
 11119        _isHydrated = true;
 11120    }
 121
 122    public async ValueTask ResetAsync(CancellationToken cancellationToken = default)
 123    {
 6124        await Data.ClearAsync(cancellationToken).ConfigureAwait(false);
 4125        await ClearAsync(cancellationToken).ConfigureAwait(false);
 4126    }
 127
 128    private async ValueTask EnsureHydratedAsync(CancellationToken cancellationToken)
 129    {
 60130        if (_isHydrated)
 131        {
 19132            return;
 133        }
 134
 41135        _currentState = await _stateStore.GetStateAsync(Key, cancellationToken).ConfigureAwait(false);
 40136        _isHydrated = true;
 59137    }
 138
 139    private async ValueTask<bool> IsAsyncSlowAsync(State state, CancellationToken cancellationToken)
 140    {
 0141        await EnsureHydratedAsync(cancellationToken).ConfigureAwait(false);
 0142        return string.Equals(_currentState, state.Id, StringComparison.Ordinal);
 0143    }
 144}