Skip to content
Browse files

Warn people who attempt to use Property Change Tracking with non-Reac…

…tiveObjects, and enable support for INotifyPropertyChanged. Closes #185, maybe.
  • Loading branch information...
1 parent c7cb53e commit 7158f17ffefc65c654622cbaf42f67f26009077d @paulcbetts paulcbetts committed Feb 26, 2013
Showing with 24 additions and 41 deletions.
  1. +24 −7 ReactiveUI/ReactiveCollection.cs
  2. +0 −34 ReactiveUI/ReactiveNotifyPropertyChangedMixin.cs
View
31 ReactiveUI/ReactiveCollection.cs
@@ -18,7 +18,7 @@
namespace ReactiveUI
{
- public class ReactiveCollection<T> : IList<T>, IList, IReactiveCollection<T>, INotifyPropertyChanging, INotifyPropertyChanged
+ public class ReactiveCollection<T> : IList<T>, IList, IReactiveCollection<T>, INotifyPropertyChanging, INotifyPropertyChanged, IEnableLogger
{
public event NotifyCollectionChangedEventHandler CollectionChanging;
public event NotifyCollectionChangedEventHandler CollectionChanged;
@@ -408,20 +408,37 @@ public IDisposable SuppressChangeNotifications()
void addItemToPropertyTracking(T toTrack)
{
- var item = toTrack as IReactiveNotifyPropertyChanged;
- if (item == null)
- return;
-
if (_propertyChangeWatchers.ContainsKey(toTrack)) {
_propertyChangeWatchers[toTrack].AddRef();
return;
}
+ var changing = Observable.Never<IObservedChange<object, object>>();
+ var changed = Observable.Never<IObservedChange<object, object>>();
+
+ var irnpc = toTrack as IReactiveNotifyPropertyChanged;
+ if (irnpc != null) {
+ changing = irnpc.Changing;
+ changed = irnpc.Changed;
+ goto isSetup;
+ }
+
+ var inpc = toTrack as INotifyPropertyChanged;
+ if (inpc != null) {
+ changed = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(x => inpc.PropertyChanged += x, x => inpc.PropertyChanged -= x)
+ .Select(x => (IObservedChange<object, object>)
+ new ObservedChange<object, object>() { PropertyName = x.EventArgs.PropertyName, Sender = inpc });
+ goto isSetup;
+ }
+
+ this.Log().Warn("Property change notifications are enabled and type {0} isn't INotifyPropertyChanged or ReactiveObject", typeof(T));
+
+ isSetup:
var toDispose = new[] {
- item.Changing.Where(_ => _suppressionRefCount == 0).Subscribe(beforeChange =>
+ changing.Where(_ => _suppressionRefCount == 0).Subscribe(beforeChange =>
_itemChanging.Value.OnNext(new ObservedChange<T, object>() {
Sender = toTrack, PropertyName = beforeChange.PropertyName })),
- item.Changed.Where(_ => _suppressionRefCount == 0).Subscribe(change =>
+ changed.Where(_ => _suppressionRefCount == 0).Subscribe(change =>
_itemChanged.Value.OnNext(new ObservedChange<T,object>() {
Sender = toTrack, PropertyName = change.PropertyName })),
};
View
34 ReactiveUI/ReactiveNotifyPropertyChangedMixin.cs
@@ -222,40 +222,6 @@ public static class ReactiveNotifyPropertyChangedMixin
Contract.Requires(selector != null);
return This.ObservableForProperty(property, beforeChange).Select(x => selector(x.Value));
}
-
- /* NOTE: This is left here for reference - the real one is expanded out
- * to 10 parameters in VariadicTemplates.tt */
-#if FALSE
- public static IObservable<TRet> WhenAny<TSender, T1, T2, TRet>(this TSender This,
- Expression<Func<TSender, T1>> property1,
- Expression<Func<TSender, T2>> property2,
- Func<IObservedChange<TSender, T1>, IObservedChange<TSender, T2>, TRet> selector)
- where TSender : IReactiveNotifyPropertyChanged
- {
- var slot1 = new ObservedChange<TSender, T1>() {
- Sender = This,
- PropertyName = String.Join(".", RxApp.expressionToPropertyNames(property1)),
- };
- T1 slot1Value = default(T1); slot1.TryGetValue(out slot1Value); slot1.Value = slot1Value;
-
- var slot2 = new ObservedChange<TSender, T2>() {
- Sender = This,
- PropertyName = String.Join(".", RxApp.expressionToPropertyNames(property2)),
- };
- T2 slot2Value = default(T2); slot2.TryGetValue(out slot2Value); slot2.Value = slot2Value;
-
- IObservedChange<TSender, T1> islot1 = slot1;
- IObservedChange<TSender, T2> islot2 = slot2;
- return Observable.CreateWithDisposable<TRet>(subject => {
- subject.OnNext(selector(slot1, slot2));
-
- return Observable.Merge(
- This.ObservableForProperty(property1).Do(x => { lock (slot1) { islot1 = x.fillInValue(); } }).Select(x => selector(islot1, islot2)),
- This.ObservableForProperty(property2).Do(x => { lock (slot2) { islot2 = x.fillInValue(); } }).Select(x => selector(islot1, islot2))
- ).Subscribe(subject);
- });
- }
-#endif
}
}

0 comments on commit 7158f17

Please sign in to comment.
Something went wrong with that request. Please try again.