This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 471
/
WeakEventManager.shared.cs
143 lines (119 loc) · 5.14 KB
/
WeakEventManager.shared.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using static System.String;
// Inspired by AsyncAwaitBestPractices.WeakEventManager: https://github.com/brminnick/AsyncAwaitBestPractices
namespace Xamarin.CommunityToolkit.Helpers
{
/// <summary>
/// Weak event manager that allows for garbage collection when the EventHandler is still subscribed
/// </summary>
/// <typeparam name="TEventArgs">Event args type.</typeparam>
public class WeakEventManager<TEventArgs>
{
readonly Dictionary<string, List<Subscription>> eventHandlers = new Dictionary<string, List<Subscription>>();
/// <summary>
/// Adds the event handler
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void AddEventHandler(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
var methodInfo = handler.GetMethodInfo() ?? throw new NullReferenceException("Could not locate MethodInfo");
EventManagerService.AddEventHandler(eventName, handler.Target, methodInfo, eventHandlers);
}
/// <summary>
/// Adds the event handler
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
public void AddEventHandler(Action<TEventArgs> action, [CallerMemberName] string eventName = "")
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
if (action == null)
throw new ArgumentNullException(nameof(action));
var methodInfo = action.GetMethodInfo() ?? throw new NullReferenceException("Could not locate MethodInfo");
EventManagerService.AddEventHandler(eventName, action.Target, methodInfo, eventHandlers);
}
/// <summary>
/// Removes the event handler
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(EventHandler<TEventArgs> handler, [CallerMemberName] string eventName = "")
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
if (handler == null)
throw new ArgumentNullException(nameof(handler));
var methodInfo = handler.GetMethodInfo() ?? throw new NullReferenceException("Could not locate MethodInfo");
EventManagerService.RemoveEventHandler(eventName, handler.Target, methodInfo, eventHandlers);
}
/// <summary>
/// Removes the event handler
/// </summary>
/// <param name="action">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(Action<TEventArgs> action, [CallerMemberName] string eventName = "")
{
if (IsNullOrWhiteSpace(eventName))
throw new ArgumentNullException(nameof(eventName));
if (action == null)
throw new ArgumentNullException(nameof(action));
var methodInfo = action.GetMethodInfo() ?? throw new NullReferenceException("Could not locate MethodInfo");
EventManagerService.RemoveEventHandler(eventName, action.Target, methodInfo, eventHandlers);
}
/// <summary>
/// Invokes the event EventHandler
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="eventArgs">Event arguments</param>
/// <param name="eventName">Event name</param>
public void HandleEvent(object? sender, TEventArgs eventArgs, string eventName) => RaiseEvent(sender, eventArgs, eventName);
/// <summary>
/// Invokes the event Action
/// </summary>
/// <param name="eventArgs">Event arguments</param>
/// <param name="eventName">Event name</param>
public void HandleEvent(TEventArgs eventArgs, string eventName) => RaiseEvent(eventArgs, eventName);
/// <summary>
/// Invokes the event EventHandler
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="eventArgs">Event arguments</param>
/// <param name="eventName">Event name</param>
public void RaiseEvent(object? sender, TEventArgs eventArgs, string eventName) =>
EventManagerService.HandleEvent(eventName, sender, eventArgs, eventHandlers);
/// <summary>
/// Invokes the event Action
/// </summary>
/// <param name="eventArgs">Event arguments</param>
/// <param name="eventName">Event name</param>
public void RaiseEvent(TEventArgs eventArgs, string eventName) =>
EventManagerService.HandleEvent(eventName, eventArgs, eventHandlers);
}
/// <summary>
/// Extensions for Xamarin.Forms.WeakEventManager
/// </summary>
public static class WeakEventManagerExtensions
{
/// <summary>
/// Invokes the event EventHandler
/// </summary>
/// <param name="weakEventManager">WeakEventManager</param>
/// <param name="sender">Sender</param>
/// <param name="eventArgs">Event arguments</param>
/// <param name="eventName">Event name</param>
public static void RaiseEvent(this Forms.WeakEventManager weakEventManager, object? sender, object eventArgs, string eventName)
{
_ = weakEventManager ?? throw new ArgumentNullException(nameof(weakEventManager));
weakEventManager.HandleEvent(sender, eventArgs, eventName);
}
}
}