Skip to content

Commit

Permalink
fix: Added Dictionary of automatically registerd Event Handlers (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheXorog authored Jul 19, 2024
1 parent 78a78b9 commit 0409725
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions DisCatSharp/Clients/DiscordClient.EventHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public sealed partial class DiscordClient
private readonly Dictionary<(object?, Type, bool), List<(EventInfo, Delegate)[]>> _registrationToDelegate = [];
private readonly Dictionary<Type, List<object>> _typeToAnonymousHandlers = [];

/// <summary>
/// Gets all the registered event handlers.
/// </summary>
public IReadOnlyDictionary<Type, object> RegisteredEventhandlers => this._registeredEventhandlers.AsReadOnly();

private readonly Dictionary<Type, object> _registeredEventhandlers = [];

/// <summary>
/// Registers all methods annotated with <see cref="EventAttribute"/> from the given object.
/// </summary>
Expand Down Expand Up @@ -155,6 +162,9 @@ private static IEnumerable<Type> GetEventHandlersFromAssembly(Assembly assembly)
/// <param name="wasRegisteredWithStatic">Whether it considereded static methods.</param>
private void UnregisterEventHandlerImpl(object? handler, Type type, bool wasRegisteredWithStatic = true)
{
if (this._registeredEventhandlers.ContainsKey(type))
this._registeredEventhandlers.Remove(type);

if (!this._registrationToDelegate.TryGetValue((handler, type, wasRegisteredWithStatic), out var delegateLists) || delegateLists.Count == 0)
return;

Expand Down Expand Up @@ -192,6 +202,7 @@ from method in type.GetMethods(BindingFlags.Instance | BindingFlags.Static | Bin
this._registrationToDelegate[(handler, type, registerStatic)] = this._registrationToDelegate.TryGetValue((handler, type, registerStatic), out var delList) ? delList : delList = [];

delList.Add(delegates);
this._registeredEventhandlers.Add(type, handler);

foreach (var (evnt, dlgt) in delegates)
evnt.AddEventHandler(this, dlgt);
Expand Down

0 comments on commit 0409725

Please sign in to comment.