Пример #1
0
        private void RemoveSubscriptionInternal <TMessage>(SubscriptionToken subscriptionToken)
            where TMessage : class, IMapleMessage
        {
            if (subscriptionToken == null)
            {
                throw new ArgumentNullException(nameof(subscriptionToken), $"{nameof(subscriptionToken)} {Resources.IsRequired}");
            }

            lock (_SubscriptionsPadlock)
            {
                var currentlySubscribed = (from sub in _subscriptions
                                           where ReferenceEquals(sub.Subscription.SubscriptionToken, subscriptionToken)
                                           select sub).ToList();

                currentlySubscribed.ForEach(sub => _subscriptions.Remove(sub));
            }
        }
        public WeakMapleMessageSubscription(ITranslationProvider translationProvider, SubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter)
        {
            if (deliveryAction == null)
            {
                throw new ArgumentNullException(nameof(deliveryAction), $"{nameof(deliveryAction)} {Resources.IsRequired}");
            }

            if (messageFilter == null)
            {
                throw new ArgumentNullException(nameof(messageFilter), $"{nameof(messageFilter)} {Resources.IsRequired}");
            }

            _translationProvider = translationProvider ?? throw new ArgumentNullException(nameof(translationProvider), $"{nameof(translationProvider)} {Resources.IsRequired}");

            SubscriptionToken = subscriptionToken ?? throw new ArgumentNullException(nameof(subscriptionToken), $"{nameof(subscriptionToken)} {Resources.IsRequired}");
            DeliveryAction    = new WeakReference(deliveryAction);
            MessageFilter     = new WeakReference(messageFilter);
        }
Пример #3
0
        private SubscriptionToken AddSubscriptionInternal <TMessage>(Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter, bool strongReference, IMapleMessageProxy proxy)
            where TMessage : class, IMapleMessage
        {
            if (deliveryAction == null)
            {
                throw new ArgumentNullException(nameof(deliveryAction), $"{nameof(deliveryAction)} {Resources.IsRequired}");
            }

            if (messageFilter == null)
            {
                throw new ArgumentNullException(nameof(messageFilter), $"{nameof(messageFilter)} {Resources.IsRequired}");
            }

            if (proxy == null)
            {
                throw new ArgumentNullException(nameof(proxy), $"{nameof(proxy)} {Resources.IsRequired}");
            }

            lock (_SubscriptionsPadlock)
            {
                var subscriptionToken = new SubscriptionToken(this, typeof(TMessage));

                IMapleMessageSubscription subscription;
                if (strongReference)
                {
                    subscription = new StrongMapleMessageSubscription <TMessage>(_translationProvider, subscriptionToken, deliveryAction, messageFilter);
                }
                else
                {
                    subscription = new WeakMapleMessageSubscription <TMessage>(_translationProvider, subscriptionToken, deliveryAction, messageFilter);
                }

                _subscriptions.Add(new SubscriptionItem(proxy, subscription));

                return(subscriptionToken);
            }
        }
Пример #4
0
 public void Unsubscribe <TMessage>(SubscriptionToken subscriptionToken)
     where TMessage : class, IMapleMessage
 {
     RemoveSubscriptionInternal <TMessage>(subscriptionToken);
 }
        public StrongMapleMessageSubscription(ITranslationProvider translationProvider, SubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter)
        {
            SubscriptionToken = subscriptionToken ?? throw new ArgumentNullException(nameof(subscriptionToken), $"{nameof(subscriptionToken)} {Resources.IsRequired}");
            DeliveryAction    = deliveryAction ?? throw new ArgumentNullException(nameof(deliveryAction), $"{nameof(deliveryAction)} {Resources.IsRequired}");
            MessageFilter     = messageFilter ?? throw new ArgumentNullException(nameof(messageFilter), $"{nameof(messageFilter)} {Resources.IsRequired}");

            _translationProvider = translationProvider ?? throw new ArgumentNullException(nameof(translationProvider), $"{nameof(translationProvider)} {Resources.IsRequired}");
        }