public Subscription(Action <TEventBase> action, SubscriptionToken token) { if (action == null) { throw new ArgumentNullException("action"); } if (token == null) { throw new ArgumentNullException("token"); } _action = action; _subscriptionToken = token; }
// Unsubscribe from the Event type related to the specified "SubscriptionToken" // param = "token" The "SubscriptionToken" received from calling the Subscribe method public void Unsubscribe(SubscriptionToken token) { if (token == null) { throw new ArgumentNullException("token"); } lock (SubscriptionsLock) { if (_subscriptions.ContainsKey(token.EventItemType)) { var allSubscriptions = _subscriptions[token.EventItemType]; var subscriptionToRemove = allSubscriptions.FirstOrDefault(x => x.SubscriptionToken.Token == token.Token); if (subscriptionToRemove != null) { _subscriptions[token.EventItemType].Remove(subscriptionToRemove); } } } }
// Subscribes to the specified event type with the specified action // param = "action" The Action to invoke when an event of this type is published // Returns A "SubscriptionToken" to be used when calling "Unsubscribe" public SubscriptionToken Subscribe <TEventBase>(Action <TEventBase> action) where TEventBase : EventBase { if (action == null) { throw new ArgumentNullException("action"); } lock (SubscriptionsLock) { if (!_subscriptions.ContainsKey(typeof(TEventBase))) { _subscriptions.Add(typeof(TEventBase), new List <ISubscription>()); } var token = new SubscriptionToken(typeof(TEventBase)); _subscriptions[typeof(TEventBase)].Add(new Subscription <TEventBase>(action, token)); return(token); } }