Improve logic for Unregistering Listeners

This commit is contained in:
MidoriKami 2023-09-08 02:33:27 -07:00
parent e914f33990
commit 967c79fdd3

View file

@ -328,19 +328,15 @@ internal class AddonLifecyclePluginScoped : IDisposable, IServiceType, IAddonLif
/// <inheritdoc/> /// <inheritdoc/>
public void UnregisterListener(AddonEvent eventType, string addonName, IAddonLifecycle.AddonEventDelegate? handler = null) public void UnregisterListener(AddonEvent eventType, string addonName, IAddonLifecycle.AddonEventDelegate? handler = null)
{ {
// This style is simpler to read imo. If the handler is null we want all entries, this.eventListeners.RemoveAll(entry =>
// if they specified a handler then only the specific entries with that handler.
var targetListeners = this.eventListeners
.Where(entry => entry.EventType == eventType)
.Where(entry => entry.AddonName == addonName)
.Where(entry => handler is null || entry.FunctionDelegate == handler)
.ToArray(); // Make a copy so we don't mutate this list while removing entries.
foreach (var listener in targetListeners)
{ {
this.addonLifecycleService.UnregisterListener(listener); if (entry.EventType != eventType) return false;
this.eventListeners.Remove(listener); if (entry.AddonName != addonName) return false;
} if (handler is not null && entry.FunctionDelegate != handler) return false;
this.addonLifecycleService.UnregisterListener(entry);
return true;
});
} }
/// <inheritdoc/> /// <inheritdoc/>