예제 #1
0
        public SubscriptionToken Subscribe <TEventBase>(Action <TEventBase> action) where TEventBase : EventBase
        {
            if (action == null)
            {
                throw new ArgumentNullException();
            }

            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);
            }
        }
예제 #2
0
        public void Unsubscribe(SubscriptionToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException();
            }

            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);
                    }
                }
            }
        }