using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using Selector.Web.Hubs; using Selector.Events; using Selector.SignalR; namespace Selector.Web.Service { public class NowPlayingHubMapping: IEventHubMapping { private readonly ILogger Logger; private readonly UserEventBus UserEvent; private readonly IHubContext Hub; public NowPlayingHubMapping(ILogger logger, UserEventBus userEvent, IHubContext hub) { Logger = logger; UserEvent = userEvent; Hub = hub; } public Task ConstructMapping() { Logger.LogDebug("Forming now playing event mapping between event bus and SignalR hub"); UserEvent.CurrentlyPlaying += async (o, args) => { Logger.LogDebug("Passing now playing event to SignalR hub [{userId}]", args.UserId); await Hub.Clients.User(args.UserId).OnNewPlaying(args); }; return Task.CompletedTask; } } }