@inject IJSRuntime JS @implements IAsyncDisposable @if (Visible) { var closeGeneration = _generation; } @code { /// Whether this overlay is presented. [Parameter] public bool Visible { get; set; } /// The accessible dialog title. [Parameter] public string Title { get; set; } = "Dialog"; /// Optional title markup; its text must retain the dialog's domain name. [Parameter] public RenderFragment? TitleContent { get; set; } /// Optional plain description linked to the modal container. [Parameter] public string? Description { get; set; } /// Optional existing Bootstrap dialog size/scroll modifiers. [Parameter] public string? DialogClass { get; set; } /// The body content. [Parameter] public RenderFragment? ChildContent { get; set; } /// The caller's action controls. [Parameter] public RenderFragment? Footer { get; set; } /// Prevents close while an operation is still in flight. [Parameter] public bool Busy { get; set; } /// The caller's existing close/cancel path; also rejects a second simultaneous overlay. [Parameter] public EventCallback OnClose { get; set; } /// Optional nonconfidential editor/confirmation identity; changing it starts a fresh presentation. [Parameter] public object? Target { get; set; } private readonly SemaphoreSlim _lifecycle = new(1, 1); private readonly string _id = "ot-modal-" + Guid.NewGuid().ToString("N"); private DotNetObjectReference? _reference; private bool _active; private long _activeGeneration; private bool _disposed; private bool _lastVisible; private object? _lastTarget; private bool _needsOpen; private bool _closePending; private long _generation; private Task? _disposeTask; protected override void OnParametersSet() { if (_lastVisible == Visible && Equals(_lastTarget, Target)) return; _lastVisible = Visible; _lastTarget = Target; ++_generation; _needsOpen = Visible; _closePending = false; } /// Closes only the current idle presentation, never a reopened target via a stale native event. [JSInvokable] public Task RequestCloseFromDom(long generation) => InvokeAsync(async () => { if (_disposed || !Visible || Busy || _closePending || generation != _generation) return; _closePending = true; try { await OnClose.InvokeAsync(); } finally { if (generation == _generation) _closePending = false; } }); protected override async Task OnAfterRenderAsync(bool firstRender) { await _lifecycle.WaitAsync(); long rejectedGeneration = -1; try { if (_disposed) return; var generation = _generation; if (!Visible) { if (_reference is not null) await JS.InvokeVoidAsync("otDialog.deactivate", _id); _active = false; return; } if (_active && _activeGeneration != generation) { await JS.InvokeVoidAsync("otDialog.deactivate", _id, _activeGeneration); _active = false; } if (_disposed || !Visible || generation != _generation) return; if (!_active || _needsOpen) { _needsOpen = false; _reference ??= DotNetObjectReference.Create(this); var accepted = await JS.InvokeAsync("otDialog.activate", _id, _reference, generation, Busy); // A hide/target switch/dispose can occur while the browser reply is pending. // Clean only that old native generation before a queued render opens the next. if (_disposed || !Visible || generation != _generation) { if (accepted) await JS.InvokeVoidAsync("otDialog.deactivate", _id, generation); return; } _active = accepted; _activeGeneration = generation; if (!accepted) rejectedGeneration = generation; } else await JS.InvokeVoidAsync("otDialog.update", _id, generation, Busy); } catch (JSDisconnectedException) { } catch (JSException) { } catch (InvalidOperationException) { } // Static SSR cannot invoke JavaScript. finally { _lifecycle.Release(); } // Never hold the lifecycle gate while invoking a caller that can render/dispose us. if (rejectedGeneration == _generation) await RequestCloseFromDom(rejectedGeneration); } public ValueTask DisposeAsync() => new(_disposeTask ??= DisposeCoreAsync()); private async Task DisposeCoreAsync() { _disposed = true; ++_generation; await _lifecycle.WaitAsync(); try { if (_reference is not null) await JS.InvokeVoidAsync("otDialog.deactivate", _id); } catch (JSDisconnectedException) { } catch (JSException) { } catch (InvalidOperationException) { } finally { _active = false; _reference?.Dispose(); _lifecycle.Release(); } } }