@page "/scripts" @attribute [Authorize(Policy = AdminUiPolicies.ConfigEditor)] @rendermode RenderMode.InteractiveServer @using Microsoft.EntityFrameworkCore @using ZB.MOM.WW.OtOpcUa.Configuration @using ZB.MOM.WW.OtOpcUa.AdminUI.ScriptList @inject IDbContextFactory DbFactory @implements IDisposable Scripts · OtOpcUa

Scripts

New script
@if (_rows is null) {

Loading…

} else {

@_rows.Count script@(_rows.Count == 1 ? "" : "s")

@if (_rows.Count == 0) {
No scripts defined.
} else {
@foreach (var s in _rows) { }
Script Actions
@(string.IsNullOrWhiteSpace(s.Name) ? s.ScriptId : s.Name) @s.Language
@s.ScriptId · hash=@DisplayText.Abbreviate(s.SourceHash, 12)
Edit
}
} @* The dialog's EXISTENCE is gated on the held id (operator intent); the resolve drives only its CONTENT. Gating existence on FindSourceScript() would make the modal a function of _rows, and any render where the row is momentarily unresolvable (a future reload/poll setting _rows null or replacing the list) would unmount the subtree and dispose the Close handler's binding id — a click already in flight then throws GetRequiredEventBindingEntry. Latent today (_rows is written once, in OnInitializedAsync) but the shape bit ScadaBridge live the moment a refresh landed. *@ @{ var sourceId = _sourceScriptId; var sourceGeneration = _sourceGeneration; var src = FindSourceScript(); var sourceTitle = src is null ? sourceId ?? "Script source" : string.IsNullOrWhiteSpace(src.Name) ? src.ScriptId : src.Name; } @sourceTitle @if (src is not null) { @src.ScriptId } @if (_sourceError is not null) { } else if (src is null || (_sourceLoaded && _sourceCode is null)) {

This script is no longer loaded.

} else if (!_sourceLoaded) {

Loading source…

} else {
@_sourceCode
}
@code { // A projection without SourceCode (R04 P8); the source loads per dialog open. private List? _rows; // Only the id of the script whose source dialog is open — never the record itself, so a reload // that drops or replaces the row cannot leave a stale object on screen. private string? _sourceScriptId; // The open dialog's source, fetched on open. _sourceGeneration discards a superseded fetch (the // operator opened another row, or closed the dialog, while the first one was in flight). private string? _sourceCode; private bool _sourceLoaded; private string? _sourceError; private int _sourceGeneration; private bool _disposed; private bool CurrentSource(int generation, string? scriptId) => !_disposed && scriptId is not null && generation == _sourceGeneration && scriptId == _sourceScriptId; protected override async Task OnInitializedAsync() { await using var db = await DbFactory.CreateDbContextAsync(); var rows = await ScriptListQueries.LoadRowsAsync(db); if (!_disposed) _rows = rows; } /// Re-resolves the open dialog's script from the current list on every render. Drives /// the dialog's CONTENT only — never its existence (see the comment at the markup): a script /// that is no longer loaded renders a notice with the frame and Close still mounted. private ScriptListRow? FindSourceScript() => _sourceScriptId is null ? null : _rows?.FirstOrDefault(s => s.ScriptId == _sourceScriptId); // One dialog at a time: opening another row replaces the id. private async Task ShowSource(string scriptId) { if (_disposed) return; var generation = ++_sourceGeneration; _sourceScriptId = scriptId; _sourceCode = null; _sourceLoaded = false; _sourceError = null; string? source; try { await using var db = await DbFactory.CreateDbContextAsync(); source = await ScriptListQueries.LoadSourceAsync(db, scriptId); } catch (Exception ex) { // A ConfigDb hiccup must not escape the click handler into the page's error boundary (review r0 M7). if (!CurrentSource(generation, scriptId)) return; _sourceError = $"The script source could not be loaded: {ex.Message}"; _sourceLoaded = true; return; } if (!CurrentSource(generation, scriptId)) return; _sourceCode = source; _sourceLoaded = true; } private void CloseSource(int generation, string? scriptId) { if (!CurrentSource(generation, scriptId)) return; ClearSource(); } private void ClearSource() { _sourceGeneration++; _sourceScriptId = null; _sourceCode = null; _sourceLoaded = false; _sourceError = null; } /// Retires pending read results and captured close handlers without changing the database query. public void Dispose() { _disposed = true; ClearSource(); } }