Esempio n. 1
0
        /// <inheritdoc />
        public SubscriptionToken Subscribe <TEvent>(Action <TEvent> handler) where TEvent : EventBase
        {
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            lock (this.subscriptionsLock)
            {
                if (!this.subscriptions.ContainsKey(typeof(TEvent)))
                {
                    this.subscriptions.Add(typeof(TEvent), new List <ISubscription>());
                }

                var subscriptionToken = new SubscriptionToken(this, typeof(TEvent));
                this.subscriptions[typeof(TEvent)].Add(new Subscription <TEvent>(handler, subscriptionToken));

                return(subscriptionToken);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public void Unsubscribe(SubscriptionToken subscriptionToken)
        {
            // Ignore null token
            if (subscriptionToken == null)
            {
                this.logger.LogDebug("Unsubscribe called with a null token, ignored.");
                return;
            }

            lock (this.subscriptionsLock)
            {
                if (this.subscriptions.ContainsKey(subscriptionToken.EventType))
                {
                    var allSubscriptions = this.subscriptions[subscriptionToken.EventType];

                    var subscriptionToRemove = allSubscriptions.FirstOrDefault(sub => sub.SubscriptionToken.Token == subscriptionToken.Token);
                    if (subscriptionToRemove != null)
                    {
                        this.subscriptions[subscriptionToken.EventType].Remove(subscriptionToRemove);
                    }
                }
            }
        }
Esempio n. 3
0
 public Subscription(Action <TEventBase> action, SubscriptionToken token)
 {
     this.action            = action ?? throw new ArgumentNullException(nameof(action));
     this.SubscriptionToken = token ?? throw new ArgumentNullException(nameof(token));
 }