|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | + |
| 5 | +namespace AsyncAwaitBestPractices |
| 6 | +{ |
| 7 | + static class EventManagerService |
| 8 | + { |
| 9 | + internal static void AddEventHandler(string eventName, object handlerTarget, MethodInfo methodInfo, in Dictionary<string, List<Subscription>> eventHandlers) |
| 10 | + { |
| 11 | + var doesContainSubscriptions = eventHandlers.TryGetValue(eventName, out List<Subscription> targets); |
| 12 | + if (!doesContainSubscriptions) |
| 13 | + { |
| 14 | + targets = new List<Subscription>(); |
| 15 | + eventHandlers.Add(eventName, targets); |
| 16 | + } |
| 17 | + |
| 18 | + if (handlerTarget is null) |
| 19 | + targets.Add(new Subscription(null, methodInfo)); |
| 20 | + else |
| 21 | + targets.Add(new Subscription(new WeakReference(handlerTarget), methodInfo)); |
| 22 | + } |
| 23 | + |
| 24 | + internal static void RemoveEventHandler(string eventName, object handlerTarget, MemberInfo methodInfo, in Dictionary<string, List<Subscription>> eventHandlers) |
| 25 | + { |
| 26 | + var doesContainSubscriptions = eventHandlers.TryGetValue(eventName, out List<Subscription> subscriptions); |
| 27 | + if (!doesContainSubscriptions) |
| 28 | + return; |
| 29 | + |
| 30 | + for (int n = subscriptions.Count; n > 0; n--) |
| 31 | + { |
| 32 | + Subscription current = subscriptions[n - 1]; |
| 33 | + |
| 34 | + if (current.Subscriber?.Target != handlerTarget |
| 35 | + || current.Handler.Name != methodInfo.Name) |
| 36 | + { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + subscriptions.Remove(current); |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + internal static void HandleEvent(string eventName, object sender, object eventArgs, in Dictionary<string, List<Subscription>> eventHandlers) |
| 46 | + { |
| 47 | + var toRaise = new List<Tuple<object, MethodInfo>>(); |
| 48 | + var toRemove = new List<Subscription>(); |
| 49 | + |
| 50 | + if (eventHandlers.TryGetValue(eventName, out List<Subscription> target)) |
| 51 | + { |
| 52 | + for (int i = 0; i < target.Count; i++) |
| 53 | + { |
| 54 | + Subscription subscription = target[i]; |
| 55 | + bool isStatic = subscription.Subscriber is null; |
| 56 | + if (isStatic) |
| 57 | + { |
| 58 | + toRaise.Add(Tuple.Create<object, MethodInfo>(null, subscription.Handler)); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + object subscriber = subscription.Subscriber.Target; |
| 63 | + |
| 64 | + if (subscriber is null) |
| 65 | + toRemove.Add(subscription); |
| 66 | + else |
| 67 | + toRaise.Add(Tuple.Create(subscriber, subscription.Handler)); |
| 68 | + } |
| 69 | + |
| 70 | + for (int i = 0; i < toRemove.Count; i++) |
| 71 | + { |
| 72 | + Subscription subscription = toRemove[i]; |
| 73 | + target.Remove(subscription); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + for (int i = 0; i < toRaise.Count; i++) |
| 78 | + { |
| 79 | + Tuple<object, MethodInfo> tuple = toRaise[i]; |
| 80 | + tuple.Item2.Invoke(tuple.Item1, new[] { sender, eventArgs }); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments