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));
        }
 protected BaseSubscription(IMvxActionRunner actionRunner, string tag)
 {
     _actionRunner = actionRunner;
     Id = Guid.NewGuid();
     Tag = tag;
 }
Example #3
0
 protected BaseSubscription(IMvxActionRunner actionRunner)
 {
     _actionRunner = actionRunner;
     Id            = Guid.NewGuid();
 }
Example #4
0
 public WeakSubscription(IMvxActionRunner actionRunner, Action <TMessage> listener, string tag)
     : base(actionRunner, tag)
 {
     _weakReference = new WeakReference(listener);
 }
 protected TypedSubscription(IMvxActionRunner actionRunner, string tag)
     : base(actionRunner, tag)
 {
 }
Example #6
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));
        }
 protected TypedSubscription(IMvxActionRunner actionRunner)
     : base(actionRunner)
 {
 }
 protected BaseSubscription(IMvxActionRunner actionRunner, string tag)
 {
     _actionRunner = actionRunner;
     Id            = Guid.NewGuid();
     Tag           = tag;
 }
 public StrongSubscription(IMvxActionRunner actionRunner, Action <TMessage> action, string tag)
     : base(actionRunner, tag)
 {
     _action = action;
 }
Example #10
0
 public StrongSubscription(IMvxActionRunner actionRunner, Action <TMessage> action)
     : base(actionRunner)
 {
     _action = action;
 }
 protected BaseSubscription(IMvxActionRunner actionRunner)
 {
     _actionRunner = actionRunner;
     Id = Guid.NewGuid();
 }