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 468
/
Copy pathDelegateWeakEventManager.shared.cs
81 lines (68 loc) · 2.82 KB
/
DelegateWeakEventManager.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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using static System.String;
namespace Xamarin.CommunityToolkit.Helpers
{
/// <summary>
/// Weak Delegate event manager that allows for garbage collection when the EventHandler is still subscribed
/// </summary>
public class DelegateWeakEventManager
{
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(Delegate? 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>
/// Removes the event handler.
/// </summary>
/// <param name="handler">Handler</param>
/// <param name="eventName">Event name</param>
public void RemoveEventHandler(Delegate? 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>
/// 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, object eventArgs, string eventName) => RaiseEvent(sender, eventArgs, eventName);
/// <summary>
/// Invokes the event Action
/// </summary>
/// <param name="eventName">Event name</param>
public void HandleEvent(string eventName) => RaiseEvent(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, object eventArgs, string eventName) =>
EventManagerService.HandleEvent(eventName, sender, eventArgs, eventHandlers);
/// <summary>
/// Invokes the event Action
/// </summary>
/// <param name="eventName">Event name</param>
public void RaiseEvent(string eventName) => EventManagerService.HandleEvent(eventName, eventHandlers);
}
}