mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 移除多余using语句和空行,统一代码缩进格式 - 优化注释文档中的缩进和对齐格式 - 简化条件语句和方法实现,提升代码可读性 - 重构协程系统相关类的字段和方法定义格式 - 更新架构服务中容器访问方式的实现 - 调整异步操作类的属性和方法组织结构 - [skip ci]
123 lines
4.0 KiB
C#
123 lines
4.0 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>
|
||
/// 支持两个泛型参数 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);
|
||
}
|
||
} |