Skip to content

Commit a815ac5

Browse files
Added BaseWeakEventManager
1 parent 61e5435 commit a815ac5

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

Src/AsyncAwaitBestPractices/WeakEventManager.cs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace AsyncAwaitBestPractices
1111
/// Weak event manager that allows for garbage collection when the EventHandler is still subscribed
1212
/// </summary>
1313
/// <typeparam name="TEventArgs">Event args type.</typeparam>
14-
public class WeakEventManager<TEventArgs> : WeakEventManager
14+
public class WeakEventManager<TEventArgs> : BaseWeakEventManager
1515
{
1616
/// <summary>
1717
/// Adds the event handler
@@ -57,11 +57,8 @@ public void RemoveEventHandler(EventHandler<TEventArgs> handler, [CallerMemberNa
5757
/// <summary>
5858
/// Weak event manager that allows for garbage collection when the EventHandler is still subscribed
5959
/// </summary>
60-
public class WeakEventManager
60+
public class WeakEventManager : BaseWeakEventManager
6161
{
62-
readonly Dictionary<string, List<Subscription>> _eventHandlers =
63-
new Dictionary<string, List<Subscription>>();
64-
6562
/// <summary>
6663
/// Adds the event handler
6764
/// </summary>
@@ -86,45 +83,6 @@ public void AddEventHandler(EventHandler handler, [CallerMemberName] string even
8683
/// <param name="eventName">Event name</param>
8784
public void HandleEvent(object sender, object eventArgs, string eventName) => HandleEvent(eventName, sender, eventArgs);
8885

89-
protected void HandleEvent(string eventName, object sender, object eventArgs)
90-
{
91-
var toRaise = new List<Tuple<object, MethodInfo>>();
92-
var toRemove = new List<Subscription>();
93-
94-
if (_eventHandlers.TryGetValue(eventName, out List<Subscription> target))
95-
{
96-
for (int i = 0; i < target.Count; i++)
97-
{
98-
Subscription subscription = target[i];
99-
bool isStatic = subscription.Subscriber is null;
100-
if (isStatic)
101-
{
102-
toRaise.Add(Tuple.Create<object, MethodInfo>(null, subscription.Handler));
103-
continue;
104-
}
105-
106-
object subscriber = subscription.Subscriber.Target;
107-
108-
if (subscriber is null)
109-
toRemove.Add(subscription);
110-
else
111-
toRaise.Add(Tuple.Create(subscriber, subscription.Handler));
112-
}
113-
114-
for (int i = 0; i < toRemove.Count; i++)
115-
{
116-
Subscription subscription = toRemove[i];
117-
target.Remove(subscription);
118-
}
119-
}
120-
121-
for (int i = 0; i < toRaise.Count; i++)
122-
{
123-
Tuple<object, MethodInfo> tuple = toRaise[i];
124-
tuple.Item2.Invoke(tuple.Item1, new[] { sender, eventArgs });
125-
}
126-
}
127-
12886
/// <summary>
12987
/// Removes the event handler.
13088
/// </summary>
@@ -140,6 +98,12 @@ public void RemoveEventHandler(EventHandler handler, [CallerMemberName] string e
14098

14199
RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
142100
}
101+
}
102+
103+
public abstract class BaseWeakEventManager
104+
{
105+
readonly Dictionary<string, List<Subscription>> _eventHandlers =
106+
new Dictionary<string, List<Subscription>>();
143107

144108
protected void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodInfo)
145109
{
@@ -180,6 +144,45 @@ protected void RemoveEventHandler(string eventName, object handlerTarget, Member
180144
}
181145
}
182146

147+
protected void HandleEvent(string eventName, object sender, object eventArgs)
148+
{
149+
var toRaise = new List<Tuple<object, MethodInfo>>();
150+
var toRemove = new List<Subscription>();
151+
152+
if (_eventHandlers.TryGetValue(eventName, out List<Subscription> target))
153+
{
154+
for (int i = 0; i < target.Count; i++)
155+
{
156+
Subscription subscription = target[i];
157+
bool isStatic = subscription.Subscriber is null;
158+
if (isStatic)
159+
{
160+
toRaise.Add(Tuple.Create<object, MethodInfo>(null, subscription.Handler));
161+
continue;
162+
}
163+
164+
object subscriber = subscription.Subscriber.Target;
165+
166+
if (subscriber is null)
167+
toRemove.Add(subscription);
168+
else
169+
toRaise.Add(Tuple.Create(subscriber, subscription.Handler));
170+
}
171+
172+
for (int i = 0; i < toRemove.Count; i++)
173+
{
174+
Subscription subscription = toRemove[i];
175+
target.Remove(subscription);
176+
}
177+
}
178+
179+
for (int i = 0; i < toRaise.Count; i++)
180+
{
181+
Tuple<object, MethodInfo> tuple = toRaise[i];
182+
tuple.Item2.Invoke(tuple.Item1, new[] { sender, eventArgs });
183+
}
184+
}
185+
183186
struct Subscription
184187
{
185188
public Subscription(WeakReference subscriber, MethodInfo handler)

0 commit comments

Comments
 (0)