/// <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); } }
/// <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); } }