public Action Add(object sender, Action localDelegate, EventQueueMode mode = EventQueueMode.Synchronous) { var key = new EventHandlerKey(localDelegate, sender); Action ourDelegate = () => OnRemoteInvoked(key, new object[0]); return(AddInternal <Action>(key, mode, sender, localDelegate, ourDelegate)); }
// Add custom methods public TDelegateType AddInternal <TDelegateType>( EventHandlerKey key, EventQueueMode mode, object sender, Delegate localDelegate, Delegate ourDelegate) { Assert.IsNotNull(localDelegate); HandlerInfo handler; if (_handlers.TryGetValue(key, out handler)) { Assert.IsEqual(mode, handler.Mode, "Cannot use the same event handler method with multiple different EventQueueMode's"); handler.UsageCount++; return((TDelegateType)(object)handler.OurDelegate); } _handlers.Add(key, new HandlerInfo() { UsageCount = 1, Key = key, Mode = mode, OurDelegate = ourDelegate, DelegateType = typeof(TDelegateType), LocalDelegate = localDelegate, }); return((TDelegateType)(object)ourDelegate); }
public Action <TParam1, TParam2, TParam3, TParam4> Add <TParam1, TParam2, TParam3, TParam4>( object sender, Action <TParam1, TParam2, TParam3, TParam4> localDelegate, EventQueueMode mode) { var key = new EventHandlerKey(localDelegate, sender); Action <TParam1, TParam2, TParam3, TParam4> ourDelegate = (TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4) => OnRemoteInvoked(key, new object[] { param1, param2, param3, param4 }); return(AddInternal <Action <TParam1, TParam2, TParam3, TParam4> >(key, mode, sender, localDelegate, ourDelegate)); }
public void OnRemoteInvoked(EventHandlerKey key, object[] args) { Assert.That(_handlers.ContainsKey(key), "Received invoke for unrecognized event '{0}'", key); HandlerInfo handlerInfo; // If we can't find the handler than the local object that owns // the event handler is probably deleted so just ignore if (_handlers.TryGetValue(key, out handlerInfo)) { OnRemoteInvoked(_handlers[key], args); } }
// Remove custom methods public TDelegateType RemoveInternal <TDelegateType>( object sender, TDelegateType localDelegate) where TDelegateType : class { var key = new EventHandlerKey((Delegate)(object)localDelegate, sender); var handlerInfo = _handlers[key]; // There is a minor bug here where sometimes the handler items won't be removed // from the queue when UsageCount > 1 if (handlerInfo.UsageCount == 1) { _handlers.RemoveWithConfirm(key); RemoveHandlerItemsFromQueue(handlerInfo); } else { handlerInfo.UsageCount--; } return((TDelegateType)(object)handlerInfo.OurDelegate); }
// We could make this public but there's only two queue modes // that make sense so it's easier this way void TriggerOneOff(Action localDelegate, EventQueueMode mode) { var key = new EventHandlerKey(localDelegate, null); if (_handlers.ContainsKey(key)) { // Already queued return; } _handlers.Add(key, new HandlerInfo() { UsageCount = 1, Key = key, Mode = mode, IsOneOff = true, DelegateType = typeof(Action), LocalDelegate = localDelegate, }); OnRemoteInvoked(key, new object[0]); }
void TriggerInternal(object sender, Delegate localDelegate, object[] args) { var key = new EventHandlerKey(localDelegate, sender); OnRemoteInvoked(key, args); }