private TinyMessageSubscriptionToken AddSubscriptionInternal <TMessage>(Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter, bool strongReference, ITinyMessageProxy proxy) where TMessage : class, ITinyMessage { if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (messageFilter == null) { throw new ArgumentNullException("messageFilter"); } if (proxy == null) { throw new ArgumentNullException("proxy"); } lock (_SubscriptionsPadlock) { List <SubscriptionItem> currentSubscriptions; if (!_Subscriptions.TryGetValue(typeof(TMessage), out currentSubscriptions)) { currentSubscriptions = new List <SubscriptionItem>(); _Subscriptions[typeof(TMessage)] = currentSubscriptions; } var subscriptionToken = new TinyMessageSubscriptionToken(this, typeof(TMessage)); ITinyMessageSubscription subscription; if (strongReference) { subscription = new StrongTinyMessageSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter); } else { subscription = new WeakTinyMessageSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter); } currentSubscriptions.Add(new SubscriptionItem(proxy, subscription)); return(subscriptionToken); } }
/// <summary> /// Initializes a new instance of the TinyMessageSubscription class. /// </summary> /// <param name="destination">Destination object</param> /// <param name="deliveryAction">Delivery action</param> /// <param name="messageFilter">Filter function</param> public StrongTinyMessageSubscription(TinyMessageSubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter) { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (messageFilter == null) { throw new ArgumentNullException("messageFilter"); } _SubscriptionToken = subscriptionToken; _DeliveryAction = deliveryAction; _MessageFilter = messageFilter; }
/// <summary> /// Initializes a new instance of the WeakTinyMessageSubscription class. /// </summary> /// <param name="destination">Destination object</param> /// <param name="deliveryAction">Delivery action</param> /// <param name="messageFilter">Filter function</param> public WeakTinyMessageSubscription(TinyMessageSubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter) { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } if (deliveryAction == null) { throw new ArgumentNullException("deliveryAction"); } if (messageFilter == null) { throw new ArgumentNullException("messageFilter"); } _SubscriptionToken = subscriptionToken; _DeliveryAction = new WeakReference(deliveryAction); _MessageFilter = new WeakReference(messageFilter); }
private void RemoveSubscriptionInternal <TMessage>(TinyMessageSubscriptionToken subscriptionToken) where TMessage : class, ITinyMessage { if (subscriptionToken == null) { throw new ArgumentNullException("subscriptionToken"); } lock (_SubscriptionsPadlock) { List <SubscriptionItem> currentSubscriptions; if (!_Subscriptions.TryGetValue(typeof(TMessage), 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 message type. /// /// Does not throw an exception if the subscription is not found. /// </summary> /// <typeparam name="TMessage">Type of message</typeparam> /// <param name="subscriptionToken">Subscription token received from Subscribe</param> public void Unsubscribe <TMessage>(TinyMessageSubscriptionToken subscriptionToken) where TMessage : class, ITinyMessage { RemoveSubscriptionInternal <TMessage>(subscriptionToken); }