Repository navigation
Expand file tree
/
Copy pathDynamicDataEx.cs
More file actions
315 lines (278 loc) · 14.6 KB
/
Copy pathDynamicDataEx.cs
File metadata and controls
315 lines (278 loc) · 14.6 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using DynamicData.Annotations;
using DynamicData.Cache.Internal;
using DynamicData.List.Internal;
using ReactiveUI.Legacy;
#pragma warning disable CS0618 // Using legacy code.
namespace DynamicData.ReactiveUI
{
/// <summary>
/// Integration methods between dynamic data and reactive list
/// </summary>
public static class DynamicDataEx
{
/// <summary>
/// Flattens a nested reactive list
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="manyselector">The manyselector.</param>
/// <param name="equalityComparer">Used when an item has been replaced to determine whether child items are the same as previous children</param>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TDestination>> TransformMany<TDestination, TSource>(this IObservable<IChangeSet<TSource>> source,
Func<TSource, IReadOnlyReactiveList<TDestination>> manyselector,
IEqualityComparer<TDestination> equalityComparer = null)
{
return new TransformMany<TSource, TDestination>(source, manyselector, equalityComparer, t => Observable.Defer(() =>
{
var subsequentChanges = manyselector(t).ToObservableChangeSet();
if (manyselector(t).Count > 0)
return subsequentChanges;
return Observable.Return(ChangeSet<TDestination>.Empty)
.Concat(subsequentChanges);
})).Run();
}
/// <summary>
/// Flattens a nested reactive list
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <param name="source">The source.</param>
/// <param name="manyselector">The manyselector.</param>
/// <param name="equalityComparer">Used when an item has been replaced to determine whether child items are the same as previous children</param>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TDestination>> TransformMany<TDestination, TSource>(this IObservable<IChangeSet<TSource>> source,
Func<TSource, ReactiveList<TDestination>> manyselector,
IEqualityComparer<TDestination> equalityComparer = null)
{
return new TransformMany<TSource, TDestination>(source, manyselector, equalityComparer, t => Observable.Defer(() =>
{
var subsequentChanges = manyselector(t).ToObservableChangeSet();
if (manyselector(t).Count > 0)
return subsequentChanges;
return Observable.Return(ChangeSet<TDestination>.Empty)
.Concat(subsequentChanges);
})).Run();
}
/// <summary>
/// Flattens a nested reactive list, using the key selector to ensure only unique items are added
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <typeparam name="TDestinationKey">The type of the destination key.</typeparam>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TSourceKey">The type of the source key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="manyselector">The manyselector.</param>
/// <param name="keySelector">The key selector which must be unique across all</param>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TDestination, TDestinationKey>> TransformMany<TDestination, TDestinationKey, TSource, TSourceKey>(
this IObservable<IChangeSet<TSource, TSourceKey>> source,
Func<TSource, ReactiveList<TDestination>> manyselector,
Func<TDestination, TDestinationKey> keySelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (manyselector == null) throw new ArgumentNullException(nameof(manyselector));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return new TransformMany<TDestination, TDestinationKey, TSource, TSourceKey>(source,
manyselector,
keySelector,
t => Observable.Defer(() =>
{
var subsequentChanges = manyselector(t).ToObservableChangeSet(keySelector);
if (manyselector(t).Count > 0)
return subsequentChanges;
return Observable.Return(ChangeSet<TDestination, TDestinationKey>.Empty)
.Concat(subsequentChanges);
})).Run();
}
/// <summary>
/// Flattens a nested reactive list, using the key selector to ensure only unique items are added
/// </summary>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <typeparam name="TDestinationKey">The type of the destination key.</typeparam>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TSourceKey">The type of the source key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="manyselector">The manyselector.</param>
/// <param name="keySelector">The key selector which must be unique across all</param>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TDestination, TDestinationKey>> TransformMany<TDestination, TDestinationKey, TSource, TSourceKey>(
this IObservable<IChangeSet<TSource, TSourceKey>> source,
Func<TSource, IReadOnlyReactiveList<TDestination>> manyselector,
Func<TDestination, TDestinationKey> keySelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (manyselector == null) throw new ArgumentNullException(nameof(manyselector));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
return new TransformMany<TDestination, TDestinationKey, TSource, TSourceKey>(source,
manyselector,
keySelector,
t => Observable.Defer(() =>
{
var subsequentChanges = manyselector(t).ToObservableChangeSet(keySelector);
if (manyselector(t).Count > 0)
return subsequentChanges;
return Observable.Return(ChangeSet<TDestination, TDestinationKey>.Empty)
.Concat(subsequentChanges);
})).Run();
}
/// <summary>
/// Binds the observable changeset to the target ReactiveList
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="targetCollection">The target collection.</param>
/// <param name="resetThreshold">The reset threshold.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">
/// source
/// or
/// targetCollection
/// </exception>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<T>> Bind<T>([NotNull] this IObservable<IChangeSet<T>> source,
[NotNull] ReactiveList<T> targetCollection, int resetThreshold = 25)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (targetCollection == null) throw new ArgumentNullException(nameof(targetCollection));
var adaptor = new ObservableListToReactiveListAdaptor<T>(targetCollection, resetThreshold);
return source.Adapt(adaptor);
}
/// <summary>
/// Populate and maintain the specified reactive list from the source observable changeset
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="resetThreshold">The reset threshold before a reset event on the target list is invoked</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// destination
/// or
/// target
/// </exception>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TObject, TKey>> Bind<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source, ReactiveList<TObject> target,
int resetThreshold = 25)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (target == null) throw new ArgumentNullException(nameof(target));
var adaptor = new ObservableCacheToReactiveListAdaptor<TObject, TKey>(target, resetThreshold);
return source.Bind(adaptor);
}
/// <summary>
/// Binds the results using the specified changeset adaptor
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="updater">The updater.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<IChangeSet<TObject, TKey>> Bind<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source,
IChangeSetAdaptor<TObject, TKey> updater)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (updater == null) throw new ArgumentNullException(nameof(updater));
return Observable.Create<IChangeSet<TObject, TKey>>
(observer =>
{
var locker = new object();
var published = source.Synchronize(locker).Publish();
var adaptor = published.Subscribe(updates =>
{
try
{
updater.Adapt(updates);
}
catch (Exception ex)
{
observer.OnError(ex);
}
},
observer.OnError, observer.OnCompleted);
var connected = published.Connect();
var subscriber = published.SubscribeSafe(observer);
return Disposable.Create(() =>
{
adaptor.Dispose();
subscriber.Dispose();
connected.Dispose();
});
}
);
}
/// <summary>
/// Populate and maintain the specified reactive list from the source observable changeset
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="target">The target.</param>
/// <param name="resetThreshold">The reset threshold before a reset event on the target list is invoked</param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// destination
/// or
/// target
/// </exception>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<ISortedChangeSet<TObject, TKey>> Bind<TObject, TKey>(
this IObservable<ISortedChangeSet<TObject, TKey>> source, ReactiveList<TObject> target,
int resetThreshold = 25)
{
if (target == null) throw new ArgumentNullException("target");
var adaptor = new SortedReactiveListAdaptor<TObject, TKey>(target, resetThreshold);
return source.Bind(adaptor);
}
/// <summary>
/// Binds the results using the specified sorted changeset adaptor
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="updater">The updater.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentNullException">source</exception>
[Obsolete("ReactiveList has been deprecated by the ReactiveUI team.")]
public static IObservable<ISortedChangeSet<TObject, TKey>> Bind<TObject, TKey>(
this IObservable<ISortedChangeSet<TObject, TKey>> source,
ISortedChangeSetAdaptor<TObject, TKey> updater)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (updater == null) throw new ArgumentNullException(nameof(updater));
return Observable.Create<ISortedChangeSet<TObject, TKey>>
(observer =>
{
var locker = new object();
var published = source.Synchronize(locker).Publish();
var adaptor = published.Subscribe(updates =>
{
try
{
updater.Adapt(updates);
}
catch (Exception ex)
{
observer.OnError(ex);
}
},observer.OnError, observer.OnCompleted);
var connected = published.Connect();
var subscriber = published.SubscribeSafe(observer);
return Disposable.Create(() =>
{
adaptor.Dispose();
subscriber.Dispose();
connected.Dispose();
});
}
);
}
}
}