67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using System;
|
|
using System.Buffers;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.Runtime.CompilerServices;
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
namespace AlicizaX.EventKit.Runtime
|
|
{
|
|
public class EventPublisher : IDisposable
|
|
{
|
|
private readonly Dictionary<Type, List<IEventProcessor>> _asyncEvents = new Dictionary<Type, List<IEventProcessor>>();
|
|
|
|
public EventPublisher()
|
|
{
|
|
// EventKit.Publisher.Subscribe<ISystemAssemblyLoadEvent>(OnLoad);
|
|
}
|
|
//
|
|
// internal void OnLoad(SystemAssemblyLoadEventData assemblies)
|
|
// {
|
|
// HashSet<Type> types = SystemTypeCollector.Instance.GetTypes(typeof(EventProcessorAttribute));
|
|
//
|
|
// foreach (var type in types)
|
|
// {
|
|
// var @event = (IEventProcessor)Activator.CreateInstance(type);
|
|
//
|
|
// if (@event == null)
|
|
// {
|
|
// continue;
|
|
// }
|
|
//
|
|
// var eventType = @event.EventType();
|
|
// if (!_asyncEvents.ContainsKey(eventType)) _asyncEvents.Add(eventType, new List<IEventProcessor>());
|
|
// _asyncEvents[@eventType].Add(@event);
|
|
// }
|
|
// }
|
|
//
|
|
// /// <summary>
|
|
// /// 异步发布一个值类型的事件数据。
|
|
// /// </summary>
|
|
// /// <typeparam name="TEventData">事件数据类型(值类型)。</typeparam>
|
|
// /// <param name="eventData">事件数据实例。</param>
|
|
// /// <returns>表示异步操作的任务。</returns>
|
|
// public async UniTaskVoid PublishAsync<TEventData>(TEventData eventData) where TEventData : struct
|
|
// {
|
|
// if (!_asyncEvents.TryGetValue(typeof(TEventData), out var list))
|
|
// {
|
|
// return;
|
|
// }
|
|
//
|
|
// var tasks = ListPool<UniTask>.Get();
|
|
// foreach (var @event in list)
|
|
// {
|
|
// tasks.Add(@event.Invoke(eventData));
|
|
// }
|
|
//
|
|
// await UniTask.WhenAll(tasks);
|
|
// ListPool<UniTask>.Release(tasks);
|
|
// }
|
|
|
|
void IDisposable.Dispose()
|
|
{
|
|
_asyncEvents.Clear();
|
|
}
|
|
}
|
|
}
|