Esempio n. 1
0
        public static SubscriptionToken Subscribe <TEventBase>(Action <TEventBase> action) where TEventBase : EventBase
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            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);
        }
Esempio n. 2
0
        public static void Unsubscribe(SubscriptionToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(token));
            }

            if (!Subscriptions.ContainsKey(token.EventItemType))
            {
                return;
            }

            var allSubscriptions     = Subscriptions[token.EventItemType];
            var subscriptionToRemove = allSubscriptions.FirstOrDefault(x => x.SubscriptionToken.Token == token.Token);

            if (subscriptionToRemove != null)
            {
                Subscriptions[token.EventItemType].Remove(subscriptionToRemove);
            }
        }
Esempio n. 3
0
 public Subscription(Action <TEventBase> action, SubscriptionToken token)
 {
     _action           = action ?? throw new ArgumentNullException(nameof(action));
     SubscriptionToken = token ?? throw new ArgumentNullException(nameof(token));
 }