Repository navigation
Expand file tree
/
Copy pathUnsafeObservationExamples.cs
More file actions
359 lines (285 loc) · 14.2 KB
/
Copy pathUnsafeObservationExamples.cs
File metadata and controls
359 lines (285 loc) · 14.2 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright (c) ReactiveUI and Contributors. All rights reserved.
// ReactiveUI and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Linq.Expressions;
using ReactiveUI.Binding.Documentation.Todo;
using ReactiveUI.Primitives;
using ReactiveUI.Primitives.Signals;
namespace ReactiveUI.Binding.Documentation.UnsafeOverloads;
/// <summary>
/// Demonstrates the <c>Unsafe</c> twins of the observation methods. Each property path here is held in a variable, as it
/// is when a user picks the columns of a grid, so the generator cannot read it and only the reflection-based overload
/// can serve the call.
/// </summary>
public static class UnsafeObservationExamples
{
/// <summary>The title of the first seeded item.</summary>
private const string OriginalTitle = "Renew car registration";
/// <summary>The title an example gives an item.</summary>
private const string RenamedTitle = "Renew car registration online";
/// <summary>The last title an example gives an item.</summary>
private const string FinalTitle = "Renew car registration by post";
/// <summary>The last notes an example gives an item.</summary>
private const string FinalNotes = "Bring nothing.";
/// <summary>The notes of the first seeded item.</summary>
private const string SeededNotes = "Bring the insurance certificate.";
/// <summary>The notes an example writes.</summary>
private const string ReplacementNotes = "Bring the insurance certificate and a photo.";
/// <summary>The announcement an example emits.</summary>
private const string SyncComplete = "Sync complete";
/// <summary>The urgent announcement an example emits.</summary>
private const string DiskFull = "Disk full";
/// <summary>Observes one property whose path is built from a name that is only known while the app runs.</summary>
public static void ObserveTitleChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
ParameterExpression row = Expression.Parameter(typeof(TodoItem), "row");
Expression<Func<TodoItem, string>> titleColumn = Expression.Lambda<Func<TodoItem, string>>(Expression.Property(row, nameof(TodoItem.Title)), row);
List<string> titles = [];
using (item.WhenChangedUnsafe(titleColumn).Subscribe(titles.Add))
{
item.Title = RenamedTitle;
}
Console.WriteLine(string.Join(", ", titles));
// Output:
// Renew car registration, Renew car registration online
}
/// <summary>Observes two properties; each change delivers both values.</summary>
public static void ObserveTwoColumnsChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, bool>> doneColumn = x => x.IsDone;
using (item.WhenChangedUnsafe(titleColumn, doneColumn).Subscribe(static values => Console.WriteLine($"{values.Property1} {values.Property2}")))
{
item.IsDone = true;
}
// Output:
// Renew car registration False
// Renew car registration True
}
/// <summary>Observes two properties and combines them with a selector.</summary>
public static void ObserveTwoColumnsWithSelector()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, bool>> doneColumn = x => x.IsDone;
List<string> lines = [];
using (item.WhenChangedUnsafe(titleColumn, doneColumn, static (title, done) => done ? $"[x] {title}" : $"[ ] {title}").Subscribe(lines.Add))
{
item.IsDone = true;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// [ ] Renew car registration, [x] Renew car registration
}
/// <summary>Observes three properties; each change delivers all three values.</summary>
public static void ObserveThreeColumnsChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle, Notes = SeededNotes };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, string>> notesColumn = x => x.Notes;
Expression<Func<TodoItem, bool>> doneColumn = x => x.IsDone;
using (item.WhenChangedUnsafe(titleColumn, notesColumn, doneColumn).Subscribe(static values => Console.WriteLine($"{values.Property1} {values.Property2} {values.Property3}")))
{
item.IsDone = true;
}
// Output:
// Renew car registration Bring the insurance certificate. False
// Renew car registration Bring the insurance certificate. True
}
/// <summary>Observes three properties and combines them with a selector.</summary>
public static void ObserveThreeColumnsWithSelector()
{
TodoItem item = new TodoItem { Title = OriginalTitle, Notes = SeededNotes };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, string>> notesColumn = x => x.Notes;
Expression<Func<TodoItem, bool>> doneColumn = x => x.IsDone;
List<string> lines = [];
using (item.WhenChangedUnsafe(titleColumn, notesColumn, doneColumn, static (title, notes, done) => $"{title} | {notes} | {done}").Subscribe(lines.Add))
{
item.Notes = ReplacementNotes;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// Renew car registration | Bring the insurance certificate. | False, Renew car registration | Bring the insurance certificate and a photo. | False
}
/// <summary>Observes the value a property held before it changed.</summary>
public static void ObserveTitleBeforeChange()
{
EditableTodoItem item = new EditableTodoItem { Title = OriginalTitle };
Expression<Func<EditableTodoItem, string>> titleColumn = x => x.Title;
List<string> titles = [];
using (item.WhenChangingUnsafe(titleColumn).Subscribe(titles.Add))
{
item.Title = RenamedTitle;
item.Title = FinalTitle;
}
Console.WriteLine(string.Join(", ", titles));
// Output:
// Renew car registration, Renew car registration online
}
/// <summary>Observes two properties before they change.</summary>
public static void ObserveTwoColumnsBeforeChange()
{
EditableTodoItem item = new EditableTodoItem { Title = OriginalTitle, Notes = SeededNotes };
Expression<Func<EditableTodoItem, string>> titleColumn = x => x.Title;
Expression<Func<EditableTodoItem, string>> notesColumn = x => x.Notes;
using (item.WhenChangingUnsafe(titleColumn, notesColumn).Subscribe(static values => Console.WriteLine($"{values.Property1} {values.Property2}")))
{
item.Notes = ReplacementNotes;
item.Notes = FinalNotes;
}
// Output:
// Renew car registration Bring the insurance certificate.
// Renew car registration Bring the insurance certificate and a photo.
}
/// <summary>Observes two properties before they change and combines them with a selector.</summary>
public static void ObserveTwoColumnsBeforeChangeWithSelector()
{
EditableTodoItem item = new EditableTodoItem { Title = OriginalTitle, Notes = SeededNotes };
Expression<Func<EditableTodoItem, string>> titleColumn = x => x.Title;
Expression<Func<EditableTodoItem, string>> notesColumn = x => x.Notes;
List<string> lines = [];
using (item.WhenChangingUnsafe(titleColumn, notesColumn, static (title, notes) => $"{title}: {notes}").Subscribe(lines.Add))
{
item.Title = RenamedTitle;
item.Title = FinalTitle;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// Renew car registration: Bring the insurance certificate., Renew car registration online: Bring the insurance certificate.
}
/// <summary>Observes one property with <c>WhenAnyValueUnsafe</c>.</summary>
public static void ObserveAnyValueChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
List<string> titles = [];
using (item.WhenAnyValueUnsafe(titleColumn).Subscribe(titles.Add))
{
item.Title = RenamedTitle;
}
Console.WriteLine(string.Join(", ", titles));
// Output:
// Renew car registration, Renew car registration online
}
/// <summary>Observes one property with <c>WhenAnyValueUnsafe</c> and maps each value with a selector.</summary>
public static void ObserveAnyValueWithSelector()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
List<int> lengths = [];
using (item.WhenAnyValueUnsafe(titleColumn, static title => title.Length).Subscribe(lengths.Add))
{
item.Title = RenamedTitle;
}
Console.WriteLine(string.Join(", ", lengths));
// Output:
// 22, 29
}
/// <summary>Observes two properties with <c>WhenAnyValueUnsafe</c>.</summary>
public static void ObserveTwoAnyValuesChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, TodoPriority>> priorityColumn = x => x.Priority;
using (item.WhenAnyValueUnsafe(titleColumn, priorityColumn).Subscribe(static values => Console.WriteLine($"{values.Property1} {values.Property2}")))
{
item.Priority = TodoPriority.High;
}
// Output:
// Renew car registration Normal
// Renew car registration High
}
/// <summary>Observes two properties with <c>WhenAnyValueUnsafe</c> and combines them with a selector.</summary>
public static void ObserveTwoAnyValuesWithSelector()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, TodoPriority>> priorityColumn = x => x.Priority;
List<string> lines = [];
using (item.WhenAnyValueUnsafe(titleColumn, priorityColumn, static (title, priority) => $"{title} [{priority}]").Subscribe(lines.Add))
{
item.Priority = TodoPriority.High;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// Renew car registration [Normal], Renew car registration [High]
}
/// <summary>Observes one property with <c>WhenAnyUnsafe</c>; the selector receives the change, not only its value.</summary>
public static void ObserveAnyChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
List<string> lines = [];
using (item.WhenAnyUnsafe(titleColumn, static change => $"{change.Sender.Id}: {change.Value}").Subscribe(lines.Add))
{
item.Title = RenamedTitle;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// 0: Renew car registration, 0: Renew car registration online
}
/// <summary>Observes two properties with <c>WhenAnyUnsafe</c>.</summary>
public static void ObserveTwoAnyChosenAtRunTime()
{
TodoItem item = new TodoItem { Title = OriginalTitle };
Expression<Func<TodoItem, string>> titleColumn = x => x.Title;
Expression<Func<TodoItem, bool>> doneColumn = x => x.IsDone;
List<string> lines = [];
using (item.WhenAnyUnsafe(titleColumn, doneColumn, static (title, done) => $"{title.Value} {done.Value}").Subscribe(lines.Add))
{
item.IsDone = true;
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// Renew car registration False, Renew car registration True
}
/// <summary>Follows the announcement stream a property holds, and switches when the property is replaced.</summary>
public static void ObserveAnnouncementStreamChosenAtRunTime()
{
TodoAnnouncer announcer = new();
Expression<Func<TodoAnnouncer, IObservable<string>?>> latestColumn = x => x.Latest;
List<string> messages = [];
using (announcer.WhenAnyObservableUnsafe(latestColumn).Subscribe(messages.Add))
{
announcer.Latest = Signal.Return(SyncComplete);
}
Console.WriteLine(string.Join(", ", messages));
// Output:
// Sync complete
}
/// <summary>Merges the announcement streams of two properties.</summary>
public static void MergeAnnouncementStreams()
{
TodoAnnouncer announcer = new();
Expression<Func<TodoAnnouncer, IObservable<string>?>> latestColumn = x => x.Latest;
Expression<Func<TodoAnnouncer, IObservable<string>?>> urgentColumn = x => x.Urgent;
List<string> messages = [];
using (announcer.WhenAnyObservableUnsafe(latestColumn, urgentColumn).Subscribe(messages.Add))
{
announcer.Latest = Signal.Return(SyncComplete);
announcer.Urgent = Signal.Return(DiskFull);
}
Console.WriteLine(string.Join(", ", messages));
// Output:
// Sync complete, Disk full
}
/// <summary>Combines the latest announcement of two properties with a selector.</summary>
public static void CombineAnnouncementStreams()
{
TodoAnnouncer announcer = new();
Expression<Func<TodoAnnouncer, IObservable<string>?>> latestColumn = x => x.Latest;
Expression<Func<TodoAnnouncer, IObservable<string>?>> urgentColumn = x => x.Urgent;
List<string> lines = [];
using (announcer.WhenAnyObservableUnsafe(latestColumn, urgentColumn, static (latest, urgent) => $"{latest} / {urgent}").Subscribe(lines.Add))
{
announcer.Latest = Signal.Return(SyncComplete);
announcer.Urgent = Signal.Return(DiskFull);
}
Console.WriteLine(string.Join(", ", lines));
// Output:
// Sync complete / Disk full
}
}