-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
WeakEventManager.cs
148 lines (120 loc) · 4.21 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
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using static System.String;
namespace Microsoft.Maui.Controls
{
public class WeakEventManager
{
readonly Dictionary<string, List<Subscription>> _eventHandlers = new Dictionary<string, List<Subscription>>();
public void AddEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
}
public void AddEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
AddEventHandler(eventName, handler.Target, handler.GetMethodInfo());
}
public void HandleEvent(object sender, object args, string eventName)
{
var toRaise = new List<(object? subscriber, MethodInfo handler)>();
var toRemove = new List<Subscription>();
if (_eventHandlers.TryGetValue(eventName, out List<Subscription>? target))
{
for (int i = 0; i < target.Count; i++)
{
Subscription subscription = target[i];
bool isStatic = subscription.Subscriber == null;
if (isStatic)
{
// For a static method, we'll just pass null as the first parameter of MethodInfo.Invoke
toRaise.Add((null, subscription.Handler));
continue;
}
object? subscriber = subscription.Subscriber?.Target;
if (subscriber == null)
// The subscriber was collected, so there's no need to keep this subscription around
toRemove.Add(subscription);
else
toRaise.Add((subscriber, subscription.Handler));
}
for (int i = 0; i < toRemove.Count; i++)
{
Subscription subscription = toRemove[i];
target.Remove(subscription);
}
}
for (int i = 0; i < toRaise.Count; i++)
{
(var subscriber, var handler) = toRaise[i];
handler.Invoke(subscriber, new[] { sender, args });
}
}
public void RemoveEventHandler<TEventArgs>(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
where TEventArgs : EventArgs
{
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
}
public void RemoveEventHandler(Delegate? handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrEmpty(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
RemoveEventHandler(eventName, handler.Target, handler.GetMethodInfo());
}
void AddEventHandler(string eventName, object? handlerTarget, MethodInfo methodInfo)
{
if (!_eventHandlers.TryGetValue(eventName, out List<Subscription>? targets))
{
targets = new List<Subscription>();
_eventHandlers.Add(eventName, targets);
}
if (handlerTarget == null)
{
// This event handler is a static method
targets.Add(new Subscription(null, methodInfo));
return;
}
targets.Add(new Subscription(new WeakReference(handlerTarget), methodInfo));
}
void RemoveEventHandler(string eventName, object? handlerTarget, MemberInfo methodInfo)
{
if (!_eventHandlers.TryGetValue(eventName, out List<Subscription>? subscriptions))
return;
for (int n = subscriptions.Count; n > 0; n--)
{
Subscription current = subscriptions[n - 1];
if (current.Subscriber?.Target != handlerTarget || current.Handler.Name != methodInfo.Name)
continue;
subscriptions.Remove(current);
break;
}
}
struct Subscription
{
public Subscription(WeakReference? subscriber, MethodInfo handler)
{
Subscriber = subscriber;
Handler = handler ?? throw new ArgumentNullException(nameof(handler));
}
public readonly WeakReference? Subscriber;
public readonly MethodInfo Handler;
}
}
}