private void InternalRemove(Enum eventId, BaseCallbackContainer callbackContainer, Dictionary <Enum, List <BaseCallbackContainer> > dict) { // if dispatch are in progress we do not want to remove them immediately, we wait until all dispatch are complete before doing so if (_dispatchCount > 0) { if (_invalidCallbacks.ContainsKey(eventId)) { _invalidCallbacks[eventId].Add(callbackContainer); } else { _invalidCallbacks.Add(eventId, new List <BaseCallbackContainer>() { callbackContainer }); } } else { dict[eventId].Remove(callbackContainer); if (dict[eventId].Count == 0) { dict.Remove(eventId); } } }
private bool IsValidContainer(Enum eventId, BaseCallbackContainer callbackContainer) { if (_invalidCallbacks.ContainsKey(eventId) && _invalidCallbacks[eventId].Find(cb => callbackContainer == cb) != null) { return(false); } return(true); }
private void InternalAdd(Enum eventId, BaseCallbackContainer callbackContainer, Dictionary <Enum, List <BaseCallbackContainer> > dict) { if (dict.ContainsKey(eventId)) { dict[eventId].Add(callbackContainer); } else { dict.Add(eventId, new List <BaseCallbackContainer>() { callbackContainer }); } }
private void CompleteDispatch() { --_dispatchCount; if (_dispatchCount < 0) { throw new Exception("Dispatch count should never go below zero"); } // All dispatch are now complete, we can clear out all events that have been removed if (_dispatchCount == 0) { var enumerator = _invalidCallbacks.GetEnumerator(); while (enumerator.MoveNext()) { List <BaseCallbackContainer> invalids = _invalidCallbacks[enumerator.Current.Key]; int l = invalids.Count; for (int i = 0; i < l; ++i) { BaseCallbackContainer callbackContainer = invalids[i]; if (_callbacks.ContainsKey(enumerator.Current.Key)) { _callbacks[enumerator.Current.Key].Remove(callbackContainer); if (_callbacks[enumerator.Current.Key].Count == 0) { _callbacks.Remove(enumerator.Current.Key); } } if (_eventCallbacks.ContainsKey(enumerator.Current.Key)) { _eventCallbacks[enumerator.Current.Key].Remove(callbackContainer); if (_eventCallbacks[enumerator.Current.Key].Count == 0) { _eventCallbacks.Remove(enumerator.Current.Key); } } } } _invalidCallbacks.Clear(); } }
public void Remove <T>(Enum eventId, Callback <T> callback) { if (!_callbacks.ContainsKey(eventId)) { return; } List <BaseCallbackContainer> callbacks = _callbacks[eventId]; int l = callbacks.Count; for (int i = 0; i < l; ++i) { BaseCallbackContainer callbackContainer = callbacks[i]; if (((CallbackContainer <T>)callbackContainer).callback == callback) { InternalRemove(eventId, callbackContainer, _callbacks); break; } } }
public void Dispatch <T, U, V>(Enum eventId, T param1, U param2, V param3) { if (!_eventCallbacks.ContainsKey(eventId) && !_callbacks.ContainsKey(eventId)) { return; } ++_dispatchCount; if (_callbacks.ContainsKey(eventId)) { List <BaseCallbackContainer> callbacks = _callbacks[eventId]; int l = callbacks.Count; for (int i = 0; i < l; ++i) { BaseCallbackContainer callbackContainer = callbacks[i]; if (IsValidContainer(eventId, callbackContainer)) { ((CallbackContainer <T, U, V>)callbackContainer).callback(param1, param2, param3); } } } if (_eventCallbacks.ContainsKey(eventId)) { List <BaseCallbackContainer> callbacks = _eventCallbacks[eventId]; int l = callbacks.Count; for (int i = 0; i < l; ++i) { BaseCallbackContainer callbackContainer = callbacks[i]; if (IsValidContainer(eventId, callbackContainer)) { ((EventCallbackContainer <T, U, V>)callbackContainer).callback(eventId, param1, param2, param3); } } } CompleteDispatch(); }