Cache EventAggregator Subscribers.

pull/2268/merge
Taloth Saldono 7 years ago committed by Taloth
parent ff8eb0b67f
commit 67038ddd5e

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
@ -14,12 +15,38 @@ namespace NzbDrone.Core.Messaging.Events
private readonly Logger _logger;
private readonly IServiceFactory _serviceFactory;
private readonly TaskFactory _taskFactory;
private readonly Dictionary<string, object> _eventSubscribers;
private class EventSubscribers<TEvent> where TEvent : class, IEvent
{
private IServiceFactory _serviceFactory;
public IHandle<TEvent>[] _syncHandlers;
public IHandleAsync<TEvent>[] _asyncHandlers;
public IHandleAsync<IEvent>[] _globalHandlers;
public EventSubscribers(IServiceFactory serviceFactory)
{
_serviceFactory = serviceFactory;
_syncHandlers = serviceFactory.BuildAll<IHandle<TEvent>>()
.OrderBy(GetEventHandleOrder)
.ToArray();
_globalHandlers = serviceFactory.BuildAll<IHandleAsync<IEvent>>()
.ToArray();
_asyncHandlers = serviceFactory.BuildAll<IHandleAsync<TEvent>>()
.ToArray();
}
}
public EventAggregator(Logger logger, IServiceFactory serviceFactory)
{
_logger = logger;
_serviceFactory = serviceFactory;
_taskFactory = new TaskFactory();
_eventSubscribers = new Dictionary<string, object>();
}
public void PublishEvent<TEvent>(TEvent @event) where TEvent : class, IEvent
@ -47,12 +74,20 @@ namespace NzbDrone.Core.Messaging.Events
_logger.Trace("Publishing {0}", eventName);
EventSubscribers<TEvent> subscribers;
lock (_eventSubscribers)
{
object target;
if (!_eventSubscribers.TryGetValue(eventName, out target))
{
_eventSubscribers[eventName] = target = new EventSubscribers<TEvent>(_serviceFactory);
}
subscribers = target as EventSubscribers<TEvent>;
}
//call synchronous handlers first.
var handlers = _serviceFactory.BuildAll<IHandle<TEvent>>()
.OrderBy(GetEventHandleOrder)
.ToList();
var handlers = subscribers._syncHandlers;
foreach (var handler in handlers)
{
try
@ -67,7 +102,7 @@ namespace NzbDrone.Core.Messaging.Events
}
}
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<IEvent>>())
foreach (var handler in subscribers._globalHandlers)
{
var handlerLocal = handler;
@ -78,7 +113,7 @@ namespace NzbDrone.Core.Messaging.Events
.LogExceptions();
}
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
foreach (var handler in subscribers._asyncHandlers)
{
var handlerLocal = handler;
@ -102,7 +137,7 @@ namespace NzbDrone.Core.Messaging.Events
return string.Format("{0}<{1}>", eventType.Name.Remove(eventType.Name.IndexOf('`')), eventType.GetGenericArguments()[0].Name);
}
private int GetEventHandleOrder<TEvent>(IHandle<TEvent> eventHandler) where TEvent : class, IEvent
internal static int GetEventHandleOrder<TEvent>(IHandle<TEvent> eventHandler) where TEvent : class, IEvent
{
// TODO: Convert "Handle" to nameof(eventHandler.Handle) after .net 4.5
var method = eventHandler.GetType().GetMethod("Handle", new Type[] {typeof(TEvent)});

Loading…
Cancel
Save