2026-10-03 15:26:21 +02:00
|
|
|
using collAnon.Client.Services;
|
|
|
|
|
|
|
|
|
|
using decePubClient.Services;
|
|
|
|
|
|
2022-12-15 20:03:40 +01:00
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace decePubClient.LayerComponents
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Base of every page and app component (b2b.next's WidmannComponentBase). It gives them the cascading state, the
|
|
|
|
|
/// localizer, navigation and toasts, the loading flag and the disable rule, and the offline helpers: a page that
|
|
|
|
|
/// finds the app offline registers its fetch with WhenOnline, starts StartOnlinePolling and stops loading; the
|
|
|
|
|
/// job runs once when the app is back online.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LocalizableComponentBase : ComponentBase, IAsyncDisposable
|
2022-12-15 20:03:40 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
[CascadingParameter] protected CascadingState CascadingState { get; set; }
|
2022-12-15 20:03:40 +01:00
|
|
|
[Inject] public CoalescingStringLocalizer Localizer { get; set; }
|
2026-10-03 15:26:21 +02:00
|
|
|
[Inject] protected NavigationManager Navigation { get; set; }
|
|
|
|
|
[Inject] protected ToastService Toast { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>Work to run once after the next render, e.g. JS interop that needs the rendered DOM.</summary>
|
|
|
|
|
protected List<Func<Task>> AfterRenderAsyncJobs = [];
|
|
|
|
|
|
|
|
|
|
/// <summary>True until the component's first load completes; set it to false at the end of OnInitializedAsync.</summary>
|
2022-12-15 20:03:40 +01:00
|
|
|
protected bool IsLoading { get; set; } = true;
|
2026-10-03 15:26:21 +02:00
|
|
|
|
|
|
|
|
/// <summary>What every Disabled= binding starts from: loading, or no connection.</summary>
|
|
|
|
|
protected bool IsDefaultDisabled => IsLoading || CascadingState.IsOffline;
|
|
|
|
|
|
|
|
|
|
readonly List<Func<Task>> onOnlineJobs = [];
|
|
|
|
|
PeriodicTimer pollingTimer;
|
|
|
|
|
CancellationTokenSource pollingCancellation;
|
|
|
|
|
bool disposed;
|
|
|
|
|
|
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
|
|
|
{
|
|
|
|
|
if (AfterRenderAsyncJobs.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var jobs = AfterRenderAsyncJobs.ToList();
|
|
|
|
|
AfterRenderAsyncJobs.Clear();
|
|
|
|
|
foreach (var job in jobs)
|
|
|
|
|
await job();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Runs the job once, the next time the app is online (needs StartOnlinePolling).</summary>
|
|
|
|
|
protected void WhenOnline(Func<Task> job) => onOnlineJobs.Add(job);
|
|
|
|
|
|
|
|
|
|
/// <summary>Checks every interval (10 seconds by default) whether the app is back online to run the WhenOnline jobs.</summary>
|
|
|
|
|
protected void StartOnlinePolling(TimeSpan? interval = null)
|
|
|
|
|
{
|
|
|
|
|
if (pollingTimer is not null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pollingCancellation = new();
|
|
|
|
|
pollingTimer = new(interval ?? TimeSpan.FromSeconds(10));
|
|
|
|
|
_ = PollOnline(pollingCancellation.Token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task PollOnline(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
while (await pollingTimer.WaitForNextTickAsync(cancellationToken))
|
|
|
|
|
{
|
|
|
|
|
if (CascadingState.IsOffline || onOnlineJobs.Count == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var jobs = onOnlineJobs.ToList();
|
|
|
|
|
onOnlineJobs.Clear();
|
|
|
|
|
foreach (var job in jobs)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await InvokeAsync(job);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
await CascadingState.ProcessError(ex, $"{GetType().Name}.{nameof(PollOnline)}", cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
if (disposed)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
disposed = true;
|
|
|
|
|
if (pollingCancellation is not null)
|
|
|
|
|
{
|
|
|
|
|
await pollingCancellation.CancelAsync();
|
|
|
|
|
pollingCancellation.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pollingTimer?.Dispose();
|
|
|
|
|
onOnlineJobs.Clear();
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
2022-12-15 20:03:40 +01:00
|
|
|
}
|
|
|
|
|
}
|