From 449b483046fd5ba2974d4bc0bc2d78e7a879cbc4 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:42:16 -0500 Subject: [PATCH] perf(dashboard): NativeDialog skips repeat close() interop once the dialog is closed (DSH-008, #256) --- .../Components/Shared/NativeDialog.razor | 13 +- .../Dashboard/NativeDialogInteropTests.cs | 156 ++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs diff --git a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Shared/NativeDialog.razor b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Shared/NativeDialog.razor index 2ea34f4c..78610fa4 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Shared/NativeDialog.razor +++ b/src/ZB.MOM.WW.MxGateway.Server/Dashboard/Components/Shared/NativeDialog.razor @@ -22,6 +22,10 @@ private bool _cancelPending; private object? _lastTarget; private long _generation; + // Generation the JS side was last told to close. close() on an already-closed dialog is a + // no-op there, and the host pages re-render every snapshot tick, so without this a dismissed + // dialog cost one interop round trip per tick for the life of the tab (DSH-008). + private long _closedGeneration = -1; private Task? _disposeTask; /// Whether the parent requests an interactive native modal. @@ -72,7 +76,11 @@ if (_module is null) return; if (!IsOpen) { - await _module.InvokeVoidAsync("close", _element); + if (_closedGeneration != generation) + { + await _module.InvokeVoidAsync("close", _element); + _closedGeneration = generation; + } } else if (_needsOpen) { @@ -82,6 +90,9 @@ openedNow = opened; if (!opened) rejectedGeneration = generation; } + // update() is deliberately not de-duplicated: the JS side clears its in-flight "pending" + // flag there, and a re-render with unchanged parameters is how a rejected submit + // (validation error, guarded handler) re-enables Escape-to-cancel. if (!openedNow && !_disposed && generation == _generation && IsOpen) await _module.InvokeVoidAsync("update", _element, generation, IsBusy); } diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs new file mode 100644 index 00000000..b345baa4 --- /dev/null +++ b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs @@ -0,0 +1,156 @@ +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Rendering; +using Microsoft.AspNetCore.Components.RenderTree; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.JSInterop; +using ZB.MOM.WW.MxGateway.Server.Dashboard.Components.Shared; + +namespace ZB.MOM.WW.MxGateway.Tests.Dashboard; + +/// DSH-008: a dismissed must not cost JS interop on every parent render. +public sealed class NativeDialogInteropTests +{ + /// Open then close, then ten parent re-renders: only the one close call reaches JS. + /// A task that represents the asynchronous operation. + [Fact] + public async Task ClosedDialog_WhenParentRerenders_DoesNotCallCloseAgain() + { + RecordingJsRuntime js = new(); + ServiceCollection services = new(); + services.AddLogging(); + services.AddSingleton(js); + await using ServiceProvider provider = services.BuildServiceProvider(); + await using TestRenderer renderer = new(provider, provider.GetRequiredService()); + HostHandle handle = new(); + + await renderer.MountAsync(handle); + + await SetOpenAsync(renderer, handle, true); + Assert.Equal(1, js.ModuleCalls.Count(call => call == "open")); + + await SetOpenAsync(renderer, handle, false); + Assert.Equal(1, js.ModuleCalls.Count(call => call == "close")); + + for (int i = 0; i < 10; i++) + { + await renderer.Dispatcher.InvokeAsync(() => handle.Host!.Rerender()); + await Task.Delay(10); + } + + Assert.Equal(1, js.ModuleCalls.Count(call => call == "close")); + } + + private static async Task SetOpenAsync(TestRenderer renderer, HostHandle handle, bool open) + { + await renderer.Dispatcher.InvokeAsync(() => + { + handle.Host!.Open = open; + handle.Host.Rerender(); + }); + await Task.Delay(50); + } + + // Minimal renderer that runs OnAfterRenderAsync (the static HtmlRenderer does not), which is + // where NativeDialog makes its interop calls. BL0006 is suppressed for this test-only scaffold. +#pragma warning disable BL0006 + private sealed class TestRenderer(IServiceProvider services, ILoggerFactory loggerFactory) + : Renderer(services, loggerFactory) + { + /// + public override Dispatcher Dispatcher { get; } = Dispatcher.CreateDefault(); + + /// Mounts the host component and renders it. + /// Handle the host publishes itself through. + /// A task that completes once the first render has been dispatched. + public Task MountAsync(HostHandle handle) => Dispatcher.InvokeAsync(() => + { + DialogHost host = (DialogHost)InstantiateComponent(typeof(DialogHost)); + host.Handle = handle; + int id = AssignRootComponentId(host); + return RenderRootComponentAsync(id); + }); + + /// + protected override void HandleException(Exception exception) => throw new InvalidOperationException("render fault", exception); + + /// + protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) => Task.CompletedTask; + } +#pragma warning restore BL0006 + + private sealed class HostHandle + { + /// Gets or sets the mounted host. + public DialogHost? Host { get; set; } + } + + private sealed class DialogHost : ComponentBase + { + /// Gets or sets the handle the test uses to reach this instance. + public HostHandle Handle { get; set; } = null!; + + /// Gets or sets a value indicating whether the dialog is open. + public bool Open { get; set; } + + /// Requests a render, as a snapshot tick does for the host pages. + public void Rerender() => StateHasChanged(); + + /// + protected override void OnInitialized() => Handle.Host = this; + + /// + protected override void BuildRenderTree(RenderTreeBuilder builder) + { + builder.OpenComponent(0); + builder.AddAttribute(1, nameof(NativeDialog.IsOpen), Open); + builder.AddAttribute(2, nameof(NativeDialog.LabelledBy), "title"); + + // A RenderFragment is not a change-detectable parameter, so the dialog re-renders with + // its host exactly as it does on the real pages. + builder.AddAttribute(3, nameof(NativeDialog.ChildContent), (RenderFragment)(content => { })); + builder.CloseComponent(); + } + } + + private sealed class RecordingJsRuntime : IJSRuntime + { + private readonly RecordingModule _module = new(); + + /// Gets the module-level calls made, in order. + public List ModuleCalls => _module.Calls; + + /// + public ValueTask InvokeAsync(string identifier, object?[]? args) => + new((TValue)(object)_module); + + /// + public ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken, object?[]? args) => + InvokeAsync(identifier, args); + } + + private sealed class RecordingModule : IJSObjectReference + { + /// Gets the calls made, in order. + public List Calls { get; } = []; + + /// + public ValueTask InvokeAsync(string identifier, object?[]? args) + { + lock (Calls) + { + Calls.Add(identifier); + } + + return new(typeof(TValue) == typeof(bool) ? (TValue)(object)true : default!); + } + + /// + public ValueTask InvokeAsync(string identifier, CancellationToken cancellationToken, object?[]? args) => + InvokeAsync(identifier, args); + + /// + public ValueTask DisposeAsync() => ValueTask.CompletedTask; + } +}