- 新增 IShrinkEventBus、ShrinkEventBusBuilder 与 ShrinkEventBusInstance,支持独立 bus、异常策略、启动开关、类型校验和按 phase 派发 - 新增 SubscribeEvent 可释放订阅句柄与订阅快照接口,保留 EventBus 静态门面并委托到默认实例 - 重构监听列表为父子事件链共享锁与快照缓存,支持父事件监听子事件、跨父子数字优先级归并和防御性快照拷贝 - 优化 EventBase 元数据缓存、懒生成 EventId、派发快照挂接与对象池双重归还保护,移除旧 EventCache 热路径 - 恢复本地 ILPostProcessor 兜底并生成静态订阅注册表,避免共享 CodeGen 覆盖外程序集自动注册失效 - 改进事件查看器、Benchmark、README 与 CHANGELOG,并新增 Dispatch/Priority/Registration/Subscription 测试覆盖核心行为 BREAKING CHANGE: 移除 EventBase.GetListenerList(),int 数字优先级重载不再提供默认值,数字优先级 0 现在映射为 NORMAL。
77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
#nullable enable
|
|
using System;
|
|
|
|
namespace ShrinkEventBus
|
|
{
|
|
public sealed class ShrinkEventBusBuilder
|
|
{
|
|
internal IShrinkEventExceptionHandler? ExceptionHandler { get; private set; }
|
|
internal ShrinkEventExceptionHandlingMode ExceptionHandlingMode { get; private set; } =
|
|
ShrinkEventExceptionHandlingMode.LogAndContinue;
|
|
internal Action<Type>? EventClassChecker { get; private set; }
|
|
internal bool StartShutdownEnabled { get; private set; }
|
|
internal bool CheckTypesOnDispatchEnabled { get; private set; }
|
|
internal bool AllowPerPhaseDispatchEnabled { get; private set; }
|
|
|
|
public ShrinkEventBusBuilder SetExceptionHandler(IShrinkEventExceptionHandler handler)
|
|
{
|
|
ExceptionHandler = handler ?? throw new ArgumentNullException(nameof(handler));
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder SetExceptionHandlingMode(ShrinkEventExceptionHandlingMode mode)
|
|
{
|
|
ExceptionHandlingMode = mode;
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder StartShutdown()
|
|
{
|
|
StartShutdownEnabled = true;
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder CheckTypesOnDispatch()
|
|
{
|
|
CheckTypesOnDispatchEnabled = true;
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder AllowPerPhaseDispatch()
|
|
{
|
|
AllowPerPhaseDispatchEnabled = true;
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder ClassChecker(Action<Type> checker)
|
|
{
|
|
if (checker == null)
|
|
throw new ArgumentNullException(nameof(checker));
|
|
|
|
EventClassChecker = EventClassChecker == null
|
|
? checker
|
|
: EventClassChecker + checker;
|
|
return this;
|
|
}
|
|
|
|
public ShrinkEventBusBuilder MarkerInterface<TMarker>() where TMarker : class
|
|
{
|
|
var markerType = typeof(TMarker);
|
|
if (!markerType.IsInterface)
|
|
throw new InvalidOperationException($"Marker type {markerType.FullName} must be an interface.");
|
|
|
|
return ClassChecker(eventType =>
|
|
{
|
|
if (!markerType.IsAssignableFrom(eventType))
|
|
throw new ArgumentException(
|
|
$"This bus only accepts events assignable to {markerType.FullName}, but got {eventType.FullName}.");
|
|
});
|
|
}
|
|
|
|
public IShrinkEventBus Build()
|
|
{
|
|
return new ShrinkEventBusInstance(this);
|
|
}
|
|
}
|
|
}
|