sonarr-repo-only/NzbDrone.Common/Messaging/MessageAggregator.cs

50 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2013-02-23 20:09:44 +00:00
using NLog;
namespace NzbDrone.Common.Messaging
{
public class MessageAggregator : IMessageAggregator
{
2013-02-23 20:09:44 +00:00
private readonly Logger _logger;
private readonly Func<IEnumerable<IProcessMessage>> _handlers;
public MessageAggregator(Logger logger, Func<IEnumerable<IProcessMessage>> handlers)
{
2013-02-23 20:09:44 +00:00
_logger = logger;
_handlers = handlers;
}
public void Publish<TEvent>(TEvent message) where TEvent : IEvent
{
2013-02-23 20:09:44 +00:00
_logger.Trace("Publishing {0}", message.GetType().Name);
//call synchronous handlers first.
foreach (var handler in _handlers().OfType<IHandle<TEvent>>())
{
_logger.Debug("{0} -> {1}", message.GetType().Name, handler.GetType().Name);
2013-02-23 20:09:44 +00:00
handler.Handle(message);
_logger.Debug("{0} <- {1}", message.GetType().Name, handler.GetType().Name);
}
foreach (var handler in _handlers().OfType<IHandleAsync<TEvent>>())
{
var handlerLocal = handler;
Task.Factory.StartNew(() =>
{
_logger.Debug("{0} ~> {1}", message.GetType().Name, handlerLocal.GetType().Name);
handlerLocal.HandleAsync(message);
_logger.Debug("{0} <~ {1}", message.GetType().Name, handlerLocal.GetType().Name);
});
}
}
public void Execute<TCommand>(TCommand message) where TCommand : ICommand
{
throw new NotImplementedException();
}
}
}