mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
Improve LifecycleInvoke efficiency with Dictionary
This commit is contained in:
parent
0533872a73
commit
4f59e09513
1 changed files with 10 additions and 19 deletions
|
|
@ -38,7 +38,7 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a list of all AddonLifecycle Event Listeners.
|
/// Gets a list of all AddonLifecycle Event Listeners.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal List<AddonLifecycleEventListener> EventListeners { get; } = [];
|
internal Dictionary<AddonEvent, List<AddonLifecycleEventListener>> EventListeners { get; } = [];
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
void IInternalDisposableService.DisposeService()
|
void IInternalDisposableService.DisposeService()
|
||||||
|
|
@ -61,10 +61,8 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
|
||||||
/// <param name="listener">The listener to register.</param>
|
/// <param name="listener">The listener to register.</param>
|
||||||
internal void RegisterListener(AddonLifecycleEventListener listener)
|
internal void RegisterListener(AddonLifecycleEventListener listener)
|
||||||
{
|
{
|
||||||
this.framework.RunOnTick(() =>
|
this.EventListeners.TryAdd(listener.EventType, [ listener ]);
|
||||||
{
|
this.EventListeners[listener.EventType].Add(listener);
|
||||||
this.EventListeners.Add(listener);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -73,13 +71,10 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
|
||||||
/// <param name="listener">The listener to unregister.</param>
|
/// <param name="listener">The listener to unregister.</param>
|
||||||
internal void UnregisterListener(AddonLifecycleEventListener listener)
|
internal void UnregisterListener(AddonLifecycleEventListener listener)
|
||||||
{
|
{
|
||||||
// Set removed state to true immediately, then lazily remove it from the EventListeners list on next Framework Update.
|
if (this.EventListeners.TryGetValue(listener.EventType, out var listenerList))
|
||||||
listener.Removed = true;
|
|
||||||
|
|
||||||
this.framework.RunOnTick(() =>
|
|
||||||
{
|
{
|
||||||
this.EventListeners.Remove(listener);
|
listenerList.Remove(listener);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -90,16 +85,12 @@ internal unsafe class AddonLifecycle : IInternalDisposableService
|
||||||
/// <param name="blame">What to blame on errors.</param>
|
/// <param name="blame">What to blame on errors.</param>
|
||||||
internal void InvokeListenersSafely(AddonEvent eventType, AddonArgs args, [CallerMemberName] string blame = "")
|
internal void InvokeListenersSafely(AddonEvent eventType, AddonArgs args, [CallerMemberName] string blame = "")
|
||||||
{
|
{
|
||||||
|
// Early return if we don't have any listeners of this type
|
||||||
|
if (!this.EventListeners.TryGetValue(eventType, out var listenerList)) return;
|
||||||
|
|
||||||
// Do not use linq; this is a high-traffic function, and more heap allocations avoided, the better.
|
// Do not use linq; this is a high-traffic function, and more heap allocations avoided, the better.
|
||||||
foreach (var listener in this.EventListeners)
|
foreach (var listener in listenerList)
|
||||||
{
|
{
|
||||||
if (listener.EventType != eventType)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If the listener is pending removal, and is waiting until the next Framework Update, don't invoke listener.
|
|
||||||
if (listener.Removed)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Match on string.empty for listeners that want events for all addons.
|
// Match on string.empty for listeners that want events for all addons.
|
||||||
if (!string.IsNullOrWhiteSpace(listener.AddonName) && !args.IsAddon(listener.AddonName))
|
if (!string.IsNullOrWhiteSpace(listener.AddonName) && !args.IsAddon(listener.AddonName))
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue