- 新增 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。
110 lines
4.7 KiB
C#
110 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
namespace ShrinkEventBus
|
|
{
|
|
public interface IShrinkEventSubscription : IDisposable
|
|
{
|
|
long SubscriptionId { get; }
|
|
bool IsDisposed { get; }
|
|
}
|
|
|
|
public readonly struct ShrinkEventSubscriptionSnapshot
|
|
{
|
|
public ShrinkEventSubscriptionSnapshot(long subscriptionId, Type eventType, EventPriority priority,
|
|
int numericPriority, bool receiveCanceled, object target, string targetTypeName, string methodName,
|
|
string debugInfo, DateTime registeredAtUtc)
|
|
{
|
|
SubscriptionId = subscriptionId;
|
|
EventType = eventType ?? throw new ArgumentNullException(nameof(eventType));
|
|
Priority = priority;
|
|
NumericPriority = numericPriority;
|
|
ReceiveCanceled = receiveCanceled;
|
|
Target = target;
|
|
TargetTypeName = targetTypeName ?? string.Empty;
|
|
MethodName = methodName ?? string.Empty;
|
|
DebugInfo = debugInfo ?? string.Empty;
|
|
RegisteredAtUtc = registeredAtUtc;
|
|
}
|
|
|
|
public long SubscriptionId { get; }
|
|
public Type EventType { get; }
|
|
public EventPriority Priority { get; }
|
|
public int NumericPriority { get; }
|
|
public bool ReceiveCanceled { get; }
|
|
public object Target { get; }
|
|
public string TargetTypeName { get; }
|
|
public string MethodName { get; }
|
|
public string DebugInfo { get; }
|
|
public DateTime RegisteredAtUtc { get; }
|
|
public bool IsStaticHandler => Target == null;
|
|
}
|
|
|
|
public interface IShrinkEventBus
|
|
{
|
|
event Action<EventBase, Type> OnEventTriggered;
|
|
|
|
#if UNITY_EDITOR
|
|
event Action<EventBase, string, string, EventHandlerInfo[]> OnEventTriggeredForEditor;
|
|
bool EnableDebugRecord { get; set; }
|
|
#endif
|
|
|
|
bool IsStarted { get; }
|
|
|
|
void Start();
|
|
void AutoRegister(object target);
|
|
void Register(object target);
|
|
void Unregister(object target);
|
|
|
|
void RegisterEvent<TEvent>(Action<TEvent> handler, EventPriority priority = EventPriority.NORMAL,
|
|
bool receiveCanceled = false) where TEvent : EventBase;
|
|
void RegisterEvent<TEvent>(Action<TEvent> handler, int priority) where TEvent : EventBase;
|
|
void RegisterEvent<TEvent>(Func<TEvent, UniTask> handler, EventPriority priority = EventPriority.NORMAL,
|
|
bool receiveCanceled = false) where TEvent : EventBase;
|
|
void RegisterEvent<TEvent>(Func<TEvent, UniTask> handler, int priority) where TEvent : EventBase;
|
|
IShrinkEventSubscription SubscribeEvent<TEvent>(Action<TEvent> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false) where TEvent : EventBase;
|
|
IShrinkEventSubscription SubscribeEvent<TEvent>(Action<TEvent> handler, int priority)
|
|
where TEvent : EventBase;
|
|
IShrinkEventSubscription SubscribeEvent<TEvent>(Func<TEvent, UniTask> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false) where TEvent : EventBase;
|
|
IShrinkEventSubscription SubscribeEvent<TEvent>(Func<TEvent, UniTask> handler, int priority)
|
|
where TEvent : EventBase;
|
|
|
|
void UnregisterEvent<TEvent>(Action<TEvent> handler) where TEvent : EventBase;
|
|
void UnregisterEvent<TEvent>(Func<TEvent, UniTask> handler) where TEvent : EventBase;
|
|
void ClearAllSubscribersForEvent<TEvent>() where TEvent : EventBase;
|
|
void UnregisterAllEventsForObject(object targetObject);
|
|
void UnregisterAllEvents();
|
|
|
|
bool TriggerEvent<TEvent>(TEvent eventArgs) where TEvent : EventBase;
|
|
bool TriggerEvent<TEvent>(EventPriority phase, TEvent eventArgs) where TEvent : EventBase;
|
|
UniTask<bool> TriggerEventAsync<TEvent>(TEvent eventArgs) where TEvent : EventBase;
|
|
UniTask<bool> TriggerEventAsync<TEvent>(EventPriority phase, TEvent eventArgs) where TEvent : EventBase;
|
|
|
|
EventHandlerInfo[] GetEventSubscribers<TEvent>() where TEvent : EventBase;
|
|
ListenerList GetListenerList<TEvent>() where TEvent : EventBase;
|
|
IReadOnlyDictionary<Type, EventHandlerInfo[]> GetAllSubscribersSnapshot();
|
|
IReadOnlyList<ShrinkEventSubscriptionSnapshot> GetActiveSubscriptionsSnapshot();
|
|
|
|
bool IsInstanceRegistered(object target);
|
|
int GetRegisteredInstanceCount();
|
|
int GetRegisteredEventTypeCount();
|
|
}
|
|
|
|
public interface IShrinkEventExceptionHandler
|
|
{
|
|
void HandleException(IShrinkEventBus bus, EventBase eventArgs, EventHandlerInfo[] listeners, int index,
|
|
Exception exception);
|
|
}
|
|
|
|
public enum ShrinkEventExceptionHandlingMode
|
|
{
|
|
LogAndContinue,
|
|
Throw,
|
|
LogAndThrow
|
|
}
|
|
}
|