Repository navigation
Expand file tree
/
Copy pathObservedChangeExamples.cs
More file actions
167 lines (129 loc) · 6.26 KB
/
Copy pathObservedChangeExamples.cs
File metadata and controls
167 lines (129 loc) · 6.26 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
// Copyright (c) 2019-2026 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.GitHub;
using ReactiveUI.Binding.Documentation.Todo;
using ReactiveUI.Binding.ObservableForProperty;
using ReactiveUI.Primitives;
namespace ReactiveUI.Binding.Documentation.Observing;
/// <summary>
/// Shows <see cref="IObservedChange{TSender, TValue}"/>, the value that <c>WhenAny</c> hands to its selector: the object
/// that changed, the property expression and the value, together with the helpers that read them.
/// </summary>
public static class ObservedChangeExamples
{
/// <summary>The title of the review task.</summary>
private const string ReviewPrTitle = "Review PR";
/// <summary>The title the review task is renamed to.</summary>
private const string RenamedTitle = "Review PR 204";
/// <summary>The number of the checkout issue.</summary>
private const int CheckoutNumber = 101;
/// <summary>The title of the checkout issue.</summary>
private const string CheckoutTitle = "Checkout button unresponsive on Safari";
/// <summary>Reads the sender and the value of each change to a to-do title.</summary>
public static void ReadSenderAndValueOfEachChange()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
using IDisposable subscription = item.WhenAny(static x => x.Title, static change => change)
.Subscribe(static change => Console.WriteLine($"Item {change.Sender.Id} is now '{change.Value}'"));
item.Title = RenamedTitle;
// Output:
// Item 1 is now 'Review PR'
// Item 1 is now 'Review PR 204'
}
/// <summary>Reads the expression of a change; an observation the generator wrote has no expression tree to report.</summary>
public static void ReadExpressionOfAGeneratedChange()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
using IDisposable subscription = item.WhenAny(static x => x.Title, static change => change)
.Subscribe(static change => Console.WriteLine(change.Expression is null));
// Output:
// True
}
/// <summary>Creates a change by hand with its expression, to describe a value that was read from an object.</summary>
public static void CreateAChange()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
Expression<Func<TodoItem, string>> expression = x => x.Title;
ObservedChange<TodoItem, string> change = new ObservedChange<TodoItem, string>(item, expression.Body, item.Title);
Console.WriteLine(change.Sender.Title);
Console.WriteLine(change.Expression);
Console.WriteLine(change.Value);
// Output:
// Review PR
// x.Title
// Review PR
}
/// <summary>Creates a change without an expression, as an observation that needs no expression tree does.</summary>
public static void CreateAChangeWithoutAnExpression()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
ObservedChange<TodoItem, string> change = new ObservedChange<TodoItem, string>(item, null, item.Title);
Console.WriteLine(change.Sender.Title);
Console.WriteLine(change.Value);
Console.WriteLine(change.Expression is null);
// Output:
// Review PR
// Review PR
// True
}
/// <summary>Names the property a change describes, including every step of a path.</summary>
public static void NameThePropertyOfAChange()
{
Issue issue = new Issue { Number = CheckoutNumber, Title = CheckoutTitle, Assignee = new User { Login = "priya-nair" } };
Expression<Func<Issue, string>> title = x => x.Title;
Expression<Func<Issue, string>> assigneeLogin = x => x.Assignee!.Login;
ObservedChange<Issue, string> titleChange = new(issue, title.Body, issue.Title);
ObservedChange<Issue, string> loginChange = new(issue, assigneeLogin.Body, issue.Assignee!.Login);
Console.WriteLine(titleChange.GetPropertyName());
Console.WriteLine(loginChange.GetPropertyName());
// Output:
// Title
// Assignee.Login
}
/// <summary>Reads the value of a change; a change that carries the default value reads it from the sender again.</summary>
public static void ReadTheValueOfAChange()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
Expression<Func<TodoItem, string>> expression = x => x.Title;
ObservedChange<TodoItem, string> carried = new(item, expression.Body, RenamedTitle);
ObservedChange<TodoItem, string> empty = new(item, expression.Body, default!);
Console.WriteLine(carried.GetValue());
Console.WriteLine(empty.GetValue());
// Output:
// Review PR 204
// Review PR
}
/// <summary>Reads the value of a path that breaks halfway: <c>GetValue</c> reports it and <c>GetValueOrDefault</c> returns null.</summary>
public static void ReadTheValueOfABrokenPath()
{
Issue issue = new Issue { Number = CheckoutNumber, Title = CheckoutTitle, Assignee = null };
Expression<Func<Issue, string>> assigneeLogin = x => x.Assignee!.Login;
ObservedChange<Issue, string> change = new(issue, assigneeLogin.Body, default!);
Console.WriteLine(change.GetValueOrDefault() is null);
try
{
Console.WriteLine(change.GetValue());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
}
// Output:
// True
// One of the properties in the expression 'Assignee.Login' was null
}
/// <summary>Turns a stream of changes back into a stream of values.</summary>
public static void ProjectChangesToValues()
{
TodoItem item = new TodoItem { Id = 1, Title = ReviewPrTitle };
using IDisposable subscription = item.WhenAny(static x => x.Title, static change => change)
.Value()
.Subscribe(Console.WriteLine);
item.Title = RenamedTitle;
// Output:
// Review PR
// Review PR 204
}
}