From a606407fc4fe670b3ce4c9458f3c0d1fc9215e42 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 13:04:28 -0500 Subject: [PATCH] perf(dashboard): NativeDialog sends update() only on busy/generation change or a reported submit (DSH-008, #258) JS now reports when a submit raises its in-flight pending flag, and a cancel request marks the same, so the next render clears pending through update() without update() running on every snapshot tick. Escape still works after a rejected submit. --- docs/ui-validation/technical-light-dialogs.md | 7 + .../Components/Shared/NativeDialog.razor | 42 ++++- .../wwwroot/js/dashboard-dialog.js | 6 +- .../Dashboard/NativeDialogInteropTests.cs | 172 ++++++++++++++++++ 4 files changed, 222 insertions(+), 5 deletions(-) diff --git a/docs/ui-validation/technical-light-dialogs.md b/docs/ui-validation/technical-light-dialogs.md index 0d88903a..790657d9 100644 --- a/docs/ui-validation/technical-light-dialogs.md +++ b/docs/ui-validation/technical-light-dialogs.md @@ -26,6 +26,13 @@ returns to a usable connected trigger; otherwise the page heading gets temporary programmatic focus, with tabindex removed on blur. A newly claimed outside focus is respected. Native backdrop is the only dimming layer. +Interop is sent only on state changes, because the host pages re-render every +snapshot tick (DSH-008, #258): close() once per generation, update() only when the +generation or busy flag changes or JS has reported a submit. A submit or Escape +raises a client-side pending flag that blocks a second cancel; JS reports the +submit (NotifyPendingAsync) and the next render sends update(), which clears it, so +Escape works again after a rejected submit. + ## Observed checks - Baseline 341 dashboard tests passed. Two semantic tests failed on the old overlay, 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 78610fa4..337174bc 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 @@ -26,6 +26,15 @@ // 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; + // Last generation/busy pair sent to JS through open() or update(). update() runs only when + // that pair changes or JS reported its in-flight "pending" flag set, so an open dialog no + // longer costs one interop round trip per snapshot tick (DSH-008). + private long _updatedGeneration = -1; + private bool _updatedBusy; + // Set when JS reports a submit/cancel that raised its "pending" flag. The next render + // (normally the parent's re-render after handling that submit) sends update() to clear it, so + // a rejected submit (validation error, guarded handler) re-enables Escape-to-cancel. + private bool _pendingClearRequested; private Task? _disposeTask; /// Whether the parent requests an interactive native modal. @@ -54,6 +63,7 @@ ++_generation; _needsOpen = IsOpen; _cancelPending = false; + _pendingClearRequested = false; } /// @@ -86,15 +96,24 @@ { _needsOpen = false; _self ??= DotNetObjectReference.Create(this); + // open() reads busy from the rendered data-busy attribute and starts with pending + // cleared, so it counts as an update for this generation. + _updatedGeneration = generation; + _updatedBusy = IsBusy; bool opened = await _module.InvokeAsync("open", _element, _self, generation); 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) + if (!openedNow && !_disposed && generation == _generation && IsOpen + && (_pendingClearRequested || _updatedGeneration != generation || _updatedBusy != IsBusy)) + { + // Record before the await: a pending report that arrives while update() is in + // flight must survive to the next render. + _pendingClearRequested = false; + _updatedGeneration = generation; + _updatedBusy = IsBusy; await _module.InvokeVoidAsync("update", _element, generation, IsBusy); + } } catch (JSDisconnectedException) { } finally { _lifecycle.Release(); } @@ -105,12 +124,27 @@ [JSInvokable] public Task RequestCancelAsync(long generation) => InvokeAsync(async () => { + // JS raised its pending flag before calling here; a rejected cancel must still clear it. + if (generation == _generation) _pendingClearRequested = true; if (_disposed || !IsOpen || IsBusy || _cancelPending || generation != _generation) return; _cancelPending = true; try { await OnCancel.InvokeAsync(); } finally { if (generation == _generation) _cancelPending = false; } }); + /// + /// Invoked by JS when a submit raised its in-flight pending flag. No render is requested: the + /// parent's re-render after handling the submit (or the next snapshot tick) sends update(), + /// which clears the flag, so Escape works again after a rejected submit. + /// + /// Presentation generation the submit belongs to. + /// A task that completes once the report is recorded. + [JSInvokable] + public Task NotifyPendingAsync(long generation) => InvokeAsync(() => + { + if (!_disposed && generation == _generation) _pendingClearRequested = true; + }); + /// public ValueTask DisposeAsync() => new(_disposeTask ??= DisposeCoreAsync()); diff --git a/src/ZB.MOM.WW.MxGateway.Server/wwwroot/js/dashboard-dialog.js b/src/ZB.MOM.WW.MxGateway.Server/wwwroot/js/dashboard-dialog.js index e2aecf3e..c6c43050 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/wwwroot/js/dashboard-dialog.js +++ b/src/ZB.MOM.WW.MxGateway.Server/wwwroot/js/dashboard-dialog.js @@ -21,7 +21,11 @@ export function open(dialog, dotnet, generation) { dotnet.invokeMethodAsync('RequestCancelAsync', generation).catch(() => {}); }; state.operation = event => { - if (event.type === 'submit' || event.target.closest('[data-dialog-submit]')) state.pending = true; + if (event.type !== 'submit' && !event.target.closest('[data-dialog-submit]')) return; + state.pending = true; + // The server sends update() (which clears pending) only after this report, not on every + // render, so a rejected submit must say it happened. + dotnet.invokeMethodAsync('NotifyPendingAsync', generation).catch(() => {}); }; states.set(dialog, state); dialog.addEventListener('cancel', state.cancel); diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs index b345baa4..f8b9a7f6 100644 --- a/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Tests/Dashboard/NativeDialogInteropTests.cs @@ -42,6 +42,139 @@ public sealed class NativeDialogInteropTests Assert.Equal(1, js.ModuleCalls.Count(call => call == "close")); } + /// An open, idle dialog sends no update() while the parent re-renders unchanged. + /// A task that represents the asynchronous operation. + [Fact] + public async Task OpenDialog_WhenParentRerendersUnchanged_DoesNotCallUpdate() + { + (RecordingJsRuntime js, ServiceProvider provider, TestRenderer renderer, HostHandle handle) = await MountAsync(); + await using ServiceProvider providerScope = provider; + await using TestRenderer rendererScope = renderer; + + await SetOpenAsync(renderer, handle, true); + await RerenderAsync(renderer, handle, 10); + + Assert.Equal(1, js.ModuleCalls.Count(call => call == "open")); + Assert.Equal(0, js.ModuleCalls.Count(call => call == "update")); + } + + /// A busy transition reaches JS once per change, not once per render. + /// A task that represents the asynchronous operation. + [Fact] + public async Task OpenDialog_WhenBusyChanges_CallsUpdateOncePerChange() + { + (RecordingJsRuntime js, ServiceProvider provider, TestRenderer renderer, HostHandle handle) = await MountAsync(); + await using ServiceProvider providerScope = provider; + await using TestRenderer rendererScope = renderer; + + await SetOpenAsync(renderer, handle, true); + await SetBusyAsync(renderer, handle, true); + await RerenderAsync(renderer, handle, 5); + Assert.Equal([true], js.UpdateBusyArguments); + + await SetBusyAsync(renderer, handle, false); + await RerenderAsync(renderer, handle, 5); + Assert.Equal([true, false], js.UpdateBusyArguments); + } + + /// + /// A rejected submit (JS raised pending, the parent re-rendered without going busy or closing) + /// sends exactly one update(), which is what clears JS pending, and Escape then cancels. + /// + /// A task that represents the asynchronous operation. + [Fact] + public async Task RejectedSubmit_SendsOneUpdate_ThenEscapeCancels() + { + (RecordingJsRuntime js, ServiceProvider provider, TestRenderer renderer, HostHandle handle) = await MountAsync(); + await using ServiceProvider providerScope = provider; + await using TestRenderer rendererScope = renderer; + + await SetOpenAsync(renderer, handle, true); + await RerenderAsync(renderer, handle, 3); + Assert.Equal(0, js.ModuleCalls.Count(call => call == "update")); + long generation = js.OpenGeneration; + + // JS: submit clicked -> pending raised and reported; the handler rejects and the parent + // re-renders with nothing changed. + await handle.Host!.Dialog!.NotifyPendingAsync(generation); + await RerenderAsync(renderer, handle, 1); + Assert.Equal([generation], js.UpdateGenerations); + + // Further ticks do not repeat it. + await RerenderAsync(renderer, handle, 5); + Assert.Equal([generation], js.UpdateGenerations); + + // JS: Escape with pending cleared -> RequestCancelAsync -> parent cancel. + await handle.Host.Dialog.RequestCancelAsync(generation); + Assert.Equal(1, handle.Host.CancelCount); + } + + /// An Escape the parent ignores still gets JS pending cleared on the next render. + /// A task that represents the asynchronous operation. + [Fact] + public async Task IgnoredCancel_SendsUpdateOnNextRender() + { + (RecordingJsRuntime js, ServiceProvider provider, TestRenderer renderer, HostHandle handle) = await MountAsync(); + await using ServiceProvider providerScope = provider; + await using TestRenderer rendererScope = renderer; + + await SetOpenAsync(renderer, handle, true); + long generation = js.OpenGeneration; + await handle.Host!.Dialog!.RequestCancelAsync(generation); + await RerenderAsync(renderer, handle, 3); + + Assert.Equal(1, handle.Host.CancelCount); + Assert.Equal([generation], js.UpdateGenerations); + } + + /// A pending report for a superseded generation is ignored. + /// A task that represents the asynchronous operation. + [Fact] + public async Task NotifyPending_ForStaleGeneration_DoesNotCallUpdate() + { + (RecordingJsRuntime js, ServiceProvider provider, TestRenderer renderer, HostHandle handle) = await MountAsync(); + await using ServiceProvider providerScope = provider; + await using TestRenderer rendererScope = renderer; + + await SetOpenAsync(renderer, handle, true); + await handle.Host!.Dialog!.NotifyPendingAsync(js.OpenGeneration - 1); + await RerenderAsync(renderer, handle, 3); + + Assert.Equal(0, js.ModuleCalls.Count(call => call == "update")); + } + + private static async Task<(RecordingJsRuntime Js, ServiceProvider Provider, TestRenderer Renderer, HostHandle Handle)> MountAsync() + { + RecordingJsRuntime js = new(); + ServiceCollection services = new(); + services.AddLogging(); + services.AddSingleton(js); + ServiceProvider provider = services.BuildServiceProvider(); + TestRenderer renderer = new(provider, provider.GetRequiredService()); + HostHandle handle = new(); + await renderer.MountAsync(handle); + return (js, provider, renderer, handle); + } + + private static async Task RerenderAsync(TestRenderer renderer, HostHandle handle, int count) + { + for (int i = 0; i < count; i++) + { + await renderer.Dispatcher.InvokeAsync(() => handle.Host!.Rerender()); + await Task.Delay(10); + } + } + + private static async Task SetBusyAsync(TestRenderer renderer, HostHandle handle, bool busy) + { + await renderer.Dispatcher.InvokeAsync(() => + { + handle.Host!.Busy = busy; + handle.Host.Rerender(); + }); + await Task.Delay(50); + } + private static async Task SetOpenAsync(TestRenderer renderer, HostHandle handle, bool open) { await renderer.Dispatcher.InvokeAsync(() => @@ -94,6 +227,15 @@ public sealed class NativeDialogInteropTests /// Gets or sets a value indicating whether the dialog is open. public bool Open { get; set; } + /// Gets or sets a value indicating whether the parent reports in-flight work. + public bool Busy { get; set; } + + /// Gets the number of cancel callbacks; the parent ignores them (stays open). + public int CancelCount { get; private set; } + + /// Gets the rendered dialog instance. + public NativeDialog? Dialog { get; private set; } + /// Requests a render, as a snapshot tick does for the host pages. public void Rerender() => StateHasChanged(); @@ -110,6 +252,9 @@ public sealed class NativeDialogInteropTests // 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.AddAttribute(4, nameof(NativeDialog.IsBusy), Busy); + builder.AddAttribute(5, nameof(NativeDialog.OnCancel), EventCallback.Factory.Create(this, () => CancelCount++)); + builder.AddComponentReferenceCapture(6, dialog => Dialog = (NativeDialog)dialog); builder.CloseComponent(); } } @@ -121,6 +266,15 @@ public sealed class NativeDialogInteropTests /// Gets the module-level calls made, in order. public List ModuleCalls => _module.Calls; + /// Gets the generation passed to the most recent open() call. + public long OpenGeneration => _module.OpenGeneration; + + /// Gets the generation argument of each update() call, in order. + public List UpdateGenerations => _module.UpdateGenerations; + + /// Gets the busy argument of each update() call, in order. + public List UpdateBusyArguments => _module.UpdateBusyArguments; + /// public ValueTask InvokeAsync(string identifier, object?[]? args) => new((TValue)(object)_module); @@ -135,12 +289,30 @@ public sealed class NativeDialogInteropTests /// Gets the calls made, in order. public List Calls { get; } = []; + /// Gets the generation passed to the most recent open() call. + public long OpenGeneration { get; private set; } + + /// Gets the generation argument of each update() call, in order. + public List UpdateGenerations { get; } = []; + + /// Gets the busy argument of each update() call, in order. + public List UpdateBusyArguments { get; } = []; + /// public ValueTask InvokeAsync(string identifier, object?[]? args) { lock (Calls) { Calls.Add(identifier); + if (identifier == "open") + { + OpenGeneration = (long)args![2]!; + } + else if (identifier == "update") + { + UpdateGenerations.Add((long)args![1]!); + UpdateBusyArguments.Add((bool)args[2]!); + } } return new(typeof(TValue) == typeof(bool) ? (TValue)(object)true : default!);