-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChangeDetector.cs
239 lines (198 loc) · 7.53 KB
/
ChangeDetector.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Reflection;
namespace ChangeDetection
{
public class ChangeDetector : IDisposable
{
private readonly Dictionary<object, int> _trackedObjects = new Dictionary<object, int>();
private readonly Dictionary<object, Dictionary<string, object>> _propertyChangedValues = new Dictionary<object, Dictionary<string, object>>();
internal int TrackedObjects => _trackedObjects.Count;
private readonly Action _stateChanged;
public static ChangeDetector Create(object obj, Action stateChanged)
{
return new ChangeDetector(obj, stateChanged);
}
private ChangeDetector(object obj, Action stateChanged)
{
_stateChanged = stateChanged ?? throw new ArgumentNullException(nameof(stateChanged));
AttachChangeHandlers(obj);
}
protected internal bool AttachChangeHandlers(object obj) => AttachChangeHandlersInternal(obj);
private bool AttachChangeHandlersInternal(object obj)
{
if (obj == null)
{
return false;
}
if (_trackedObjects.TryGetValue(obj, out var refCount))
{
refCount++;
_trackedObjects[obj] = refCount;
return false;
}
if (obj is INotifyPropertyChanged notifyPropertyChanged)
{
_trackedObjects[obj] = 1;
foreach (var property in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (TryGetPropertyValue(obj, property, out var value))
{
if (!_propertyChangedValues.TryGetValue(obj, out var properties))
{
properties = _propertyChangedValues[obj] = new Dictionary<string, object>();
}
properties[property.Name] = value;
AttachChangeHandlersInternal(value);
}
}
notifyPropertyChanged.PropertyChanged += OnPropertyChanged;
}
if (obj is INotifyCollectionChanged notifyCollectionChanged)
{
_trackedObjects[obj] = 1;
if (obj is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
AttachChangeHandlersInternal(item);
}
}
notifyCollectionChanged.CollectionChanged += OnCollectionChanged;
}
return _trackedObjects.ContainsKey(obj);
}
protected internal bool DetachChangeHandlers(object obj) => DetachChangeHandlersInternal(obj);
protected bool DetachChangeHandlersInternal(object obj)
{
if (obj == null)
{
return false;
}
bool tracked = false, finished = false;
if (_trackedObjects.TryGetValue(obj, out var refCount))
{
tracked = true;
refCount--;
if (refCount == 0)
{
_trackedObjects.Remove(obj);
finished = true;
}
else
{
_trackedObjects[obj] = refCount;
}
}
if (finished)
{
if (obj is INotifyPropertyChanged notifyPropertyChanged)
{
notifyPropertyChanged.PropertyChanged -= OnPropertyChanged;
if (_propertyChangedValues.TryGetValue(obj, out var properties))
{
foreach (var property in properties)
{
DetachChangeHandlersInternal(property.Value);
}
properties.Clear();
_propertyChangedValues.Remove(obj);
}
}
if (obj is INotifyCollectionChanged notifyCollectionChanged)
{
notifyCollectionChanged.CollectionChanged -= OnCollectionChanged;
if (obj is IEnumerable enumerable)
{
foreach (var item in enumerable)
{
DetachChangeHandlersInternal(item);
}
}
}
}
return tracked;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
var property = sender.GetType().GetProperty(args.PropertyName, BindingFlags.Public | BindingFlags.Instance);
if (TryGetPropertyValue(sender, property, out var value))
{
if (_propertyChangedValues.TryGetValue(sender, out var properties) && properties.TryGetValue(args.PropertyName, out var oldValue))
{
if (Equals(value, oldValue))
{
return; // value has not changed
}
DetachChangeHandlers(oldValue);
}
else
{
properties = _propertyChangedValues[sender] = new Dictionary<string, object>();
}
AttachChangeHandlers(value);
properties[args.PropertyName] = value;
}
_stateChanged();
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (args.Action == NotifyCollectionChangedAction.Reset)
{
// not supported...
}
if (args.NewItems != null)
{
foreach (var newItem in args.NewItems)
{
AttachChangeHandlers(newItem);
}
}
if (args.OldItems != null)
{
foreach (var oldItem in args.OldItems)
{
DetachChangeHandlers(oldItem);
}
}
_stateChanged();
}
private static bool TryGetPropertyValue(object sender, PropertyInfo property, out object value)
{
if (property != null && property.CanRead && property.GetIndexParameters().Length == 0)
{
try
{
value = property.GetValue(sender);
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to get property value: {sender.GetType().FullName} => {property.Name}");
Console.WriteLine(ex.StackTrace);
}
}
value = null;
return false;
}
public virtual void Dispose()
{
foreach (var obj in new List<object>(_trackedObjects.Keys))
{
DetachChangeHandlers(obj);
}
foreach (var properties in _propertyChangedValues)
{
foreach (var property in properties.Value)
{
DetachChangeHandlers(property.Value);
}
properties.Value.Clear();
}
_propertyChangedValues.Clear();
}
}
}