| | | 1 | | namespace TeleFlow.Framework.States; |
| | | 2 | | |
| | | 3 | | public 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 | | { |
| | 62 | 21 | | ArgumentNullException.ThrowIfNull(stateStore); |
| | | 22 | | |
| | 62 | 23 | | _stateStore = stateStore; |
| | 62 | 24 | | _dataStore = dataStore; |
| | 62 | 25 | | _dataSerializer = dataSerializer; |
| | 62 | 26 | | _historyStore = historyStore; |
| | | 27 | | Key = key; |
| | 62 | 28 | | } |
| | | 29 | | |
| | | 30 | | public StateKey Key { get; } |
| | | 31 | | |
| | | 32 | | internal string? CurrentStateSnapshot |
| | | 33 | | { |
| | | 34 | | get |
| | | 35 | | { |
| | 8 | 36 | | if (!_isHydrated) |
| | | 37 | | { |
| | 3 | 38 | | throw new InvalidOperationException( |
| | 3 | 39 | | "State snapshot is not hydrated for the current update. Call GetAsync, SetAsync, ClearAsync, Wizard. |
| | | 40 | | } |
| | | 41 | | |
| | 5 | 42 | | return _currentState; |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public UpdateStateData Data |
| | | 47 | | { |
| | | 48 | | get |
| | | 49 | | { |
| | 41 | 50 | | if (_data is not null) |
| | | 51 | | { |
| | 22 | 52 | | return _data; |
| | | 53 | | } |
| | | 54 | | |
| | 19 | 55 | | if (_dataStore is null || _dataSerializer is null) |
| | | 56 | | { |
| | 3 | 57 | | throw new InvalidOperationException( |
| | 3 | 58 | | "State data is not available for the current update. Register state data storage and serializer befo |
| | | 59 | | } |
| | | 60 | | |
| | 16 | 61 | | _data = new UpdateStateData(_dataStore, _dataSerializer, Key); |
| | 16 | 62 | | return _data; |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public UpdateWizard Wizard |
| | | 67 | | { |
| | | 68 | | get |
| | | 69 | | { |
| | 31 | 70 | | if (_wizard is not null) |
| | | 71 | | { |
| | 13 | 72 | | return _wizard; |
| | | 73 | | } |
| | | 74 | | |
| | 18 | 75 | | if (_historyStore is null) |
| | | 76 | | { |
| | 1 | 77 | | throw new InvalidOperationException( |
| | 1 | 78 | | "Wizard is not available for the current update. Register state history storage before using ctx.Sta |
| | | 79 | | } |
| | | 80 | | |
| | 17 | 81 | | _wizard = new UpdateWizard(this, _historyStore); |
| | 17 | 82 | | return _wizard; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | public async ValueTask<string?> GetAsync(CancellationToken cancellationToken = default) |
| | | 87 | | { |
| | 60 | 88 | | await EnsureHydratedAsync(cancellationToken).ConfigureAwait(false); |
| | 59 | 89 | | return _currentState; |
| | 59 | 90 | | } |
| | | 91 | | |
| | | 92 | | public async ValueTask SetAsync(string state, CancellationToken cancellationToken = default) |
| | | 93 | | { |
| | 30 | 94 | | ArgumentException.ThrowIfNullOrWhiteSpace(state); |
| | 30 | 95 | | await _stateStore.SetStateAsync(Key, state, cancellationToken).ConfigureAwait(false); |
| | 27 | 96 | | _currentState = state; |
| | 27 | 97 | | _isHydrated = true; |
| | 27 | 98 | | } |
| | | 99 | | |
| | | 100 | | public ValueTask SetAsync(State state, CancellationToken cancellationToken = default) |
| | | 101 | | { |
| | 12 | 102 | | return SetAsync(state.Id, cancellationToken); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | public ValueTask<bool> IsAsync(State state, CancellationToken cancellationToken = default) |
| | | 106 | | { |
| | 1 | 107 | | if (_isHydrated) |
| | | 108 | | { |
| | 1 | 109 | | return ValueTask.FromResult(string.Equals(_currentState, state.Id, StringComparison.Ordinal)); |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return IsAsyncSlowAsync(state, cancellationToken); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public async ValueTask ClearAsync(CancellationToken cancellationToken = default) |
| | | 116 | | { |
| | 12 | 117 | | await _stateStore.ClearStateAsync(Key, cancellationToken).ConfigureAwait(false); |
| | 11 | 118 | | _currentState = null; |
| | 11 | 119 | | _isHydrated = true; |
| | 11 | 120 | | } |
| | | 121 | | |
| | | 122 | | public async ValueTask ResetAsync(CancellationToken cancellationToken = default) |
| | | 123 | | { |
| | 6 | 124 | | await Data.ClearAsync(cancellationToken).ConfigureAwait(false); |
| | 4 | 125 | | await ClearAsync(cancellationToken).ConfigureAwait(false); |
| | 4 | 126 | | } |
| | | 127 | | |
| | | 128 | | private async ValueTask EnsureHydratedAsync(CancellationToken cancellationToken) |
| | | 129 | | { |
| | 60 | 130 | | if (_isHydrated) |
| | | 131 | | { |
| | 19 | 132 | | return; |
| | | 133 | | } |
| | | 134 | | |
| | 41 | 135 | | _currentState = await _stateStore.GetStateAsync(Key, cancellationToken).ConfigureAwait(false); |
| | 40 | 136 | | _isHydrated = true; |
| | 59 | 137 | | } |
| | | 138 | | |
| | | 139 | | private async ValueTask<bool> IsAsyncSlowAsync(State state, CancellationToken cancellationToken) |
| | | 140 | | { |
| | 0 | 141 | | await EnsureHydratedAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 142 | | return string.Equals(_currentState, state.Id, StringComparison.Ordinal); |
| | 0 | 143 | | } |
| | | 144 | | } |