Commit 648f5737 authored by Bronuh's avatar Bronuh
Browse files

Refactor EventBus from static to instance class in KludgeBox.Events module

parent dffb9d97
No related merge requests found
Showing with 10 additions and 10 deletions
+10 -10
......@@ -7,15 +7,15 @@ namespace KludgeBox.Events;
/// <summary>
/// Provides a central event bus for publishing and subscribing to events.
/// </summary>
public static class EventBus
public class EventBus
{
/// <summary>
/// If set to true, EventBus will attempt to publish events to all EventHubs whose types are derived from the event type.
/// This option can significantly impact performance.
/// </summary>
public static bool IncludeBaseEvents = true;
public bool IncludeBaseEvents = true;
private static Dictionary<Type, EventHub> _hubs = new Dictionary<Type, EventHub>();
private Dictionary<Type, EventHub> _hubs = new Dictionary<Type, EventHub>();
/// <summary>
/// Subscribes a listener to the specified event type.
......@@ -23,7 +23,7 @@ public static class EventBus
/// <typeparam name="T">The event type to subscribe to.</typeparam>
/// <param name="action">The action to execute when the event is published.</param>
/// <returns>A listener token that can be used to unsubscribe from the event.</returns>
public static ListenerToken Subscribe<T>(Action<T> action) where T : IEvent
public ListenerToken Subscribe<T>(Action<T> action) where T : IEvent
{
return GetHub(typeof(T)).Subscribe(action);
}
......@@ -33,7 +33,7 @@ public static class EventBus
/// </summary>
/// <typeparam name="T">The type of event to publish.</typeparam>
/// <param name="event">The event to publish.</param>
public static void Publish<T>(T @event) where T : IEvent
public void Publish<T>(T @event) where T : IEvent
{
if (IncludeBaseEvents)
{
......@@ -51,7 +51,7 @@ public static class EventBus
/// <summary>
/// Resets all the EventHubs.
/// </summary>
public static void Reset()
public void Reset()
{
_hubs.Clear();
}
......@@ -60,12 +60,12 @@ public static class EventBus
/// Resets the EventHub associated with the specified event type.
/// </summary>
/// <typeparam name="T">The event type for which to reset the EventHub.</typeparam>
public static void ResetEvent<T>() where T : IEvent
public void ResetEvent<T>() where T : IEvent
{
GetHub(typeof(T)).Reset();
}
private static EventHub GetHub(Type eventType)
private EventHub GetHub(Type eventType)
{
if (_hubs.TryGetValue(eventType, out EventHub? hub) && hub is not null)
{
......@@ -83,7 +83,7 @@ public static class EventBus
/// </summary>
/// <param name="methodInfo">The MethodInfo representing the delivery action.</param>
/// <returns>Message subscription token that can be used for unsubscribing.</returns>
public static ListenerToken SubscribeMethod(MethodInfo methodInfo)
public ListenerToken SubscribeMethod(MethodInfo methodInfo)
{
Type messageType = methodInfo.GetParameters()[0].ParameterType;
......@@ -96,7 +96,7 @@ public static class EventBus
.Invoke(null, new object[] { actionDelegate }) as ListenerToken;
}
private static List<EventHub> FindApplicableHubs(Type eventType)
private List<EventHub> FindApplicableHubs(Type eventType)
{
List<EventHub> applicableHubs = new List<EventHub>();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment