Exemplo n.º 1
0
        public SubscriptionToken Subscribe <TEventBase>(string topic, Action <TEventBase> action) where TEventBase : EventBase
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            lock (SubscriptionsLock)
            {
                if (!_subscriptions.ContainsKey(topic))
                {
                    Dictionary <Type, List <ISubscription> > tmpDict = new Dictionary <Type, List <ISubscription> >();
                    tmpDict.Add(typeof(TEventBase), new List <ISubscription>());
                    _subscriptions.Add(topic, tmpDict);
                }

                var token = new SubscriptionToken(typeof(TEventBase));
                _subscriptions[topic][typeof(TEventBase)].Add(new Subscription <TEventBase>(action, token));
                return(token);
            }
        }
Exemplo n.º 2
0
        public void Unsubscribe(SubscriptionToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            lock (SubscriptionsLock)
            {
                foreach (var item in _subscriptions.Values)
                {
                    if (item.ContainsKey(token.EventItemType))
                    {
                        List <ISubscription> list = item[token.EventItemType];
                        ISubscription        subscriptionToRemove = list.FirstOrDefault(x => x.SubscriptionToken.Token == token.Token);
                        if (subscriptionToRemove != null)
                        {
                            item[token.EventItemType].Remove(subscriptionToRemove);
                            break;
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public Subscription(Action <TEventBase> action, SubscriptionToken subscriptionToken)
 {
     _action           = action ?? throw new ArgumentNullException(nameof(action));
     SubscriptionToken = subscriptionToken ?? throw new ArgumentNullException(nameof(subscriptionToken));
 }