| | | 1 | | namespace TeleFlow.Framework.Updates; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Stores the current update context for one DI update scope so middleware, dispatchers, and scoped |
| | | 5 | | /// application services observe the same update identity during processing. |
| | | 6 | | /// </summary> |
| | | 7 | | internal sealed class DefaultUpdateContextAccessor : IUpdateContextAccessor, IUpdateContextAccessorInitializer |
| | | 8 | | { |
| | | 9 | | private UpdateContext? _current; |
| | | 10 | | |
| | 1 | 11 | | public bool IsAvailable => _current is not null; |
| | | 12 | | |
| | 9 | 13 | | public UpdateContext Current => _current ?? throw new InvalidOperationException( |
| | 9 | 14 | | "No current update is available. IUpdateContextAccessor can only be used inside TeleFlow update processing, not |
| | | 15 | | |
| | | 16 | | public bool TryGetCurrent(out UpdateContext context) |
| | | 17 | | { |
| | 32 | 18 | | if (_current is null) |
| | | 19 | | { |
| | 10 | 20 | | context = null!; |
| | 10 | 21 | | return false; |
| | | 22 | | } |
| | | 23 | | |
| | 22 | 24 | | context = _current; |
| | 22 | 25 | | return true; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public void Initialize(UpdateContext context) |
| | | 29 | | { |
| | 87 | 30 | | ArgumentNullException.ThrowIfNull(context); |
| | | 31 | | |
| | 87 | 32 | | if (_current is not null) |
| | | 33 | | { |
| | 0 | 34 | | throw new InvalidOperationException("The current update accessor has already been initialized for this updat |
| | | 35 | | } |
| | | 36 | | |
| | 87 | 37 | | _current = context; |
| | 87 | 38 | | } |
| | | 39 | | |
| | | 40 | | public void Clear(UpdateContext context) |
| | | 41 | | { |
| | 87 | 42 | | ArgumentNullException.ThrowIfNull(context); |
| | | 43 | | |
| | 87 | 44 | | if (ReferenceEquals(_current, context)) |
| | | 45 | | { |
| | 87 | 46 | | _current = null; |
| | | 47 | | } |
| | 87 | 48 | | } |
| | | 49 | | } |