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