| | | 1 | | namespace TeleFlow.Framework.States; |
| | | 2 | | |
| | | 3 | | public sealed class UpdateWizard |
| | | 4 | | { |
| | | 5 | | private readonly UpdateState _state; |
| | | 6 | | private readonly IStateHistoryStore _historyStore; |
| | | 7 | | |
| | | 8 | | internal UpdateWizard( |
| | | 9 | | UpdateState state, |
| | | 10 | | IStateHistoryStore historyStore) |
| | | 11 | | { |
| | 17 | 12 | | ArgumentNullException.ThrowIfNull(state); |
| | 17 | 13 | | ArgumentNullException.ThrowIfNull(historyStore); |
| | | 14 | | |
| | 17 | 15 | | _state = state; |
| | 17 | 16 | | _historyStore = historyStore; |
| | 17 | 17 | | } |
| | | 18 | | |
| | | 19 | | public State Current |
| | | 20 | | { |
| | | 21 | | get |
| | | 22 | | { |
| | 8 | 23 | | var currentState = _state.CurrentStateSnapshot; |
| | 5 | 24 | | if (string.IsNullOrWhiteSpace(currentState)) |
| | | 25 | | { |
| | 1 | 26 | | throw new InvalidOperationException( |
| | 1 | 27 | | "Wizard current state is not available because the current update has no active state."); |
| | | 28 | | } |
| | | 29 | | |
| | 4 | 30 | | return State.Create(currentState); |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | 3 | 34 | | public UpdateStateData Data => _state.Data; |
| | | 35 | | |
| | | 36 | | public async ValueTask<State?> GetCurrentAsync(CancellationToken cancellationToken = default) |
| | | 37 | | { |
| | 2 | 38 | | var currentState = await _state.GetAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 39 | | return string.IsNullOrWhiteSpace(currentState) |
| | 2 | 40 | | ? null |
| | 2 | 41 | | : State.Create(currentState); |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | public async ValueTask GoToAsync( |
| | | 45 | | State state, |
| | | 46 | | CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 8 | 48 | | var currentState = await _state.GetAsync(cancellationToken).ConfigureAwait(false); |
| | 8 | 49 | | await _state.SetAsync(state, cancellationToken).ConfigureAwait(false); |
| | | 50 | | |
| | 7 | 51 | | if (!string.IsNullOrWhiteSpace(currentState)) |
| | | 52 | | { |
| | 6 | 53 | | await _historyStore.PushAsync(_state.Key, currentState, cancellationToken).ConfigureAwait(false); |
| | | 54 | | } |
| | 6 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async ValueTask BackAsync(CancellationToken cancellationToken = default) |
| | | 58 | | { |
| | 4 | 59 | | var previousState = await _historyStore.PeekAsync(_state.Key, cancellationToken).ConfigureAwait(false); |
| | 4 | 60 | | if (string.IsNullOrWhiteSpace(previousState)) |
| | | 61 | | { |
| | 1 | 62 | | throw new InvalidOperationException( |
| | 1 | 63 | | "Wizard cannot go back because state history is empty for the current update."); |
| | | 64 | | } |
| | | 65 | | |
| | 3 | 66 | | await _state.SetAsync(previousState, cancellationToken).ConfigureAwait(false); |
| | | 67 | | |
| | 2 | 68 | | var poppedState = await _historyStore.PopAsync(_state.Key, cancellationToken).ConfigureAwait(false); |
| | 2 | 69 | | if (!string.Equals(poppedState, previousState, StringComparison.Ordinal)) |
| | | 70 | | { |
| | 0 | 71 | | throw new InvalidOperationException( |
| | 0 | 72 | | "Wizard state history changed while going back. State history storage returned an unexpected previous st |
| | | 73 | | } |
| | 2 | 74 | | } |
| | | 75 | | |
| | | 76 | | public async ValueTask ResetAsync(CancellationToken cancellationToken = default) |
| | | 77 | | { |
| | 4 | 78 | | await _state.Data.ClearAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 79 | | await _historyStore.ClearAsync(_state.Key, cancellationToken).ConfigureAwait(false); |
| | 2 | 80 | | await _state.ClearAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 81 | | } |
| | | 82 | | } |