| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using TeleFlow.Framework.States; |
| | | 3 | | using TeleFlow.Framework.Updates; |
| | | 4 | | |
| | | 5 | | namespace TeleFlow.Framework.Middleware; |
| | | 6 | | |
| | | 7 | | public sealed class UpdateStateMiddleware : IUpdateMiddleware |
| | | 8 | | { |
| | | 9 | | private readonly IStateKeyFactory _stateKeyFactory; |
| | | 10 | | |
| | | 11 | | public UpdateStateMiddleware(IStateKeyFactory stateKeyFactory) |
| | | 12 | | { |
| | 36 | 13 | | ArgumentNullException.ThrowIfNull(stateKeyFactory); |
| | 36 | 14 | | _stateKeyFactory = stateKeyFactory; |
| | 36 | 15 | | } |
| | | 16 | | |
| | | 17 | | public async Task InvokeAsync(UpdateContext context, UpdateDelegate next) |
| | | 18 | | { |
| | 36 | 19 | | ArgumentNullException.ThrowIfNull(context); |
| | 36 | 20 | | ArgumentNullException.ThrowIfNull(next); |
| | | 21 | | |
| | 36 | 22 | | if (_stateKeyFactory.TryCreateStateKey(context, out var key)) |
| | | 23 | | { |
| | 36 | 24 | | var stateStore = context.Services.GetRequiredService<IStateStore>(); |
| | 36 | 25 | | var dataStore = context.Services.GetService<IStateDataStore>(); |
| | 36 | 26 | | var dataSerializer = context.Services.GetService<IStateDataSerializer>(); |
| | 36 | 27 | | var historyStore = context.Services.GetService<IStateHistoryStore>(); |
| | | 28 | | |
| | 36 | 29 | | context.Items[UpdateStateContextKeys.State] = new UpdateState( |
| | 36 | 30 | | stateStore, |
| | 36 | 31 | | key, |
| | 36 | 32 | | dataStore, |
| | 36 | 33 | | dataSerializer, |
| | 36 | 34 | | historyStore); |
| | | 35 | | } |
| | | 36 | | |
| | 36 | 37 | | await next(context).ConfigureAwait(false); |
| | 36 | 38 | | } |
| | | 39 | | } |