aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Events/EventManager.cs
blob: d79b7310639e66d81bf583896ac4d14ec9b3d8df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Threading.Tasks;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Events;
using Microsoft.Extensions.DependencyInjection;

namespace Jellyfin.Server.Implementations.Events
{
    /// <summary>
    /// Handles the firing of events.
    /// </summary>
    public class EventManager : IEventManager
    {
        private readonly IServerApplicationHost _appHost;

        /// <summary>
        /// Initializes a new instance of the <see cref="EventManager"/> class.
        /// </summary>
        /// <param name="appHost">The application host.</param>
        public EventManager(IServerApplicationHost appHost)
        {
            _appHost = appHost;
        }

        /// <inheritdoc />
        public void Publish<T>(T eventArgs)
            where T : EventArgs
        {
            Task.WaitAll(PublishInternal(eventArgs));
        }

        /// <inheritdoc />
        public async Task PublishAsync<T>(T eventArgs)
            where T : EventArgs
        {
            await PublishInternal(eventArgs).ConfigureAwait(false);
        }

        private async Task PublishInternal<T>(T eventArgs)
            where T : EventArgs
        {
            using var scope = _appHost.ServiceProvider.CreateScope();
            foreach (var service in scope.ServiceProvider.GetServices<IEventConsumer<T>>())
            {
                await service.OnEvent(eventArgs).ConfigureAwait(false);
            }
        }
    }
}