Exemplo n.º 1
0
        /// <summary>
        ///cqgis:  订阅消息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public SubscriptionToken Subscribe(Type type, Action <EventBase> action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            lock (SubscriptionsLock)
            {
                if (!_subscriptions.ContainsKey(type))
                {
                    _subscriptions.Add(type, new List <ISubscription>());
                }

                var token = new SubscriptionToken(type);
                _subscriptions[type].Add(new Subscription <EventBase>(action, token));
                return(token);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///cqgis:  取消订阅
        /// </summary>
        /// <param name="token"></param>
        public void Unsubscribe(SubscriptionToken token)
        {
            if (token == null)
            {
                throw new ArgumentNullException(nameof(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);
                    }
                }
            }
        }