using Microsoft.AspNetCore.SignalR; using ScrapperAPI.Enums; using ScrapperAPI.Hub; using ScrapperAPI.Interfaces; using ScrapperAPI.Records; namespace ScrapperAPI.Bus; public sealed class SignalRScrapeEventBus : IScrapeEventBus { private readonly IHubContext _hub; public SignalRScrapeEventBus(IHubContext hub) => _hub = hub; public Task PublishAsync(ScrapeEvent ev, CancellationToken ct = default) { var tasks = new List(2); // Detalhes só para a sessão if (ev.Type is ScrapeEventType.ItemStarted or ScrapeEventType.ItemSucceeded or ScrapeEventType.ItemFailed) { tasks.Add(_hub.Clients.Group(ScrapeHub.GroupName(ev.SessionId)) .SendAsync("scrapeEvent", ev, ct)); return Task.WhenAll(tasks); } // Overview recebe eventos de "estado/progresso" tasks.Add(_hub.Clients.Group(ScrapeHub.OverviewGroup) .SendAsync("scrapeEvent", ev, ct)); // E a própria sessão também recebe (pra tela da sessão atualizar sem depender do overview) tasks.Add(_hub.Clients.Group(ScrapeHub.GroupName(ev.SessionId)) .SendAsync("scrapeEvent", ev, ct)); return Task.WhenAll(tasks); } }