44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using Microsoft.JSInterop;
|
|
|
|
namespace Lagerbestand.Components.Shared;
|
|
|
|
/// <summary>
|
|
/// Helper that wires a page's reload method to the browser's
|
|
/// <c>visibilitychange</c> event via <c>appInterop.registerVisibilityRefresh</c>.
|
|
/// The caller owns the <see cref="DotNetObjectReference"/> passed in.
|
|
/// </summary>
|
|
public static class VisibilityRefresh
|
|
{
|
|
/// <summary>
|
|
/// Registers a .NET callback invoked when the browser tab becomes
|
|
/// visible again after being hidden for at least <paramref name="thresholdSeconds"/>.
|
|
/// </summary>
|
|
public static async Task RegisterAsync<T>(
|
|
IJSRuntime js,
|
|
DotNetObjectReference<T> dotNetRef,
|
|
string methodName,
|
|
int thresholdSeconds = 120) where T : class
|
|
{
|
|
try
|
|
{
|
|
await js.InvokeVoidAsync("appInterop.registerVisibilityRefresh", dotNetRef, methodName, thresholdSeconds);
|
|
}
|
|
catch
|
|
{
|
|
// Circuit may be mid-reconnect — non-fatal.
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unregisters the visibility callback. Safe to call multiple times.
|
|
/// </summary>
|
|
public static async Task UnregisterAsync(IJSRuntime js)
|
|
{
|
|
try
|
|
{
|
|
await js.InvokeVoidAsync("appInterop.unregisterVisibilityRefresh");
|
|
}
|
|
catch { /* ignore */ }
|
|
}
|
|
}
|