using System; using BlueWest.Collections; namespace BlueWest.Tools { public struct EventManagerAsync { private static readonly FastDictionary> _subscribersList; static EventManagerAsync() { _subscribersList = new FastDictionary>(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 FastList(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 }*/ FastList subscriberList = _subscribersList[eventType]; for (int i = 0; i < subscriberList.Length; i++) { if (subscriberList.Buffer[i] == listener) { subscriberList.Remove(subscriberList[i]); if (subscriberList.Length == 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); FastList list = _subscribersList[type]; for (int i = 0; i < list.Length; i++) { var baseListener = list.Buffer[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) { FastList receivers; if (!_subscribersList.TryGetValue(type, out receivers)) return false; bool exists = false; for (int i = 0; i < receivers.Length; i++) { var eventListenerBase = receivers.Buffer[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); } }