using System; using System.Collections.Generic; namespace BlueWest.Tools { public struct EventManagerAsync { private static readonly Dictionary> _subscribersList; static EventManagerAsync() { _subscribersList = new Dictionary>(12412); } /// /// Adds a new subscriber to a certain event. /// /// listener. /// The event type. public static void AddListenerAsync(EventListenerAsync listener) where TEventAsync : struct { var eventType = typeof(TEventAsync); if (!_subscribersList.ContainsKey(eventType)) _subscribersList[eventType] = new List(10000); //if( !SubscriptionExistsAsync( eventType, listener ) ) _subscribersList[eventType].Add(listener); } /// /// Removes a subscriber from a certain event. /// /// listener. /// The event type. public static void RemoveListenerAsync(EventListenerAsync listener) where TEventAsync : struct { var eventType = typeof(TEventAsync); /* if( !_subscribersList.ContainsKey( eventType ) ) { #if EVENTROUTER_THROWEXCEPTIONS throw new ArgumentException( string.Format( "Removing listener \"{0}\", but the event type \"{1}\" isn't registered.", listener, eventType.ToString() ) ); #else return; #endif }*/ List subscriberList = _subscribersList[eventType]; for (int i = 0; i < subscriberList.Count; i++) { if (subscriberList[i] == listener) { subscriberList.Remove(subscriberList[i]); if (subscriberList.Count == 0) _subscribersList.Remove(eventType); return; } } } /// /// Triggers an event. All instances that are subscribed to it will receive it (and will potentially act on it). /// /// The event to trigger. /// The 1st type parameter. public static void TriggerEventAsync(TEvent newEvent) where TEvent : struct { var type = typeof(TEvent); List list = _subscribersList[type]; for (int i = 0; i < list.Count; i++) { var baseListener = list[i]; var eventListenerCasted = baseListener as EventListenerAsync; eventListenerCasted.OnEventAsync(newEvent); } } /// /// Checks if there are subscribers for a certain type of events /// /// true, if exists was subscriptioned, false otherwise. /// Type. /// Receiver. private static bool SubscriptionExistsAsync(Type type, EventListenerBaseAsync receiver) { List receivers; if (!_subscribersList.TryGetValue(type, out receivers)) return false; bool exists = false; for (int i = 0; i < receivers.Count; i++) { var eventListenerBase = receivers[i]; if (eventListenerBase == receiver) { return true; } } return exists; } } /// /// Static class that allows any class to start or stop listening to events /// public static class EventRegisterAsync { public delegate void DelegateAsync(T eventType); public static void EventStartListeningAsync(this EventListenerAsync caller) where EventType : struct { EventManagerAsync.AddListenerAsync(caller); } public static void EventStopListeningAsync(this EventListenerAsync caller) where EventType : struct { EventManagerAsync.RemoveListenerAsync(caller); } } /// /// TEvent listener basic interface /// public interface EventListenerBaseAsync { }; /// /// A public interface you'll need to implement for each type of event you want to listen to. /// public interface EventListenerAsync : EventListenerBaseAsync { void OnEventAsync(T eventType); } }