EventManager.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Threading.Tasks;
  3. using MediaBrowser.Controller;
  4. using MediaBrowser.Controller.Events;
  5. using Microsoft.Extensions.DependencyInjection;
  6. namespace Jellyfin.Server.Implementations.Events
  7. {
  8. /// <summary>
  9. /// Handles the firing of events.
  10. /// </summary>
  11. public class EventManager : IEventManager
  12. {
  13. private readonly IServerApplicationHost _appHost;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="EventManager"/> class.
  16. /// </summary>
  17. /// <param name="appHost">The application host.</param>
  18. public EventManager(IServerApplicationHost appHost)
  19. {
  20. _appHost = appHost;
  21. }
  22. /// <inheritdoc />
  23. public void Publish<T>(T eventArgs)
  24. where T : EventArgs
  25. {
  26. Task.WaitAll(PublishInternal(eventArgs));
  27. }
  28. /// <inheritdoc />
  29. public async Task PublishAsync<T>(T eventArgs)
  30. where T : EventArgs
  31. {
  32. await PublishInternal(eventArgs).ConfigureAwait(false);
  33. }
  34. private async Task PublishInternal<T>(T eventArgs)
  35. where T : EventArgs
  36. {
  37. using var scope = _appHost.ServiceProvider.CreateScope();
  38. foreach (var service in scope.ServiceProvider.GetServices<IEventConsumer<T>>())
  39. {
  40. await service.OnEvent(eventArgs).ConfigureAwait(false);
  41. }
  42. }
  43. }
  44. }