using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Hyperbar.Widget; public sealed class WidgetHost(IServiceProvider services, IPublisher publisher, IProxyService proxyPublisher, IEnumerable hostedServices) : IWidgetHost { private readonly IPublisher publisher = publisher; 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 (proxyPublisher.Proxy is IPublisher publisher) { await publisher.PublishAsync(new Started(this), cancellationToken); } await this.publisher.PublishAsync(new Started(this), cancellationToken); } public async Task StopAsync(CancellationToken cancellationToken = default) { foreach(IHostedService service in hostedServices) { await service.StopAsync(cancellationToken); } } }