- 新增 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。
130 lines
6.1 KiB
C#
130 lines
6.1 KiB
C#
#nullable enable
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
namespace ShrinkEventBus
|
|
{
|
|
public static class EventBus
|
|
{
|
|
private static readonly IShrinkEventBus DefaultBus = CreateDefaultBus();
|
|
|
|
public static IShrinkEventBus Default => DefaultBus;
|
|
|
|
public static event Action<EventBase, Type> OnEventTriggered
|
|
{
|
|
add => DefaultBus.OnEventTriggered += value;
|
|
remove => DefaultBus.OnEventTriggered -= value;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public static bool EnableDebugRecord
|
|
{
|
|
get => DefaultBus.EnableDebugRecord;
|
|
set => DefaultBus.EnableDebugRecord = value;
|
|
}
|
|
|
|
public static event Action<EventBase, string, string, EventHandlerInfo[]> OnEventTriggeredForEditor
|
|
{
|
|
add => DefaultBus.OnEventTriggeredForEditor += value;
|
|
remove => DefaultBus.OnEventTriggeredForEditor -= value;
|
|
}
|
|
#endif
|
|
|
|
public static ShrinkEventBusBuilder Builder() => new();
|
|
|
|
public static IShrinkEventBus CreateBus(Action<ShrinkEventBusBuilder>? configure = null)
|
|
{
|
|
var builder = new ShrinkEventBusBuilder();
|
|
configure?.Invoke(builder);
|
|
return builder.Build();
|
|
}
|
|
|
|
public static void Start() => DefaultBus.Start();
|
|
public static void AutoRegister(object target) => DefaultBus.AutoRegister(target);
|
|
public static void Register(object target) => DefaultBus.Register(target);
|
|
public static void Unregister(object target) => DefaultBus.Unregister(target);
|
|
|
|
public static void RegisterEvent<TEvent>(Func<TEvent, UniTask> handler, int priority)
|
|
where TEvent : EventBase => DefaultBus.RegisterEvent(handler, priority);
|
|
|
|
public static void RegisterEvent<TEvent>(Action<TEvent> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false)
|
|
where TEvent : EventBase => DefaultBus.RegisterEvent(handler, priority, receiveCanceled);
|
|
|
|
public static void RegisterEvent<TEvent>(Action<TEvent> handler, int priority)
|
|
where TEvent : EventBase => DefaultBus.RegisterEvent(handler, priority);
|
|
|
|
public static void RegisterEvent<TEvent>(Func<TEvent, UniTask> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false)
|
|
where TEvent : EventBase => DefaultBus.RegisterEvent(handler, priority, receiveCanceled);
|
|
|
|
public static IShrinkEventSubscription SubscribeEvent<TEvent>(Action<TEvent> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false)
|
|
where TEvent : EventBase => DefaultBus.SubscribeEvent(handler, priority, receiveCanceled);
|
|
|
|
public static IShrinkEventSubscription SubscribeEvent<TEvent>(Action<TEvent> handler, int priority)
|
|
where TEvent : EventBase => DefaultBus.SubscribeEvent(handler, priority);
|
|
|
|
public static IShrinkEventSubscription SubscribeEvent<TEvent>(Func<TEvent, UniTask> handler,
|
|
EventPriority priority = EventPriority.NORMAL, bool receiveCanceled = false)
|
|
where TEvent : EventBase => DefaultBus.SubscribeEvent(handler, priority, receiveCanceled);
|
|
|
|
public static IShrinkEventSubscription SubscribeEvent<TEvent>(Func<TEvent, UniTask> handler, int priority)
|
|
where TEvent : EventBase => DefaultBus.SubscribeEvent(handler, priority);
|
|
|
|
public static void UnregisterEvent<TEvent>(Func<TEvent, UniTask> handler) where TEvent : EventBase =>
|
|
DefaultBus.UnregisterEvent(handler);
|
|
|
|
public static void UnregisterEvent<TEvent>(Action<TEvent> handler) where TEvent : EventBase =>
|
|
DefaultBus.UnregisterEvent(handler);
|
|
|
|
public static void ClearAllSubscribersForEvent<TEvent>() where TEvent : EventBase =>
|
|
DefaultBus.ClearAllSubscribersForEvent<TEvent>();
|
|
|
|
public static void UnregisterAllEventsForObject(object targetObject) =>
|
|
DefaultBus.UnregisterAllEventsForObject(targetObject);
|
|
|
|
public static void UnregisterInstance(object targetObject) => DefaultBus.Unregister(targetObject);
|
|
public static void UnregisterAllEvents() => DefaultBus.UnregisterAllEvents();
|
|
|
|
public static UniTask<bool> TriggerEventAsync<TEvent>(TEvent eventArgs) where TEvent : EventBase =>
|
|
DefaultBus.TriggerEventAsync(eventArgs);
|
|
|
|
public static UniTask<bool> TriggerEventAsync<TEvent>(EventPriority phase, TEvent eventArgs)
|
|
where TEvent : EventBase => DefaultBus.TriggerEventAsync(phase, eventArgs);
|
|
|
|
public static bool TriggerEvent<TEvent>(TEvent eventArgs) where TEvent : EventBase =>
|
|
DefaultBus.TriggerEvent(eventArgs);
|
|
|
|
public static bool TriggerEvent<TEvent>(EventPriority phase, TEvent eventArgs) where TEvent : EventBase =>
|
|
DefaultBus.TriggerEvent(phase, eventArgs);
|
|
|
|
public static EventHandlerInfo[] GetEventSubscribers<TEvent>() where TEvent : EventBase =>
|
|
DefaultBus.GetEventSubscribers<TEvent>();
|
|
|
|
public static ListenerList GetListenerList<TEvent>() where TEvent : EventBase =>
|
|
DefaultBus.GetListenerList<TEvent>();
|
|
|
|
public static IReadOnlyDictionary<Type, EventHandlerInfo[]> GetAllSubscribersSnapshot() =>
|
|
DefaultBus.GetAllSubscribersSnapshot();
|
|
|
|
public static IReadOnlyList<ShrinkEventSubscriptionSnapshot> GetActiveSubscriptionsSnapshot() =>
|
|
DefaultBus.GetActiveSubscriptionsSnapshot();
|
|
|
|
public static bool IsInstanceRegistered(object target) => DefaultBus.IsInstanceRegistered(target);
|
|
public static int GetRegisteredInstanceCount() => DefaultBus.GetRegisteredInstanceCount();
|
|
public static int GetRegisteredEventTypeCount() => DefaultBus.GetRegisteredEventTypeCount();
|
|
|
|
private static IShrinkEventBus CreateDefaultBus()
|
|
{
|
|
var bus = (ShrinkEventBusInstance)new ShrinkEventBusBuilder()
|
|
.SetExceptionHandlingMode(ShrinkEventExceptionHandlingMode.LogAndContinue)
|
|
.AllowPerPhaseDispatch()
|
|
.Build();
|
|
EventBusRegHelper.RegStaticEventHandler(bus);
|
|
return bus;
|
|
}
|
|
}
|
|
}
|