Files
qbtmud/Lantean.QBTMud/Components/WebSeedsTab.razor.cs
2024-10-22 09:57:50 +01:00

101 lines
3.2 KiB
C#

using Lantean.QBitTorrentClient;
using Lantean.QBitTorrentClient.Models;
using Lantean.QBTMud.Models;
using Lantean.QBTMud.Services;
using Microsoft.AspNetCore.Components;
using System.Net;
namespace Lantean.QBTMud.Components
{
public partial class WebSeedsTab : IAsyncDisposable
{
private readonly CancellationTokenSource _timerCancellationToken = new();
private bool _disposedValue;
[Parameter]
public bool Active { get; set; }
[Parameter, EditorRequired]
public string? Hash { get; set; }
[CascadingParameter(Name = "RefreshInterval")]
public int RefreshInterval { get; set; }
[Inject]
protected IApiClient ApiClient { get; set; } = default!;
[Inject]
protected IDataManager DataManager { get; set; } = default!;
protected IReadOnlyList<WebSeed>? WebSeeds { get; set; }
public async ValueTask DisposeAsync()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
await DisposeAsync(disposing: true);
GC.SuppressFinalize(this);
}
protected virtual async Task DisposeAsync(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_timerCancellationToken.Cancel();
_timerCancellationToken.Dispose();
await Task.CompletedTask;
}
_disposedValue = true;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
using (var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(RefreshInterval)))
{
while (!_timerCancellationToken.IsCancellationRequested && await timer.WaitForNextTickAsync())
{
if (Active && Hash is not null)
{
try
{
WebSeeds = await ApiClient.GetTorrentWebSeeds(Hash);
}
catch (HttpRequestException exception) when (exception.StatusCode == HttpStatusCode.Forbidden || exception.StatusCode == HttpStatusCode.NotFound)
{
_timerCancellationToken.CancelIfNotDisposed();
return;
}
}
await InvokeAsync(StateHasChanged);
}
}
}
}
protected override async Task OnParametersSetAsync()
{
if (Hash is null)
{
return;
}
WebSeeds = await ApiClient.GetTorrentWebSeeds(Hash);
await InvokeAsync(StateHasChanged);
}
protected IEnumerable<ColumnDefinition<WebSeed>> Columns => ColumnsDefinitions;
public static List<ColumnDefinition<WebSeed>> ColumnsDefinitions { get; } =
[
new ColumnDefinition<WebSeed>("URL", w => w.Url, w => w.Url),
];
}
}