Example #1
0
        private MvxSubscriptionToken SubscribeInternal <TMessage>(
            Action <TMessage> deliveryAction,
            IMvxActionRunner actionRunner,
            MvxReference reference,
            string?tag,
            bool isSticky) where TMessage : MvxMessage
        {
            if (deliveryAction == null)
            {
                throw new ArgumentNullException(nameof(deliveryAction));
            }

            BaseSubscription subscription;

            switch (reference)
            {
            case MvxReference.Strong:
                subscription = new StrongSubscription <TMessage>(actionRunner, deliveryAction, tag);
                break;

            case MvxReference.Weak:
                subscription = new WeakSubscription <TMessage>(actionRunner, deliveryAction, tag);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(reference), "reference type unexpected " + reference);
            }

            lock (_locker)
            {
                if (!_subscriptions.TryGetValue(
                        typeof(TMessage), out Dictionary <Guid, BaseSubscription> messageSubscriptions))
                {
                    messageSubscriptions             = new Dictionary <Guid, BaseSubscription>();
                    _subscriptions[typeof(TMessage)] = messageSubscriptions;
                }
                MvxPluginLog.Instance?.Log(LogLevel.Trace, "Adding subscription {0} for {1}", subscription.Id, typeof(TMessage).Name);
                messageSubscriptions[subscription.Id] = subscription;

                PublishSubscriberChangeMessage <TMessage>(messageSubscriptions);
            }

            if (isSticky && _messageDictionary.ContainsKey(typeof(TMessage)))
            {
                deliveryAction.Invoke((TMessage)_messageDictionary[typeof(TMessage)]);
            }

            return(new MvxSubscriptionToken(
                       subscription.Id,
                       () => InternalUnsubscribe <TMessage>(subscription.Id),
                       deliveryAction));
        }
Example #2
0
        private MvxSubscriptionToken SubscribeInternal <TMessage>(Action <TMessage> deliveryAction, IMvxActionRunner actionRunner, MvxReference reference, string tag)
            where TMessage : MvxMessage
        {
            if (deliveryAction == null)
            {
                throw new ArgumentNullException(nameof(deliveryAction));
            }

            BaseSubscription subscription;

            switch (reference)
            {
            case MvxReference.Strong:
                subscription = new StrongSubscription <TMessage>(actionRunner, deliveryAction, tag);
                break;

            case MvxReference.Weak:
                subscription = new WeakSubscription <TMessage>(actionRunner, deliveryAction, tag);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(reference), "reference type unexpected " + reference);
            }

            lock (this)
            {
                Dictionary <Guid, BaseSubscription> messageSubscriptions;
                if (!_subscriptions.TryGetValue(typeof(TMessage), out messageSubscriptions))
                {
                    messageSubscriptions             = new Dictionary <Guid, BaseSubscription>();
                    _subscriptions[typeof(TMessage)] = messageSubscriptions;
                }
                MvxTrace.Trace("Adding subscription {0} for {1}", subscription.Id, typeof(TMessage).Name);
                messageSubscriptions[subscription.Id] = subscription;

                PublishSubscriberChangeMessage <TMessage>(messageSubscriptions);
            }

            return(new MvxSubscriptionToken(
                       subscription.Id,
                       () => InternalUnsubscribe <TMessage>(subscription.Id),
                       deliveryAction));
        }