using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Hyperbar.Widget; public sealed class WidgetHost : IWidgetHost { private readonly IServiceProvider services; private readonly IMediator mediator; private readonly IProxyService proxyMediator; private readonly IEnumerable hostedServices; public WidgetHost(IServiceProvider services, IMediator mediator, IProxyService proxyMediator, IEnumerable hostedServices) { this.services = services; this.mediator = mediator; this.proxyMediator = proxyMediator; this.hostedServices = hostedServices; } public WidgetConfiguration Configuration => Services.GetRequiredService(); public IServiceProvider Services => services; public void Dispose() { } public async Task StartAsync(CancellationToken cancellationToken = default) { foreach (IHostedService service in hostedServices) { await service.StartAsync(cancellationToken); } if (proxyMediator.Proxy is IMediator mediator) { await mediator.PublishAsync(new Started(this), cancellationToken); } await this.mediator.PublishAsync(new Started(this), cancellationToken); } public async Task StopAsync(CancellationToken cancellationToken = default) { foreach(IHostedService service in hostedServices) { await service.StopAsync(cancellationToken); } } }