private BusEventSubscriptionToken AddSubscriptionInternal <TBusEvent>(Action <TBusEvent> deliveryAction, Func <TBusEvent, bool> eventFilter, bool strongReference, IBusEventProxy proxy) where TBusEvent : class, IBusEvent { if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (eventFilter == null) { throw new ArgumentNullException("eventFilter"); } if (proxy == null) { throw new ArgumentNullException("proxy"); } lock (m_subscriptionsPadlock) { List <SubscriptionItem> currentSubscriptions; if (!m_subscriptions.TryGetValue(typeof(TBusEvent), out currentSubscriptions)) { currentSubscriptions = new List <SubscriptionItem>(); m_subscriptions[typeof(TBusEvent)] = currentSubscriptions; } var subscriptionToken = new BusEventSubscriptionToken(this, typeof(TBusEvent)); IBusEventSubscription subscription; if (strongReference) { subscription = new StrongBusEventSubscription <TBusEvent>(subscriptionToken, deliveryAction, eventFilter); } else { subscription = new WeakBusEventSubscription <TBusEvent>(subscriptionToken, deliveryAction, eventFilter); } currentSubscriptions.Add(new SubscriptionItem(proxy, subscription)); return(subscriptionToken); } }
/// <summary> /// Initializes a new instance of the WeakBusEventSubscription class. /// </summary> /// <param name="destination">Destination object</param> /// <param name="deliveryAction">Delivery action</param> /// <param name="eventFilter">Filter function</param> public WeakBusEventSubscription(BusEventSubscriptionToken subscriptionToken, Action <TBusEvent> deliveryAction, Func <TBusEvent, bool> eventFilter) { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (eventFilter == null) { throw new ArgumentNullException("eventFilter"); } m_subscriptionToken = subscriptionToken; m_deliveryAction = new WeakReference(deliveryAction); m_eventFilter = new WeakReference(eventFilter); }
/// <summary> /// Initializes a new instance of the StrongBusEventSubscription class. /// </summary> /// <param name="destination">Destination object</param> /// <param name="deliveryAction">Delivery action</param> /// <param name="eventFilter">Filter function</param> public StrongBusEventSubscription(BusEventSubscriptionToken subscriptionToken, Action <TBusEvent> deliveryAction, Func <TBusEvent, bool> eventFilter) { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (eventFilter == null) { throw new ArgumentNullException("eventFilter"); } m_subscriptionToken = subscriptionToken; m_deliveryAction = deliveryAction; m_eventFilter = eventFilter; }
private void RemoveSubscriptionInternal <TBusEvent>(BusEventSubscriptionToken subscriptionToken) where TBusEvent : class, IBusEvent { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } lock (m_subscriptionsPadlock) { List <SubscriptionItem> currentSubscriptions; if (!m_subscriptions.TryGetValue(typeof(TBusEvent), out currentSubscriptions)) { return; } var currentlySubscribed = (from sub in currentSubscriptions where object.ReferenceEquals(sub.Subscription.SubscriptionToken, subscriptionToken) select sub).ToList(); currentlySubscribed.ForEach(sub => currentSubscriptions.Remove(sub)); } }
/// <summary> /// Unsubscribe from a particular bus event type. /// /// Does not throw an exception if the subscription is not found. /// </summary> /// <typeparam name="TBusEvent">Type of bus event</typeparam> /// <param name="subscriptionToken">Subscription token received from Subscribe</param> public void Unsubscribe <TBusEvent>(BusEventSubscriptionToken subscriptionToken) where TBusEvent : class, IBusEvent { RemoveSubscriptionInternal <TBusEvent>(subscriptionToken); }