forked from PeteGoo/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeObjectReactiveHelper.cs
More file actions
66 lines (58 loc) · 2.82 KB
/
MakeObjectReactiveHelper.cs
File metadata and controls
66 lines (58 loc) · 2.82 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.ComponentModel;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using System.Reactive.Subjects;
namespace ReactiveUI
{
/// <summary>
/// This class helps you take existing objects and make them compatible with
/// ReactiveUI and Rx.Net. To use this, declare an instance field of this
/// class in your class, initialize it in your Constructor, make your class
/// derive from IReactiveNotifyPropertyChanged, then implement all of the
/// properties/methods using MakeObjectReactiveHelper.
/// </summary>
public class MakeObjectReactiveHelper : IReactiveNotifyPropertyChanged
{
public MakeObjectReactiveHelper(INotifyPropertyChanged hostObject)
{
var hostChanging = hostObject as INotifyPropertyChanging;
if (hostChanging != null) {
hostChanging.PropertyChanging += (o, e) => _Changing.OnNext(
new ObservedChange<object, object>() { Sender = o, PropertyName = e.PropertyName });
} else {
this.Log().ErrorFormat("'{0}' does not implement INotifyPropertyChanging - RxUI may return duplicate change notifications",
hostObject.GetType().FullName);
}
hostObject.PropertyChanged += (o, e) => _Changed.OnNext(
new ObservedChange<object, object>() { Sender = o, PropertyName = e.PropertyName });
}
int _changeCountSuppressed = 0;
public IDisposable SuppressChangeNotifications()
{
Interlocked.Increment(ref _changeCountSuppressed);
return Disposable.Create(() => Interlocked.Decrement(ref _changeCountSuppressed));
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
readonly ISubject<IObservedChange<object, object>> _Changing =
new ScheduledSubject<IObservedChange<object, object>>(RxApp.DeferredScheduler);
public IObservable<IObservedChange<object, object>> Changing {
#if SILVERLIGHT || PORTABLE_LIB
get { return _Changing.Where(_ => _changeCountSuppressed == 0); }
#else
get { return _Changing.Where(_ => Interlocked.Read(ref _changeCountSuppressed) == 0); }
#endif
}
ISubject<IObservedChange<object, object>> _Changed =
new ScheduledSubject<IObservedChange<object, object>>(RxApp.DeferredScheduler);
public IObservable<IObservedChange<object, object>> Changed {
#if SILVERLIGHT || PORTABLE_LIB
get { return _Changed.Where(_ => _changeCountSuppressed == 0); }
#else
get { return _Changed.Where(_ => Interlocked.Read(ref _changeCountSuppressed) == 0); }
#endif
}
}
}