/// <summary> /// Adds a function to an event table. This function will be called anytime the associated eventType is triggered. /// </summary> /// <param name="eventType">One of the following consts from io.newgrounds.SessionResult: SESSION_EXPIRED, USER_LOADED, REQUEST_LOGIN</param> /// <param name="handler">A handler function to call whenever this event is triggered.</param> public void addEventListener(string eventType, onCallResult <SessionResult> handler) { lock (eventTable) { if (!eventTable.ContainsKey(eventType)) { eventTable.Add(eventType, null); } eventTable[eventType] = (onCallResult <SessionResult>)eventTable[eventType] + handler; } }
/// <summary> /// Calls all handler functions in the event table attached to the given eventType. /// </summary> /// <param name="eventType">The eventType to trigger</param> /// <param name="data">Any object to pass to the handler function(s).</param> public void dispatchEvent(string eventType, SessionResult data) { Delegate d; if (eventTable.TryGetValue(eventType, out d)) { onCallResult <SessionResult> cb = (onCallResult <SessionResult>)d; if (cb != null) { cb(data); } } }
/// <summary> /// Removes a callback function you have already added to the event table. /// </summary> /// <param name="eventType">One of the following consts from io.newgrounds.SessionResult: SESSION_EXPIRED, USER_LOADED, REQUEST_LOGIN</param> /// <param name="handler">The handler function you want to remove from the event table.</param> /// <returns></returns> public bool removeEventListener(string eventType, onCallResult <SessionResult> handler) { lock (eventTable) { if (!eventTable.ContainsKey(eventType)) { return(false); } eventTable[eventType] = (onCallResult <SessionResult>)eventTable[eventType] - handler; if (eventTable[eventType] == null) { eventTable.Remove(eventType); } } return(true); }