-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertySourceLinq.cs
290 lines (255 loc) · 14.1 KB
/
PropertySourceLinq.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Text;
using System.Threading.Tasks;
namespace ReactiveProperties
{
public static partial class PropertySource
{
/// <summary>
/// Creates an immutable property source with the given value. This is the return operator of the property source monad.
/// </summary>
/// <typeparam name="T">The type of the property source.</typeparam>
/// <param name="value">The value of the property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<T> Return<T>(T value)
{
return PropertySource.Create(observer => Disposable.Empty, () => value);
}
/// <summary>
/// Given a property source, creates a property source that notifies only when the original property source's value changes according to the provided equality comparer.
/// </summary>
/// <typeparam name="T">The type of the property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <param name="comparer">The comparer that determines whether the property source value has changed.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<T> Distinct<T>(this IPropertySource<T> source, IEqualityComparer<T> comparer)
{
if (source == null) throw new ArgumentNullException("source");
if (comparer == null) throw new ArgumentNullException("comparer");
return PropertySource.Create(
action =>
{
T cachedValue = default(T);
Action<T> sendAndCache = value =>
{
action();
cachedValue = value;
};
Action sendIfChanged = () =>
{
var value = source.Value;
if (!comparer.Equals(value, cachedValue))
sendAndCache(value);
};
Action observer = null;
observer = () =>
{
observer = sendIfChanged;
sendAndCache(source.Value);
};
return source.RawSubscribe(() =>
{
observer();
});
},
() => source.Value
);
}
/// <summary>
/// Given a property source, creates a property source that notifies only when the original property source's value changes.
/// </summary>
/// <typeparam name="T">The type of the property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<T> Distinct<T>(this IPropertySource<T> source)
{
if (source == null) throw new ArgumentNullException("source");
return source.Distinct(EqualityComparer<T>.Default);
}
/// <summary>
/// Given a property source, creates a property source that notifies at subscription time, regardless what the original source does.
/// </summary>
/// <typeparam name="T">The type of the property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<T> Eager<T>(this IPropertySource<T> source)
{
if (source == null) throw new ArgumentNullException("source");
return PropertySource.Create(
action =>
{
action();
return source.RawSubscribe(action);
},
() => source.Value
);
}
/// <summary>
/// Given a property source, creates a property source that does not notify at subscription time, regardless what the original source does.
/// </summary>
/// <typeparam name="T">The type of the property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<T> Lazy<T>(this IPropertySource<T> source)
{
if (source == null) throw new ArgumentNullException("source");
return PropertySource.Create(
action =>
{
Action send = () => { };
var subscription = source.RawSubscribe(() => send());
send = action;
return subscription;
},
() => source.Value
);
}
/// <summary>
/// Given a property source and a selector that returns a property source, calls the selector passing the source's value,
/// at subscription time and every time the original source changes, and creates a property source whose value is the value
/// of the property source returned by the selector. This is the bind operator of the property source monad.
/// </summary>
/// <typeparam name="TSource">The type of the original property soruce.</typeparam>
/// <typeparam name="TResult">The type of the created property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <param name="selector">A function that receives the value of the original source at subscription time and every time it changes, and returns a property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<TResult> SelectMany<TSource, TResult>(this IPropertySource<TSource> source, Func<TSource, IPropertySource<TResult>> selector)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
return PropertySource.Create(
observer =>
{
IDisposable rightSubscription = Disposable.Empty;
Func<TSource, IPropertySource<TResult>> reattachRight = leftValue =>
{
rightSubscription.Dispose();
var rightSource = selector(leftValue);
rightSubscription = rightSource.Lazy().RawSubscribe(observer);
return rightSource;
};
IDisposable leftSubscription = source.Lazy().RawSubscribe(() =>
{
reattachRight(source.Value);
observer();
});
reattachRight(source.Value);
return Disposable.Create(() =>
{
leftSubscription.Dispose();
rightSubscription.Dispose();
});
},
() => selector(source.Value).Value
);
}
/// <summary>
/// Creates a new property source given an original property source and a selector that maps each value taken by the original source property into a new value.
/// </summary>
/// <typeparam name="TSource">The type of the original source.</typeparam>
/// <typeparam name="TResult">The type of the created property source.</typeparam>
/// <param name="source">The original property source.</param>
/// <param name="selector">The selector that maps the original source values into new values.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<TResult> Select<TSource, TResult>(this IPropertySource<TSource> source, Func<TSource, TResult> selector)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
return source.SelectMany(value => PropertySource.Return(selector(value)));
}
/// <summary>
/// Casts the values taken by a property source to the specified type.
/// </summary>
/// <typeparam name="T">The type to case the original source values to.</typeparam>
/// <param name="source">The orignal property source.</param>
/// <returns>A property source whose value is the result of casting the original property source.</returns>
public static IPropertySource<T> Cast<T>(this IPropertySource<object> source)
{
if (source == null) throw new ArgumentNullException("source");
return source.Select(obj => (T)obj);
}
/// <summary>
/// Casts the values taken by a property source to the specified type, or set the resulting property source's value to null if casting isn't possible.
/// </summary>
/// <typeparam name="T">The type to case the original source values to.</typeparam>
/// <param name="source">The orignal property source.</param>
/// <returns>A property source whose value is the result of casting the original property source, or null if casting isn't possible.</returns>
public static IPropertySource<T> As<T>(this IPropertySource<object> source)
where T : class
{
if (source == null) throw new ArgumentNullException("source");
return source.Select(obj => obj as T);
}
/// <summary>
/// Merges two property sources into a single one using a selector that takes values from both sources and returns another value.
/// </summary>
/// <typeparam name="TLeft">The type of the left property source.</typeparam>
/// <typeparam name="TRight">The type fo the right property source.</typeparam>
/// <typeparam name="TResult">The type of the selector's result and the created property source.</typeparam>
/// <param name="left">The left property source.</param>
/// <param name="right">The right property source.</param>
/// <param name="selector">A function that is called every time the left or right property sources change and returns another value.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<TResult> Merge<TLeft, TRight, TResult>(this IPropertySource<TLeft> left, IPropertySource<TRight> right, Func<TLeft, TRight, TResult> selector)
{
if (left == null) throw new ArgumentNullException("left");
if (right == null) throw new ArgumentNullException("right");
if (selector == null) throw new ArgumentNullException("selector");
return left.SelectMany(leftValue =>
right.Select(rightValue => selector(leftValue, rightValue))
);
}
/// <summary>
/// Merges three property sources into a single one using a selector that takes values from all three sources and returns another value.
/// </summary>
/// <typeparam name="TLeft">The type of the left property source.</typeparam>
/// <typeparam name="TMiddle">The type fo the middle property source.</typeparam>
/// <typeparam name="TRight">The type fo the right property source.</typeparam>
/// <typeparam name="TResult">The type of the selector's result and the created property source.</typeparam>
/// <param name="left">The left property source.</param>
/// <param name="middle">The middle property source.</param>
/// <param name="right">The right property source.</param>
/// <param name="selector">A function that is called every time the left, middle or right property sources change and returns another value.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<TResult> Merge<TLeft, TMiddle, TRight, TResult>(this IPropertySource<TLeft> left, IPropertySource<TMiddle> middle, IPropertySource<TRight> right, Func<TLeft, TMiddle, TRight, TResult> selector)
{
if (left == null) throw new ArgumentNullException("left");
if (middle == null) throw new ArgumentNullException("middle");
if (right == null) throw new ArgumentNullException("right");
if (selector == null) throw new ArgumentNullException("selector");
return left.SelectMany(leftValue =>
middle.SelectMany(middleValue =>
right.Select(rightValue => selector(leftValue, middleValue, rightValue))
)
);
}
/// <summary>
/// Creates a property source whose value is the result of applying the "and" logical operator to the left and right property sources.
/// </summary>
/// <param name="left">The left property source.</param>
/// <param name="right">The right property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<bool> And(this IPropertySource<bool> left, IPropertySource<bool> right)
{
if (left == null) throw new ArgumentNullException("left");
if (right == null) throw new ArgumentNullException("right");
return left.Merge(right, (l, r) => l && r).Distinct();
}
/// <summary>
/// Creates a property source whose value is the result of applying the "or" logical operator to the left and right property sources.
/// </summary>
/// <param name="left">The left property source.</param>
/// <param name="right">The right property source.</param>
/// <returns>The created property source.</returns>
public static IPropertySource<bool> Or(this IPropertySource<bool> left, IPropertySource<bool> right)
{
if (left == null) throw new ArgumentNullException("left");
if (right == null) throw new ArgumentNullException("right");
return left.Merge(right, (l, r) => l || r).Distinct();
}
}
}