mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 在EasyEventGeneric中添加GetListenerCount方法获取监听器数量 - 重新排列EnhancedEventBus中字段顺序以优化内存布局 - 为所有Send方法添加发布统计记录功能 - 实现事件处理器包装以添加统计功能,包括成功处理和失败记录 - 添加监听器数量统计更新机制,自动跟踪注册和注销事件 - 重构FilterableEvent中的过滤逻辑以减少锁持有时间 - 在PriorityEvent中添加GetListenerCount方法获取总监听器数量 - 移除EventStatistics中的过时字段并优化ActiveEventTypes和ActiveListeners计算 - 清理StatisticsEventDecorator中的并发问题和实现装饰器模式
141 lines
4.4 KiB
C#
141 lines
4.4 KiB
C#
using GFramework.Core.Abstractions.events;
|
||
|
||
namespace GFramework.Core.events;
|
||
|
||
/// <summary>
|
||
/// 泛型事件类,支持一个泛型参数 T 的事件注册、注销与触发。
|
||
/// 实现了 IEvent 接口以提供统一的事件操作接口。
|
||
/// </summary>
|
||
/// <typeparam name="T">事件回调函数的第一个参数类型。</typeparam>
|
||
public class Event<T> : IEvent
|
||
{
|
||
/// <summary>
|
||
/// 存储已注册的事件处理委托。
|
||
/// 默认为空操作(no-op)委托,避免 null 检查。
|
||
/// </summary>
|
||
private Action<T>? _mOnEvent = _ => { };
|
||
|
||
/// <summary>
|
||
/// 显式实现 IEvent 接口中的 Register 方法。
|
||
/// 允许使用无参 Action 来订阅当前带参事件。
|
||
/// </summary>
|
||
/// <param name="onEvent">无参事件处理方法。</param>
|
||
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
|
||
IUnRegister IEvent.Register(Action onEvent)
|
||
{
|
||
return Register(Action);
|
||
|
||
void Action(T _)
|
||
{
|
||
onEvent();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册一个事件监听器,并返回可用于取消注册的对象。
|
||
/// </summary>
|
||
/// <param name="onEvent">要注册的事件处理方法。</param>
|
||
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
|
||
public IUnRegister Register(Action<T> onEvent)
|
||
{
|
||
_mOnEvent += onEvent;
|
||
return new DefaultUnRegister(() => UnRegister(onEvent));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消指定的事件监听器。
|
||
/// </summary>
|
||
/// <param name="onEvent">需要被注销的事件处理方法。</param>
|
||
public void UnRegister(Action<T> onEvent)
|
||
{
|
||
_mOnEvent -= onEvent;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 触发所有已注册的事件处理程序,并传递参数 t。
|
||
/// </summary>
|
||
/// <param name="t">传递给事件处理程序的参数。</param>
|
||
public void Trigger(T t)
|
||
{
|
||
_mOnEvent?.Invoke(t);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前已注册的监听器数量
|
||
/// </summary>
|
||
/// <returns>监听器数量</returns>
|
||
public int GetListenerCount()
|
||
{
|
||
return _mOnEvent?.GetInvocationList().Length ?? 0;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 支持两个泛型参数 T 和 TK 的事件类。
|
||
/// 提供事件注册、注销和触发功能。
|
||
/// </summary>
|
||
/// <typeparam name="T">第一个参数类型。</typeparam>
|
||
/// <typeparam name="Tk">第二个参数类型。</typeparam>
|
||
public class Event<T, Tk> : IEvent
|
||
{
|
||
/// <summary>
|
||
/// 存储已注册的双参数事件处理委托。
|
||
/// 默认为空操作(no-op)委托。
|
||
/// </summary>
|
||
private Action<T, Tk>? _mOnEvent = (_, _) => { };
|
||
|
||
/// <summary>
|
||
/// 显式实现 IEvent 接口中的 Register 方法。
|
||
/// 允许使用无参 Action 来订阅当前带参事件。
|
||
/// </summary>
|
||
/// <param name="onEvent">无参事件处理方法。</param>
|
||
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
|
||
IUnRegister IEvent.Register(Action onEvent)
|
||
{
|
||
return Register(Action);
|
||
|
||
void Action(T _, Tk __)
|
||
{
|
||
onEvent();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册一个接受两个参数的事件监听器,并返回可用于取消注册的对象。
|
||
/// </summary>
|
||
/// <param name="onEvent">要注册的事件处理方法。</param>
|
||
/// <returns>IUnRegister 对象,用于稍后注销该事件监听器。</returns>
|
||
public IUnRegister Register(Action<T, Tk> onEvent)
|
||
{
|
||
_mOnEvent += onEvent;
|
||
return new DefaultUnRegister(() => UnRegister(onEvent));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取消指定的双参数事件监听器。
|
||
/// </summary>
|
||
/// <param name="onEvent">需要被注销的事件处理方法。</param>
|
||
public void UnRegister(Action<T, Tk> onEvent)
|
||
{
|
||
_mOnEvent -= onEvent;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 触发所有已注册的事件处理程序,并传递参数 t 和 k。
|
||
/// </summary>
|
||
/// <param name="t">第一个参数。</param>
|
||
/// <param name="k">第二个参数。</param>
|
||
public void Trigger(T t, Tk k)
|
||
{
|
||
_mOnEvent?.Invoke(t, k);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前已注册的监听器数量
|
||
/// </summary>
|
||
/// <returns>监听器数量</returns>
|
||
public int GetListenerCount()
|
||
{
|
||
return _mOnEvent?.GetInvocationList().Length ?? 0;
|
||
}
|
||
} |