| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | |
|
| | 5 | | #if NETSTANDARD2_0 |
| | 6 | | using Amusoft.Toolkit.Threading.Compat; |
| | 7 | | #endif |
| | 8 | |
|
| | 9 | | namespace Amusoft.Toolkit.Threading; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Ambient scope utility class |
| | 13 | | /// </summary> |
| | 14 | | /// <typeparam name="T"></typeparam> |
| | 15 | | public abstract class AmbientScope<T> : IDisposable |
| | 16 | | where T: AmbientScope<T> |
| | 17 | | { |
| 1 | 18 | | private static readonly AsyncLocal<Stack<T>> GlobalScopes = new() |
| 1 | 19 | | { |
| 1 | 20 | | Value = new Stack<T>() |
| 1 | 21 | | }; |
| | 22 | |
|
| | 23 | | // ReSharper disable once InconsistentNaming |
| 1 | 24 | | private static readonly AsyncLocal<T?> GlobalCurrent = new (); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Current ambient instance |
| | 28 | | /// </summary> |
| 17 | 29 | | public static T? Current => GlobalCurrent.Value; |
| | 30 | |
|
| 16 | 31 | | private readonly AsyncLocal<T?> _localParentScope = new(); |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Parent scope of current scope |
| | 35 | | /// </summary> |
| 20 | 36 | | public T? ParentScope => _localParentScope.Value; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Parent scopes of current scope |
| | 40 | | /// </summary> |
| | 41 | | public IEnumerable<T> GetParentScopes(Predicate<T>? continueCondition = default) |
| | 42 | | { |
| 2 | 43 | | var current = this; |
| 6 | 44 | | while (current != null) |
| | 45 | | { |
| 6 | 46 | | if (current.ParentScope == null || current._localParentScope.Value == null) |
| | 47 | | break; |
| | 48 | |
|
| 4 | 49 | | if (continueCondition?.Invoke(current._localParentScope.Value) ?? true) |
| | 50 | | { |
| 4 | 51 | | yield return current._localParentScope.Value; |
| 4 | 52 | | current = current._localParentScope.Value; |
| | 53 | | } |
| | 54 | | } |
| 2 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Constructor |
| | 59 | | /// </summary> |
| 16 | 60 | | protected AmbientScope() |
| | 61 | | { |
| 16 | 62 | | if (GlobalCurrent.Value != null) |
| | 63 | | { |
| 6 | 64 | | _localParentScope.Value = GlobalCurrent.Value; |
| | 65 | | } |
| | 66 | |
|
| 16 | 67 | | if (GlobalScopes.Value == null) |
| 9 | 68 | | GlobalScopes.Value = new Stack<T>(); |
| | 69 | |
|
| 16 | 70 | | if (this is T c) |
| 16 | 71 | | GlobalScopes.Value.Push(c); |
| 16 | 72 | | ChangeCurrentScope(this as T); |
| 16 | 73 | | } |
| | 74 | |
|
| | 75 | | private void ChangeCurrentScope(T? scope) |
| | 76 | | { |
| 30 | 77 | | if (GlobalCurrent.Value == null) |
| | 78 | | { |
| 10 | 79 | | if (scope is not null) |
| | 80 | | { |
| 10 | 81 | | GlobalCurrent.Value = scope; |
| | 82 | | } |
| | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 20 | 86 | | GlobalCurrent.Value = scope; |
| | 87 | | } |
| 20 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Dispose method |
| | 92 | | /// </summary> |
| | 93 | | public void Dispose() |
| | 94 | | { |
| 16 | 95 | | Dispose(true); |
| 16 | 96 | | GC.SuppressFinalize(this); |
| 16 | 97 | | } |
| | 98 | |
|
| | 99 | | private bool _disposed; |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Dispose |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="disposing">true if called by dispose, false if it is called by finalizer</param> |
| | 105 | | protected virtual void Dispose(bool disposing) |
| | 106 | | { |
| 16 | 107 | | if(_disposed) |
| 2 | 108 | | return; |
| | 109 | |
|
| 14 | 110 | | if (disposing) |
| | 111 | | { |
| 14 | 112 | | if (GlobalScopes.Value!.TryPop(out var scope)) |
| | 113 | | { |
| 14 | 114 | | ChangeCurrentScope(scope.ParentScope); |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 14 | 118 | | _disposed = true; |
| 14 | 119 | | } |
| | 120 | | } |