2021-12-20 23:04:53 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
|
2021-12-21 22:22:52 +00:00
|
|
|
namespace Selector.Events
|
2021-12-20 23:04:53 +00:00
|
|
|
{
|
2022-06-28 07:30:27 +01:00
|
|
|
public class UserEventFirer : IPlayerConsumer
|
2021-12-20 23:04:53 +00:00
|
|
|
{
|
|
|
|
protected readonly IPlayerWatcher Watcher;
|
|
|
|
protected readonly ILogger<UserEventFirer> Logger;
|
|
|
|
|
|
|
|
protected readonly UserEventBus UserEvent;
|
|
|
|
|
|
|
|
public CancellationToken CancelToken { get; set; }
|
|
|
|
|
|
|
|
public UserEventFirer(
|
|
|
|
IPlayerWatcher watcher,
|
|
|
|
UserEventBus userEvent,
|
|
|
|
ILogger<UserEventFirer> logger = null,
|
|
|
|
CancellationToken token = default
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Watcher = watcher;
|
|
|
|
UserEvent = userEvent;
|
|
|
|
Logger = logger ?? NullLogger<UserEventFirer>.Instance;
|
|
|
|
CancelToken = token;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Callback(object sender, ListeningChangeEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Current is null) return;
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await AsyncCallback(e);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logger.LogError(e, "Error occured during callback");
|
|
|
|
}
|
|
|
|
}, CancelToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task AsyncCallback(ListeningChangeEventArgs e)
|
|
|
|
{
|
|
|
|
Logger.LogDebug("Firing now playing event on user bus [{username}/{userId}]", e.SpotifyUsername, e.Id);
|
|
|
|
|
2021-12-21 22:22:52 +00:00
|
|
|
UserEvent.OnCurrentlyPlayingChange(this, (CurrentlyPlayingDTO) e);
|
2021-12-20 23:04:53 +00:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Subscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange += Callback;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unsubscribe(IWatcher watch = null)
|
|
|
|
{
|
|
|
|
var watcher = watch ?? Watcher ?? throw new ArgumentNullException("No watcher provided");
|
|
|
|
|
|
|
|
if (watcher is IPlayerWatcher watcherCast)
|
|
|
|
{
|
|
|
|
watcherCast.ItemChange -= Callback;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new ArgumentException("Provided watcher is not a PlayerWatcher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|