Files
sharp-basic/src/Sharp/EventBus/EventSubscription.cs
T
2025-12-30 09:17:25 +08:00

37 lines
971 B
C#

using System;
using System.Threading.Tasks;
namespace Sharp.EventBus;
internal sealed class EventSubscription : IDisposable
{
public Type EventType { get; }
public int Priority { get; }
public Func<object, Task> Handler { get; }
public Func<object, bool>? Filter { get; }
private readonly Action<EventSubscription> _onDispose;
private bool _disposed;
public EventSubscription(Type eventType, Func<object, Task> handler, Func<object, bool>? filter, int priority,
Action<EventSubscription> onDispose)
{
EventType = eventType;
Handler = handler;
Filter = filter;
Priority = priority;
_onDispose = onDispose;
}
public bool CanHandle(object evt)
{
if (evt.GetType() != EventType) return false;
return Filter == null || Filter(evt);
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
_onDispose(this);
}
}