Esempio n. 1
0
 /// <summary>
 /// Removes the event if empty.
 /// </summary>
 /// <param name="eventSubject">The event subject.</param>
 private void RemoveEventSubjectIfEmpty(EventSubject eventSubject)
 {
     if (!eventSubject.HasPublications && !eventSubject.HasSubscriptions)
     {
         this.subjects.Remove(eventSubject.Subject);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Registers the publisher.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="publisher">The publisher.</param>
 /// <param name="eventName">Name of the event.</param>
 public void RegisterPublisher(string subject, object publisher, string eventName)
 {
     lock (this.subjects)
     {
         EventSubject publishedEvent = this.GetEvent(subject);
         publishedEvent.AddPublication(new Publication(publishedEvent, publisher, eventName));
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Gets the event.
 /// </summary>
 /// <param name="eventName">Name of the event.</param>
 /// <returns><see cref="EventSubject"/> for the specified event name.</returns>
 private EventSubject GetEvent(string eventName)
 {
     lock (this.subjects)
     {
         if (!this.subjects.ContainsKey(eventName))
         {
             var subject = new EventSubject(eventName);
             this.subjects.Add(eventName, subject);
             return(subject);
         }
         return(this.subjects[eventName]);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Registers the subscriber.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodName">Name of the handler method.</param>
        /// <param name="threadOption">The thread option.</param>
        public void RegisterSubscriber(string subject, object subscriber, string handlerMethodName, ThreadOption threadOption)
        {
            Requires.NotNull(subject, "subject");
            Requires.NotNull(subscriber, "subscriber");
            Requires.NotNullOrEmpty(handlerMethodName, "handlerMethodName");

            this.VerifyThreadOption(threadOption);

            lock (this.subjects)
            {
                EventSubject publishedEvent = this.GetEvent(subject);
                publishedEvent.AddSubscription(new WeakSubscription(publishedEvent, subscriber, handlerMethodName, this.dispatcher, threadOption));
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Unsubscribes for.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="predicate">The predicate.</param>
        private void RemoveSubscriptionFor(string subject, Func <SubscriptionBase, bool> predicate)
        {
            lock (this.subjects)
            {
                EventSubject     publishedEvent       = this.GetEvent(subject);
                SubscriptionBase subscriptionToRemove = publishedEvent.Subscriptions.LastOrDefault(predicate);

                if (subscriptionToRemove != null)
                {
                    publishedEvent.RemoveSubscription(subscriptionToRemove);

                    this.RemoveEventSubjectIfEmpty(publishedEvent);
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Registers the subscriber for.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="handlerAction">The handler action.</param>
 /// <param name="threadOption">The thread option.</param>
 private void AddSubscribtionFor(string subject, Delegate handlerAction, ThreadOption threadOption)
 {
     lock (this.subjects)
     {
         EventSubject     publishedEvent = this.GetEvent(subject);
         SubscriptionBase subscription;
         if (handlerAction.Method.IsStatic)
         {
             subscription = new StaticSubscription(handlerAction, this.dispatcher, threadOption);
         }
         else
         {
             subscription = new WeakSubscription(publishedEvent, handlerAction, this.dispatcher, threadOption);
         }
         publishedEvent.AddSubscription(subscription);
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="subscriber">The subscriber.</param>
        /// <param name="handlerMethodInfo">The handler method info.</param>
        /// <param name="dispatcher">The UI thread dispatcher.</param>
        /// <param name="threadOption">The thread option.</param>
        protected WeakSubscription(EventSubject subject, object subscriber, MethodInfo handlerMethodInfo, Dispatcher dispatcher, ThreadOption threadOption)
            : base(handlerMethodInfo, dispatcher, threadOption)
        {
            Requires.NotNull(subject, "subject");
            Requires.NotNull(subscriber, "subscriber");
            Requires.NotNull(handlerMethodInfo, "handlerMethodInfo");

            if (handlerMethodInfo.IsStatic)
            {
                throw ExceptionBuilder.ArgumentNotValid("handlerMethodInfo", Messages.PassedMethodCantBeStatic);
            }

            this._handlerMethodName  = handlerMethodInfo.Name;
            this.subject             = subject;
            this.subscriberReference = new WeakReference(subscriber);
            this.typeHandle          = subscriber.GetType().TypeHandle;
            this.methodHandle        = handlerMethodInfo.MethodHandle;
        }
Esempio n. 8
0
        /// <summary>
        /// Unregisters the publisher.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventName">Name of the event.</param>
        public void UnregisterPublisher(string subject, object publisher, string eventName)
        {
            lock (this.subjects)
            {
                EventSubject publishedEvent = this.GetEvent(subject);

                List <Publication> publicationsToRemove =
                    new List <Publication>(
                        publishedEvent.Publications.Where(p => (p.EventName == eventName && p.Publisher == publisher)));

                foreach (Publication publication in publicationsToRemove)
                {
                    publishedEvent.RemovePublication(publication);
                }

                this.RemoveEventSubjectIfEmpty(publishedEvent);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Publication"/> class.
        /// </summary>
        /// <param name="subject">The subject.</param>
        /// <param name="publisher">The publisher.</param>
        /// <param name="eventName">Name of the event.</param>
        internal Publication(EventSubject subject, object publisher, string eventName)
        {
            Requires.NotNull(subject, "subject");

            this.subject            = subject;
            this.publisherReference = new WeakReference(publisher);
            this.EventName          = eventName;

            Type      publisherType  = publisher.GetType();
            EventInfo publishedEvent = publisherType.GetEvent(eventName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);

            if (publishedEvent == null)
            {
                throw new ArgumentException(string.Format(Messages.PublicEventNotFound, eventName, publisherType.Name));
            }

            EnsureIfIsSupported(publishedEvent);

            Delegate handler = Delegate.CreateDelegate(publishedEvent.EventHandlerType, this, this.GetType().GetMethod(EventHandlerMethodName));

            publishedEvent.AddEventHandler(publisher, handler);
        }
Esempio n. 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="subscriber">The subscriber.</param>
 /// <param name="handlerMethodName">Name of the handler method.</param>
 /// <param name="dispatcher">The UI thread dispatcher.</param>
 /// <param name="threadOption">The thread option.</param>
 internal WeakSubscription(EventSubject subject, object subscriber, string handlerMethodName, Dispatcher dispatcher, ThreadOption threadOption)
     : this(subject, subscriber, GetMethodInfo(subscriber, handlerMethodName, null), dispatcher, threadOption)
 {
 }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="WeakSubscription"/> class.
 /// </summary>
 /// <param name="subject">The subject.</param>
 /// <param name="handlerAction">The handler action.</param>
 /// <param name="dispatcher">The UI thread dispatcher.</param>
 /// <param name="threadOption">The thread option.</param>
 internal WeakSubscription(EventSubject subject, Delegate handlerAction, Dispatcher dispatcher, ThreadOption threadOption)
     : this(subject, CheckHandlerAction(handlerAction).Target, CheckHandlerAction(handlerAction).Method, dispatcher, threadOption)
 {
 }