forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRNPCObservableForProperty.cs
More file actions
36 lines (32 loc) · 1.37 KB
/
IRNPCObservableForProperty.cs
File metadata and controls
36 lines (32 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Reactive.Linq;
using System.Reflection;
namespace ReactiveUI
{
/// <summary>
/// Generates Observables based on observing Reactive objects
/// </summary>
public class IRNPCObservableForProperty : ICreatesObservableForProperty
{
public int GetAffinityForObject(Type type, string propertyName, bool beforeChanged = false)
{
// NB: Since every IRNPC is also an INPC, we need to bind more
// tightly than INPCObservableForProperty, so we return 10 here
// instead of one
return typeof (IReactiveNotifyPropertyChanged).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) ? 10 : 0;
}
public IObservable<IObservedChange<object, object>> GetNotificationForProperty(object sender, string propertyName, bool beforeChanged = false)
{
var irnpc = sender as IReactiveNotifyPropertyChanged;
if (irnpc == null) {
throw new ArgumentException("Sender doesn't implement IReactiveNotifyPropertyChanging");
}
return Observable.Create<IObservedChange<object, object>>(subj => {
var obs = (beforeChanged ? irnpc.Changing : irnpc.Changed);
return obs
.Where(x => x.PropertyName == propertyName)
.Subscribe(subj);
});
}
}
}