更新框架
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user