Beispiel #1
0
        /// <inheritdoc />
        /// <summary>
        /// Remove the observer for a given notifyContext from an observer list for a given Notification name.
        /// </summary>
        /// <param name="notificationName">which observer list to remove from </param>
        /// <param name="notifyContext">remove the observer with this object as its notifyContext</param>
        public virtual void RemoveObserver(string notificationName, object notifyContext)
        {
            IList <IObserver> observers;

            if (!ObserverMap.TryGetValue(notificationName, out observers))
            {
                return;
            }
            for (var i = 0; i < observers.Count; i++)
            {
                if (!observers[i].CompareNotifyContext(notifyContext))
                {
                    continue;
                }
                observers.RemoveAt(i);
                break;
            }

            // Also, when a Notification's Observer list length falls to
            // zero, delete the notification key from the observer map
            if (observers.Count == 0 && ObserverMap.ContainsKey(notificationName))
            {
                ObserverMap.Remove(notificationName);
            }
        }
Beispiel #2
0
 /// <inheritdoc />
 /// <summary>
 ///     Register an <c>IObserver</c> to be notified
 ///     of <c>INotifications</c> with a given name.
 /// </summary>
 /// <param name="notificationName">the name of the <c>INotifications</c> to notify this <c>IObserver</c> of</param>
 /// <param name="observer">the <c>IObserver</c> to register</param>
 public virtual void RegisterObserver(string notificationName, IObserver observer)
 {
     if (!ObserverMap.ContainsKey(notificationName))
     {
         ObserverMap.Add(notificationName, new List <IObserver>());
     }
     ObserverMap[notificationName].Add(observer);
 }
Beispiel #3
0
        /// <inheritdoc />
        /// <summary>
        /// Notify the <c>IObservers</c> for a particular <c>INotification</c>.
        /// </summary>
        /// <remarks>
        ///     <para>
        ///         All previously attached <c>IObservers</c> for this <c>INotification</c>'s
        ///         list are notified and are passed a reference to the <c>INotification</c> in
        ///         the order in which they were registered.
        ///     </para>
        /// </remarks>
        /// <param name="notification"></param>
        public virtual void NotifyObservers(INotification notification)
        {
            // Get a reference to the observers list for this notification name
            IList <IObserver> observersRef;

            if (!ObserverMap.TryGetValue(notification.Name, out observersRef))
            {
                return;
            }
            // Copy observers from reference array to working array,
            // since the reference array may change during the notification loop
            var observers = new List <IObserver>(observersRef);

            foreach (var observer in observers)
            {
                observer.NotifyObserver(notification);
            }
        }