-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ObservableAsPropertyHelper.cs
executable file
·220 lines (197 loc) · 9.17 KB
/
ObservableAsPropertyHelper.cs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using Splat;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading;
namespace ReactiveUI
{
/// <summary>
/// ObservableAsPropertyHelper is a class to help ViewModels implement
/// "output properties", that is, a property that is backed by an
/// Observable. The property will be read-only, but will still fire change
/// notifications. This class can be created directly, but is more often created via the
/// ToProperty and ObservableToProperty extension methods.
/// </summary>
public sealed class ObservableAsPropertyHelper<T> : IHandleObservableErrors, IDisposable, IEnableLogger
{
T _lastValue;
readonly IConnectableObservable<T> _source;
IDisposable _inner;
private int _activated;
/// <summary>
/// Constructs an ObservableAsPropertyHelper object.
/// </summary>
/// <param name="observable">The Observable to base the property on.</param>
/// <param name="onChanged">The action to take when the property
/// changes, typically this will call the ViewModel's
/// RaisePropertyChanged method.</param>
/// <param name="initialValue">The initial value of the property.</param>
/// <param name="scheduler">The scheduler that the notifications will be
/// provided on - this should normally be a Dispatcher-based scheduler
/// (and is by default)</param>
public ObservableAsPropertyHelper(
IObservable<T> observable,
Action<T> onChanged,
T initialValue = default(T),
IScheduler scheduler = null) : this(observable, onChanged, null, initialValue, scheduler) {}
/// <summary>
/// Constructs an ObservableAsPropertyHelper object.
/// </summary>
/// <param name="observable">The Observable to base the property on.</param>
/// <param name="onChanged">The action to take when the property
/// changes, typically this will call the ViewModel's
/// RaisePropertyChanged method.</param>
/// <param name="onChanging">The action to take when the property
/// changes, typically this will call the ViewModel's
/// RaisePropertyChanging method.</param>
/// <param name="initialValue">The initial value of the property.</param>
/// <param name="scheduler">The scheduler that the notifications will be
/// provided on - this should normally be a Dispatcher-based scheduler
/// (and is by default)</param>
public ObservableAsPropertyHelper(
IObservable<T> observable,
Action<T> onChanged,
Action<T> onChanging = null,
T initialValue = default(T),
IScheduler scheduler = null)
{
Contract.Requires(observable != null);
Contract.Requires(onChanged != null);
scheduler = scheduler ?? CurrentThreadScheduler.Instance;
onChanging = onChanging ?? (_ => { });
var subj = new ScheduledSubject<T>(scheduler);
var exSubject = new ScheduledSubject<Exception>(CurrentThreadScheduler.Instance, RxApp.DefaultExceptionHandler);
subj.Subscribe(x => {
onChanging(x);
_lastValue = x;
onChanged(x);
}, exSubject.OnNext);
ThrownExceptions = exSubject;
_lastValue = initialValue;
_source = observable.StartWith(initialValue).DistinctUntilChanged().Multicast(subj);
if (ModeDetector.InUnitTestRunner())
{
_inner = _source.Connect();
_activated = 1;
}
}
/// <summary>
/// The last provided value from the Observable.
/// </summary>
public T Value {
get {
if (Interlocked.CompareExchange(ref _activated, 1, 0) == 0) {
_inner = _source.Connect();
}
return _lastValue;
}
}
/// <summary>
/// Fires whenever an exception would normally terminate ReactiveUI
/// internal state.
/// </summary>
public IObservable<Exception> ThrownExceptions { get; private set; }
public void Dispose()
{
(_inner ?? Disposable.Empty).Dispose();
_inner = null;
}
/// <summary>
/// Constructs a "default" ObservableAsPropertyHelper object. This is
/// useful for when you will initialize the OAPH later, but don't want
/// bindings to access a null OAPH at startup.
/// </summary>
/// <param name="initialValue">The initial (and only) value of the property.</param>
/// <param name="scheduler">The scheduler that the notifications will be
/// provided on - this should normally be a Dispatcher-based scheduler
/// (and is by default)</param>
public static ObservableAsPropertyHelper<T> Default(T initialValue = default(T), IScheduler scheduler = null)
{
return new ObservableAsPropertyHelper<T>(Observable.Never<T>(), _ => {}, initialValue, scheduler);
}
}
public static class OAPHCreationHelperMixin
{
static ObservableAsPropertyHelper<TRet> observableToProperty<TObj, TRet>(
this TObj This,
IObservable<TRet> observable,
Expression<Func<TObj, TRet>> property,
TRet initialValue = default(TRet),
IScheduler scheduler = null)
where TObj : IReactiveObject
{
Contract.Requires(This != null);
Contract.Requires(observable != null);
Contract.Requires(property != null);
Expression expression = Reflection.Rewrite(property.Body);
if (expression.GetParent().NodeType != ExpressionType.Parameter) {
throw new ArgumentException("Property expression must be of the form 'x => x.SomeProperty'");
}
var name = expression.GetMemberInfo().Name;
if (expression is IndexExpression)
name += "[]";
var ret = new ObservableAsPropertyHelper<TRet>(observable,
_ => This.raisePropertyChanged(name),
_ => This.raisePropertyChanging(name),
initialValue, scheduler);
return ret;
}
/// <summary>
/// Converts an Observable to an ObservableAsPropertyHelper and
/// automatically provides the onChanged method to raise the property
/// changed notification.
/// </summary>
/// <param name="source">The ReactiveObject that has the property</param>
/// <param name="property">An Expression representing the property (i.e.
/// 'x => x.SomeProperty'</param>
/// <param name="initialValue">The initial value of the property.</param>
/// <param name="scheduler">The scheduler that the notifications will be
/// provided on - this should normally be a Dispatcher-based scheduler
/// (and is by default)</param>
/// <returns>An initialized ObservableAsPropertyHelper; use this as the
/// backing field for your property.</returns>
public static ObservableAsPropertyHelper<TRet> ToProperty<TObj, TRet>(
this IObservable<TRet> This,
TObj source,
Expression<Func<TObj, TRet>> property,
TRet initialValue = default(TRet),
IScheduler scheduler = null)
where TObj : IReactiveObject
{
return source.observableToProperty(This, property, initialValue, scheduler);
}
/// <summary>
/// Converts an Observable to an ObservableAsPropertyHelper and
/// automatically provides the onChanged method to raise the property
/// changed notification.
/// </summary>
/// <param name="source">The ReactiveObject that has the property</param>
/// <param name="property">An Expression representing the property (i.e.
/// 'x => x.SomeProperty'</param>
/// <param name="initialValue">The initial value of the property.</param>
/// <param name="scheduler">The scheduler that the notifications will be
/// provided on - this should normally be a Dispatcher-based scheduler
/// (and is by default)</param>
/// <returns>An initialized ObservableAsPropertyHelper; use this as the
/// backing field for your property.</returns>
public static ObservableAsPropertyHelper<TRet> ToProperty<TObj, TRet>(
this IObservable<TRet> This,
TObj source,
Expression<Func<TObj, TRet>> property,
out ObservableAsPropertyHelper<TRet> result,
TRet initialValue = default(TRet),
IScheduler scheduler = null)
where TObj : IReactiveObject
{
var ret = source.observableToProperty(This, property, initialValue, scheduler);
result = ret;
return ret;
}
}
}
// vim: tw=120 ts=4 sw=4 et :