private void CleanupSubscription(PropertyChangedSubscription sub) { List <PropertyChangedSubscription> subsForProperty; if (subscribers.TryGetValue(sub.PropertyName, out subsForProperty) == false) { throw new KeyNotFoundException("The given subscription was not found"); } subsForProperty.Remove(sub); if (subsForProperty.Count == 0) { subscribers.Remove(sub.PropertyName); } }
/// <summary> /// Subscribes to be notified when the given property changes. /// </summary> /// <param name="propertyName">The name of the property to subscribe to or ObservableObject.AnyProperty if you want to be notified of any property change.</param> /// <param name="handler">The action to call for notifications</param> /// <returns>A subscription that will receive notifications until it is disposed</returns> public IDisposable SubscribeUnmanaged(string propertyName, Action handler) { if (propertyName.Contains(".") == false) { var sub = new PropertyChangedSubscription(propertyName, handler, CleanupSubscription); List <PropertyChangedSubscription> subsForProperty; if (subscribers.TryGetValue(propertyName, out subsForProperty) == false) { subsForProperty = new List <PropertyChangedSubscription>(); subscribers.Add(propertyName, subsForProperty); } subsForProperty.Add(sub); return(sub); } else { return(DeepSubscribeInternal(propertyName, handler)); } }