mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
This PR refactors numerous lambda expressions by removing unnecessary braces and streamlining single-statement lambdas throughout the codebase, improving readability and maintainability. - Consider simplifying lambda when its body has a single statement: DeepSource flagged lambdas written with a block body for only one statement, causing extra verbosity. This update converts those to concise expression-bodied lambdas by stripping out the curly braces, applied across test assertions, event registrations, and unregister logic. The code is now cleaner and more consistent. > This Autofix was generated by AI. Please review the change before merging.
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);
|
||
}
|
||
} |