/
WeakEventManager.cs
204 lines (185 loc) · 8.54 KB
/
WeakEventManager.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
using System;
using System.ComponentModel;
namespace MToolkit
{
/// <summary>
/// Static Class that holds the extension methods to handle events using weak references and actions.
/// This way we don't need to worry about unregister the event handler meaning that.
/// </summary>
public static class WeakEventManager
{
public class WeakEventSubscription<TDelegate> : IDisposable
where TDelegate : class
{
#region constructors...
public WeakEventSubscription(Action<TDelegate> removeAction, TDelegate handler)
{
if (removeAction == null)
{
throw new ArgumentNullException(nameof(removeAction));
}
if (handler == null)
{
throw new ArgumentNullException(nameof(handler));
}
this.removeAction = removeAction;
this.handler = handler;
}
#endregion
#region fields...
private Action<TDelegate> removeAction;
private TDelegate handler;
#endregion
#region IDisposable implementation...
private bool disposed;
// Dispose() calls Dispose(true)
public void Dispose()
{
if (disposed)
{
return;
}
Dispose(true);
disposed = true;
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
removeAction(handler);
removeAction = null;
handler = null;
}
#endregion
}
/// <summary>
/// This overload handles any type of EventHandler
/// </summary>
/// <typeparam name="T">The type of the T.</typeparam>
/// <typeparam name="TDelegate">The type of the T delegate.</typeparam>
/// <typeparam name="TArgs">The type of the T args.</typeparam>
/// <param name="subscriber">The subscriber.</param>
/// <param name="converter">The converter.</param>
/// <param name="add">The add.</param>
/// <param name="remove">The remove.</param>
/// <param name="action">The action.</param>
public static WeakEventSubscription<TDelegate> WeakSubscribe<T, TDelegate, TArgs>
(this T subscriber,
Func<EventHandler<TArgs>, TDelegate> converter,
Action<TDelegate> add,
Action<TDelegate> remove,
Action<T, TArgs> action)
where TArgs : EventArgs
where TDelegate : class
where T : class
{
var subsWeakRef = new WeakReference(subscriber);
TDelegate[] handler =
{
null
};
handler[0] = converter((s, e) =>
{
var subsStrongRef = subsWeakRef.Target as T;
if (subsStrongRef != null)
{
action(subsStrongRef, e);
}
else
{
if (handler[0] != null)
{
try
{
remove(handler[0]);
}
catch (Exception)
{
}
}
handler[0] = null;
}
});
add(handler[0]);
return new WeakEventSubscription<TDelegate>(remove, handler[0]);
}
/// <summary>
/// this overload is simplified for generic EventHandlers
/// </summary>
/// <typeparam name="T">The type of the T.</typeparam>
/// <typeparam name="TArgs">The type of the T args.</typeparam>
/// <param name="subscriber">The subscriber.</param>
/// <param name="add">The add.</param>
/// <param name="remove">The remove.</param>
/// <param name="action">The action.</param>
/// var subscription = this.WeakSubscribe(h => viewModel.PropertyChanged += h,
/// h => viewModel.PropertyChanged -= h,
/// (s, e) => s.HandleEvent(e));
public static WeakEventSubscription<EventHandler<TArgs>> WeakSubscribe<T, TArgs>(this T subscriber,
Action<EventHandler<TArgs>> add,
Action<EventHandler<TArgs>>
remove,
Action<T, TArgs> action)
where TArgs : EventArgs
where T : class
{
return WeakSubscribe(subscriber,
h => h,
add,
remove,
action);
}
/// <summary>
/// this overload is simplified for EventHandlers.
/// </summary>
/// <typeparam name="T">The type of the T.</typeparam>
/// <param name="subscriber">The subscriber.</param>
/// <param name="add">The add.</param>
/// <param name="remove">The remove.</param>
/// <param name="action">The action.</param>
/// <example>
/// var subscription = this.WeakSubscribe(h => viewModel.PropertyChanged += h,
/// h => viewModel.PropertyChanged -= h,
/// (s, e) => s.HandleEvent(e));
/// </example>
public static WeakEventSubscription<EventHandler> WeakSubscribe<T>(this T subscriber,
Action<EventHandler> add,
Action<EventHandler> remove,
Action<T, EventArgs> action)
where T : class
{
return WeakSubscribe(subscriber,
h => (o, e) => h(o, e),
//This is a workaround from Rx
add,
remove,
action);
}
/// <summary>
/// this overload is simplified for EventHandlers.
/// </summary>
/// <typeparam name="T">The type of the T.</typeparam>
/// <param name="subscriber">The subscriber.</param>
/// <param name="add">The add.</param>
/// <param name="remove">The remove.</param>
/// <param name="action">The action.</param>
/// var subscription = this.WeakSubscribe(h => viewModel.PropertyChanged += h,
/// h => viewModel.PropertyChanged -= h,
/// (s, e) => s.HandlePropertyChanged(e.PropertyName));
public static WeakEventSubscription<PropertyChangedEventHandler> WeakSubscribe<T>(this T subscriber,
Action<PropertyChangedEventHandler> add,
Action<PropertyChangedEventHandler> remove,
Action<T, PropertyChangedEventArgs> action)
where T : class
{
return WeakSubscribe(subscriber,
h => (o, e) => h(o, e),
//This is a workaround from Rx
add,
remove,
action);
}
}
}