-
Notifications
You must be signed in to change notification settings - Fork 11
/
ObservableValue.cs
286 lines (253 loc) · 9.56 KB
/
ObservableValue.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
// <copyright file="ObservableValue.cs" company="Michel Weststrate, Jan-Willem Spuij">
// Copyright 2019 Michel Weststrate, Jan-Willem Spuij
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
namespace Cortex.Net.Types
{
using System;
using System.Collections.Generic;
using System.Text;
using Cortex.Net.Core;
using Cortex.Net.Spy;
/// <summary>
/// Observable value. Can be changed and can be observed.
/// </summary>
/// <typeparam name="T">The type of the Observable value.</typeparam>
public class ObservableValue<T> :
Atom, IObservableValue<T>
{
/// <summary>
/// A set of event handlers for the change event.
/// </summary>
private readonly HashSet<EventHandler<ValueChangeEventArgs<T>>> changeEventHandlers = new HashSet<EventHandler<ValueChangeEventArgs<T>>>();
/// <summary>
/// A set of event handlers for the changed event.
/// </summary>
private readonly HashSet<EventHandler<ValueChangedEventArgs<T>>> changedEventHandlers = new HashSet<EventHandler<ValueChangedEventArgs<T>>>();
/// <summary>
/// An enhancer that possibly makes new values observable as well.
/// </summary>
private readonly IEnhancer enhancer;
/// <summary>
/// The value of the observable.
/// </summary>
private T value;
/// <summary>
/// Initializes a new instance of the <see cref="ObservableValue{T}"/> class.
/// </summary>
/// <param name="sharedState">The shared state this Observable will be attached to.</param>
/// <param name="name">The name of this observable value.</param>
/// <param name="enhancer">The enhancer to use on the type.</param>
/// <param name="value">The initial value of the Observable.</param>
public ObservableValue(ISharedState sharedState, string name, IEnhancer enhancer, T value = default)
: base(sharedState, name)
{
this.enhancer = enhancer ?? throw new ArgumentNullException(nameof(enhancer));
this.value = this.enhancer.Enhance(value, default, this.Name);
this.SharedState.OnSpy(this, new ObservableValueCreateSpyEventArgs() { Name = this.Name, NewValue = value });
}
/// <summary>
/// Event that fires before the value will change.
/// </summary>
public event EventHandler<ValueChangeEventArgs<T>> Change
{
add
{
this.changeEventHandlers.Add(value);
}
remove
{
this.changeEventHandlers.Remove(value);
}
}
/// <summary>
/// Event that fires after the value has changed.
/// </summary>
public event EventHandler<ValueChangedEventArgs<T>> Changed
{
add
{
this.changedEventHandlers.Add(value);
}
remove
{
this.changedEventHandlers.Remove(value);
}
}
/// <summary>
/// Gets or sets the underlying value.
/// </summary>
public T Value
{
get
{
this.ReportObserved();
return this.value;
}
set
{
T oldValue = this.value;
(T newValue, bool changed) = this.PrepareNewValue(value);
if (changed)
{
this.SharedState.OnSpy(this, new ObservableValueStartSpyEventArgs()
{
Name = this.Name,
OldValue = oldValue,
NewValue = newValue,
StartTime = DateTime.UtcNow,
});
this.SetNewValue(newValue);
this.SharedState.OnSpy(this, new ObservableValueEndSpyEventArgs()
{
Name = this.Name,
EndTime = DateTime.UtcNow,
});
}
}
}
/// <summary>
/// Gets or sets the underlying value.
/// </summary>
object IValue.Value
{
get => this.Value;
set
{
if (value is null)
{
this.Value = default;
}
else
{
this.Value = (T)value;
}
}
}
/// <summary>
/// Gets the untracked value of this observable.
/// </summary>
internal T UntrackedValue
{
get
{
return this.value;
}
}
/// <summary>
/// Registers the secified event handler, and optionally fires it first.
/// </summary>
/// <param name="changedEventHandler">The event handler to register.</param>
/// <param name="fireImmediately">Whether to fire the event handler immediately.</param>
public void Observe(EventHandler<ValueChangedEventArgs<T>> changedEventHandler, bool fireImmediately)
{
if (changedEventHandler is null)
{
throw new ArgumentNullException(nameof(changedEventHandler));
}
if (fireImmediately)
{
changedEventHandler(this, new ValueChangedEventArgs<T>()
{
Context = this,
OldValue = default,
NewValue = this.Value,
});
}
this.Changed += changedEventHandler;
}
/// <summary>
/// Prepares setting of a new Value.
/// </summary>
/// <param name="newValue">The new value.</param>
/// <returns>The value plus a boolean indicating whether the observable value has changed.</returns>
internal (T, bool hasChanged) PrepareNewValue(T newValue)
{
this.CheckIfStateModificationsAreAllowed();
var changeEventArgs = new ValueChangeEventArgs<T>()
{
Cancel = false,
Context = this,
NewValue = newValue,
OldValue = this.value,
};
this.InterceptChange(changeEventArgs);
if (changeEventArgs.Cancel || !changeEventArgs.Changed)
{
return (this.value, true);
}
newValue = this.enhancer.Enhance(changeEventArgs.NewValue, this.value, this.Name);
return (newValue, !Equals(this.value, newValue));
}
/// <summary>
/// Sets a new value.
/// </summary>
/// <param name="newValue">The new value to set.</param>
internal void SetNewValue(T newValue)
{
var oldValue = this.value;
this.value = newValue;
this.ReportChanged();
var eventArgs = new ValueChangedEventArgs<T>()
{
Context = this,
OldValue = oldValue,
NewValue = newValue,
};
this.NotifyListeners(eventArgs);
}
/// <summary>
/// Fires a Change event that can be intercepted and or canceled.
/// </summary>
/// <param name="changeEventArgs">The change event args.</param>
private void InterceptChange(ValueChangeEventArgs<T> changeEventArgs)
{
var previousDerivation = this.SharedState.StartUntracked();
try
{
foreach (var handler in this.changeEventHandlers)
{
handler(this, changeEventArgs);
if (changeEventArgs.Cancel)
{
break;
}
}
}
finally
{
this.SharedState.EndTracking(previousDerivation);
}
}
/// <summary>
/// Notifies Listeners on the <see cref="Changed"/> event.
/// </summary>
/// <param name="eventArgs">The event arguments.</param>
private void NotifyListeners(ValueChangedEventArgs<T> eventArgs)
{
var previousDerivation = this.SharedState.StartUntracked();
try
{
foreach (var handler in this.changedEventHandlers)
{
handler(this, eventArgs);
}
}
finally
{
this.SharedState.EndTracking(previousDerivation);
}
}
}
}