| File: AutoPauseBrowserOptionsExtensions.cs | Web Access |
| Project: src\aspnetcore\src\Components\Server.AutoPause\src\Microsoft.AspNetCore.Components.Server.AutoPause.csproj (Microsoft.AspNetCore.Components.Server.AutoPause) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Text.Json; using System.Text.Json.Serialization; namespace Microsoft.AspNetCore.Components; /// <summary> /// Extension methods for enabling auto-pause through <see cref="BrowserOptions"/>. /// </summary> public static class AutoPauseBrowserOptionsExtensions { /// <summary> /// Enables automatic circuit pausing for hidden tabs. The configuration is written to the /// circuit's library extension data and flows to the client, where the auto-pause JavaScript /// initializer reads it. No core Blazor type needs to know about auto-pause. /// </summary> /// <param name="options">The <see cref="BrowserOptions"/> to configure.</param> /// <param name="configure">An optional callback to customize the auto-pause behavior.</param> /// <returns>The same <see cref="BrowserOptions"/> instance for chaining.</returns> public static BrowserOptions AddAutoPause(this BrowserOptions options, Action<AutoPauseBrowserOptions>? configure = null) { ArgumentNullException.ThrowIfNull(options); var settings = new AutoPauseBrowserOptions(); configure?.Invoke(settings); var delayMilliseconds = Math.Ceiling(settings.HiddenDelay.TotalMilliseconds); if (settings.Enabled && settings.HiddenDelay <= TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(configure), settings.HiddenDelay, $"{nameof(AutoPauseBrowserOptions.HiddenDelay)} must be greater than zero."); } // Flat keys follow the existing browser-config convention (see InteractiveServerBrowserOptions). options.InteractiveServer.Extensions["autoPauseEnabled"] = JsonSerializer.SerializeToElement(settings.Enabled, AutoPauseJsonContext.Default.Boolean); options.InteractiveServer.Extensions["autoPauseHiddenDelayMilliseconds"] = JsonSerializer.SerializeToElement((long)delayMilliseconds, AutoPauseJsonContext.Default.Int64); return options; } } [JsonSerializable(typeof(bool))] [JsonSerializable(typeof(long))] internal sealed partial class AutoPauseJsonContext : JsonSerializerContext { }