static public void AddListener(EEventID eventType, Callback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Create an entry for this event type if it doesn't already exist. if (!eventTable.ContainsKey(eventType)) { eventTable.Add(eventType, null); } // Add the handler to the event. eventTable[eventType] = (Callback)eventTable[eventType] + handler; } }
static public void TriggerEvent(EEventID eventType, System.Object arg) { Delegate d; // Invoke the delegate only if the event type is in the dictionary. if (eventTable.TryGetValue(eventType, out d)) { // Take a local copy to prevent a race condition if another thread // were to unsubscribe from this event. Callback callback = (Callback)d; // Invoke the delegate if it's not null. if (callback != null) { callback(arg); } } }
static public void RemoveListener(EEventID eventType, Callback handler) { // Obtain a lock on the event table to keep this thread-safe. lock (eventTable) { // Only take action if this event type exists. if (eventTable.ContainsKey(eventType)) { // Remove the event handler from this event. eventTable[eventType] = (Callback)eventTable[eventType] - handler; // If there's nothing left then remove the event type from the event table. if (eventTable[eventType] == null) { eventTable.Remove(eventType); } } } }