更新框架
This commit is contained in:
1 parent
ef50fa0620
commit
84b28f9672
72 files changed
+2222
-282
No files matched your search
@@ -0,0 +1,32 @@
|
||||
namespace Store.Management.Framework.Utilities;
|
||||
|
||||
/// <summary>按订阅顺序等待异步处理器;异常向调用者传播并停止后续处理。</summary>
|
||||
public sealed class AsyncEvent<TEventArgs> where TEventArgs : EventArgs
|
||||
{
|
||||
private readonly object _gate = new();
|
||||
private readonly List<Func<object?, TEventArgs, Task>> _handlers = [];
|
||||
|
||||
public void Subscribe(Func<object?, TEventArgs, Task> callback)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(callback);
|
||||
lock (_gate) { _handlers.Add(callback); }
|
||||
}
|
||||
|
||||
public void Unsubscribe(Func<object?, TEventArgs, Task> callback)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(callback);
|
||||
lock (_gate)
|
||||
{
|
||||
int index = _handlers.LastIndexOf(callback);
|
||||
if (index >= 0) _handlers.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InvokeAsync(object? sender, TEventArgs eventArgs)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(eventArgs);
|
||||
Func<object?, TEventArgs, Task>[] snapshot;
|
||||
lock (_gate) { snapshot = _handlers.ToArray(); }
|
||||
foreach (var callback in snapshot) await callback(sender, eventArgs);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace Store.Management.Framework.Utilities;
|
||||
|
||||
/// <summary>在释放时执行一次清理操作,可安全地重复或并发释放。</summary>
|
||||
public sealed class DisposeAction : IDisposable
|
||||
{
|
||||
private Action? _action;
|
||||
|
||||
public DisposeAction(Action action)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(action);
|
||||
_action = action;
|
||||
}
|
||||
|
||||
public void Dispose() => Interlocked.Exchange(ref _action, null)?.Invoke();
|
||||
}
|
||||
Reference in new issue
Block a user