Exemple #1
0
        /// <inheritdoc />
        public void Unsubscribe <TMessage>(MessageHubSubscriptionToken subscriptionToken) where TMessage : class, IMessageHubMessage
        {
            if (subscriptionToken == null)
            {
                throw new ArgumentNullException(nameof(subscriptionToken));
            }

            lock (_subscriptionsPadlock)
            {
                if (!_subscriptions.TryGetValue(typeof(TMessage), out var currentSubscriptions))
                {
                    return;
                }

                var currentlySubscribed = currentSubscriptions.Where(sub => ReferenceEquals(sub.Subscription.SubscriptionToken, subscriptionToken)).ToList();

                currentlySubscribed.ForEach(sub => currentSubscriptions.Remove(sub));
            }
        }
Exemple #2
0
        /// <summary>
        ///     Subscribe to a message type with the given destination and delivery action with the given filter.
        ///     Messages will be delivered via the specified proxy.
        ///     All references are held with WeakReferences
        ///     Only messages that "pass" the filter will be delivered.
        /// </summary>
        /// <typeparam name="TMessage">Type of message.</typeparam>
        /// <param name="deliveryAction">Action to invoke when message is delivered.</param>
        /// <param name="messageFilter">The message filter.</param>
        /// <param name="useStrongReferences">Use strong references to destination and deliveryAction.</param>
        /// <param name="proxy">Proxy to use when delivering the messages.</param>
        /// <returns>
        ///     MessageSubscription used to unsubscribing.
        /// </returns>
        public MessageHubSubscriptionToken Subscribe <TMessage>(Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter, bool useStrongReferences = true, IMessageHubProxy proxy = null) where TMessage : class, IMessageHubMessage
        {
            if (deliveryAction == null)
            {
                throw new ArgumentNullException(nameof(deliveryAction));
            }

            if (messageFilter == null)
            {
                throw new ArgumentNullException(nameof(messageFilter));
            }

            lock (_subscriptionsPadlock)
            {
                if (!_subscriptions.TryGetValue(typeof(TMessage), out var currentSubscriptions))
                {
                    currentSubscriptions             = new List <SubscriptionItem>();
                    _subscriptions[typeof(TMessage)] = currentSubscriptions;
                }

                var subscriptionToken = new MessageHubSubscriptionToken(this, typeof(TMessage));

                IMessageHubSubscription subscription;
                if (useStrongReferences)
                {
                    subscription = new StrongMessageSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter);
                }
                else
                {
                    subscription = new WeakMessageSubscription <TMessage>(subscriptionToken, deliveryAction, messageFilter);
                }

                currentSubscriptions.Add(new SubscriptionItem(proxy ?? MessageHubDefaultProxy.Instance, subscription));

                return(subscriptionToken);
            }
        }
Exemple #3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="StrongMessageSubscription{TMessage}" /> class.
 /// </summary>
 /// <param name="subscriptionToken">The subscription token.</param>
 /// <param name="deliveryAction">The delivery action.</param>
 /// <param name="messageFilter">The message filter.</param>
 /// <exception cref="ArgumentNullException">
 ///     subscriptionToken
 ///     or
 ///     deliveryAction
 ///     or
 ///     messageFilter.
 /// </exception>
 public StrongMessageSubscription(MessageHubSubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter)
 {
     SubscriptionToken = subscriptionToken ?? throw new ArgumentNullException(nameof(subscriptionToken));
     _deliveryAction   = deliveryAction;
     _messageFilter    = messageFilter;
 }
Exemple #4
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="WeakMessageSubscription{TMessage}" /> class.
 /// </summary>
 /// <param name="subscriptionToken">The subscription token.</param>
 /// <param name="deliveryAction">The delivery action.</param>
 /// <param name="messageFilter">The message filter.</param>
 /// <exception cref="ArgumentNullException">
 ///     subscriptionToken
 ///     or
 ///     deliveryAction
 ///     or
 ///     messageFilter.
 /// </exception>
 public WeakMessageSubscription(MessageHubSubscriptionToken subscriptionToken, Action <TMessage> deliveryAction, Func <TMessage, bool> messageFilter)
 {
     SubscriptionToken = subscriptionToken ?? throw new ArgumentNullException(nameof(subscriptionToken));
     _deliveryAction   = new WeakReference(deliveryAction);
     _messageFilter    = new WeakReference(messageFilter);
 }