mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 在EventBus中添加RegisterWithContext方法支持事件上下文 - 实现EventContext<T>类包装事件数据并提供处理控制 - 在PriorityEvent中添加上下文事件处理器和相关管理逻辑 - 改进事件触发机制支持普通和上下文处理器混合使用 - 优化MicrosoftDiContainer的线程安全性和并发访问 - 修复SpanExtensions中TryParseValue的返回值类型问题 - 添加全面的单元测试覆盖新功能和边界情况
104 lines
3.7 KiB
C#
104 lines
3.7 KiB
C#
using GFramework.Core.Abstractions.events;
|
|
|
|
namespace GFramework.Core.events;
|
|
|
|
/// <summary>
|
|
/// 类型事件系统,提供基于类型的事件发送、注册和注销功能
|
|
/// </summary>
|
|
public class EventBus : IEventBus
|
|
{
|
|
private readonly EasyEvents _mEvents = new();
|
|
private readonly EasyEvents _mPriorityEvents = new();
|
|
|
|
/// <summary>
|
|
/// 发送事件,自动创建事件实例
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型,必须具有无参构造函数</typeparam>
|
|
public void Send<T>() where T : new()
|
|
{
|
|
_mEvents
|
|
.GetOrAddEvent<Event<T>>()
|
|
.Trigger(new T());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送指定的事件实例
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="e">事件实例</param>
|
|
public void Send<T>(T e)
|
|
{
|
|
_mEvents
|
|
.GetOrAddEvent<Event<T>>()
|
|
.Trigger(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发送指定的事件实例,并指定传播模式
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="e">事件实例</param>
|
|
/// <param name="propagation">事件传播模式</param>
|
|
public void Send<T>(T e, EventPropagation propagation)
|
|
{
|
|
_mPriorityEvents
|
|
.GetOrAddEvent<PriorityEvent<T>>()
|
|
.Trigger(e, propagation);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册事件监听器
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="onEvent">事件处理回调函数</param>
|
|
/// <returns>反注册接口,用于注销事件监听</returns>
|
|
public IUnRegister Register<T>(Action<T> onEvent)
|
|
{
|
|
return _mEvents.GetOrAddEvent<Event<T>>().Register(onEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册事件监听器,并指定优先级
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="onEvent">事件处理回调函数</param>
|
|
/// <param name="priority">优先级,数值越大优先级越高</param>
|
|
/// <returns>反注册接口,用于注销事件监听</returns>
|
|
public IUnRegister Register<T>(Action<T> onEvent, int priority)
|
|
{
|
|
return _mPriorityEvents.GetOrAddEvent<PriorityEvent<T>>().Register(onEvent, priority);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注销事件监听器
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="onEvent">要注销的事件处理回调函数</param>
|
|
public void UnRegister<T>(Action<T> onEvent)
|
|
{
|
|
_mEvents.GetEvent<Event<T>>().UnRegister(onEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册上下文事件监听器,默认优先级为 0
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="onEvent">事件处理回调函数,接收 EventContext 参数</param>
|
|
/// <returns>反注册接口,用于注销事件监听</returns>
|
|
public IUnRegister RegisterWithContext<T>(Action<EventContext<T>> onEvent)
|
|
{
|
|
return _mPriorityEvents.GetOrAddEvent<PriorityEvent<T>>().RegisterWithContext(onEvent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册上下文事件监听器,并指定优先级
|
|
/// </summary>
|
|
/// <typeparam name="T">事件类型</typeparam>
|
|
/// <param name="onEvent">事件处理回调函数,接收 EventContext 参数</param>
|
|
/// <param name="priority">优先级,数值越大优先级越高</param>
|
|
/// <returns>反注册接口,用于注销事件监听</returns>
|
|
public IUnRegister RegisterWithContext<T>(Action<EventContext<T>> onEvent, int priority)
|
|
{
|
|
return _mPriorityEvents.GetOrAddEvent<PriorityEvent<T>>().RegisterWithContext(onEvent, priority);
|
|
}
|
|
} |