fix(adminui): Monaco text syncs on demand, diagnostics are sequenced, Problems list (D1)

R04:P1 - the editor no longer ships its full text JS->.NET on every keystroke. It reports
at blur, and ScriptEdit / the VirtualTag modal read the authoritative value with
MonacoEditor.GetValueAsync at Save (validation runs after the pull). The Blazor hub's
MaximumReceiveMessageSize is raised from 32 KB to 1 MB. The JS refuses a value that
would not fit instead of sending an oversize frame, and Save surfaces that refusal
rather than persisting stale source.

R04:P9 - diagnostics carry a generation counter and an AbortController. Every edit aborts
the in-flight request, and only the response for the current text is applied.

R04:S2 - the always-empty inlay-hints provider, endpoint, service method and contracts
are removed.

R04:S3 - MarkersChanged is typed (DiagnosticMarker) and feeds a new ScriptProblemsList.
Clicking a problem calls RevealLineAsync. Markers cross to .NET only when a list
subscribes.

Refs #745

Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
Joseph Doherty
2026-10-22 17:38:56 -04:00
parent 8e3bbf91fb
commit da2578833d
14 changed files with 514 additions and 61 deletions
@@ -112,25 +112,6 @@
} catch (e) { return []; }
}
});
monaco.languages.registerInlayHintsProvider("csharp", {
provideInlayHints: async function (model) {
try {
const resp = await fetch("/api/script-analysis/inlay-hints", {
method: "POST", credentials: "same-origin",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ code: model.getValue() })
});
if (!resp.ok) return { hints: [], dispose: function () {} };
const data = await resp.json();
return {
hints: (data.hints || []).map(function (h) {
return { position: { lineNumber: h.line, column: h.column }, label: h.label, kind: 2, paddingRight: true };
}),
dispose: function () {}
};
} catch (e) { return { hints: [], dispose: function () {} }; }
}
});
monaco.languages.registerSignatureHelpProvider("csharp", {
signatureHelpTriggerCharacters: ["(", ","],
signatureHelpRetriggerCharacters: [","],
@@ -156,17 +137,41 @@
});
}
async function fetchDiagnostics(model) {
// Resolves to the marker array, or null when the request was aborted (superseded by a newer edit) — the
// caller must then leave the markers alone. A server/network failure still resolves to [] (clears markers).
async function fetchDiagnostics(model, signal) {
try {
const resp = await fetch("/api/script-analysis/diagnostics", {
method: "POST", credentials: "same-origin",
method: "POST", credentials: "same-origin", signal: signal,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ code: model.getValue(), equipmentId: equipmentIdOf(model) })
});
if (!resp.ok) return [];
const data = await resp.json();
return data.markers || [];
} catch (e) { return []; }
} catch (e) { return (signal && signal.aborted) ? null : []; }
}
// The editor text crosses the Blazor circuit (JS -> .NET) only on demand — at blur and when the page saves —
// never per keystroke. Each crossing is one SignalR message, bounded by the hub's MaximumReceiveMessageSize;
// an oversize message would tear the circuit down, so a value whose serialized form would not fit is refused
// here instead (getValue throws a descriptive error that .NET surfaces to the author).
// Blazor carries an interop value as a JSON string inside the hub frame; measuring it escaped twice is a
// conservative upper bound whichever hub protocol (BlazorPack or JSON) the circuit negotiated.
function serializedBytes(v) { return new TextEncoder().encode(JSON.stringify(JSON.stringify(v))).length; }
function tooLargeMessage(e, v) {
if (!e.maxSyncBytes) return null;
const bytes = serializedBytes(v);
return bytes > e.maxSyncBytes
? "The script is too large for the editor to send to the server (" + bytes + " bytes; the limit is " + e.maxSyncBytes + " bytes)."
: null;
}
function syncValue(e) {
if (!e.dirty) return;
const v = e.editor.getValue();
if (tooLargeMessage(e, v)) return; // stays dirty; the explicit save read reports the error
e.dirty = false;
e.dotNetRef.invokeMethodAsync("OnValueChanged", v).catch(function () { e.dirty = true; });
}
// A single body-level node for Monaco's overflow widgets (suggest / hover / parameter hints).
@@ -207,22 +212,38 @@
// Stamp the owning-equipment id on the model so the global csharp providers can thread it into the
// {{equip}}/<RefName> completion + diagnostics (null on the shared ScriptEdit page).
try { const m0 = editor.getModel(); if (m0) m0.__otEquipmentId = options.equipmentId || null; } catch (x) {}
let diagTimer = null;
const scheduleDiagnostics = function () {
if (diagTimer) clearTimeout(diagTimer);
diagTimer = setTimeout(async function () {
const model = editor.getModel(); if (!model) return;
const markers = await fetchDiagnostics(model);
monaco.editor.setModelMarkers(model, "otopcua", markers);
dotNetRef.invokeMethodAsync("OnMarkersChanged", markers).catch(function () {});
}, 500);
const e = {
editor: editor, dotNetRef: dotNetRef, dirty: false,
maxSyncBytes: options.maxSyncBytes || 0, reportMarkers: !!options.reportMarkers,
diagTimer: null, diagGeneration: 0, diagAbort: null
};
// Diagnostics: the 500 ms debounce delays the START of a request; the generation counter + AbortController
// make sure only the response for the CURRENT text is applied. Every edit bumps the generation and aborts
// the in-flight request, so a slow older response can never overwrite newer markers.
const runDiagnostics = async function () {
const model = editor.getModel(); if (!model) return;
const generation = ++e.diagGeneration;
const ac = new AbortController();
e.diagAbort = ac;
const markers = await fetchDiagnostics(model, ac.signal);
if (markers === null || generation !== e.diagGeneration || editors[id] !== e) return; // superseded
e.diagAbort = null;
monaco.editor.setModelMarkers(model, "otopcua", markers);
if (e.reportMarkers) dotNetRef.invokeMethodAsync("OnMarkersChanged", markers).catch(function () {});
};
e.scheduleDiagnostics = function () {
e.diagGeneration++;
if (e.diagAbort) { e.diagAbort.abort(); e.diagAbort = null; }
if (e.diagTimer) clearTimeout(e.diagTimer);
e.diagTimer = setTimeout(runDiagnostics, 500);
};
editor.onDidChangeModelContent(function () {
dotNetRef.invokeMethodAsync("OnValueChanged", editor.getValue()).catch(function () {});
if (options.language === "csharp") scheduleDiagnostics();
e.dirty = true;
if (options.language === "csharp") e.scheduleDiagnostics();
});
editors[id] = { editor: editor, dotNetRef: dotNetRef };
if (options.language === "csharp") scheduleDiagnostics();
editor.onDidBlurEditorText(function () { syncValue(e); });
editors[id] = e;
if (options.language === "csharp") e.scheduleDiagnostics();
}
function setEditorOption(id, name, value) {
const e = editors[id]; if (!e) return;
@@ -235,10 +256,28 @@
const e = editors[id]; if (!e) return;
e.editor.revealLineInCenter(line); e.editor.setPosition({ lineNumber: line, column: col || 1 }); e.editor.focus();
}
function setValue(id, v) { const e = editors[id]; if (e && e.editor.getValue() !== v) e.editor.setValue(v || ""); }
function getValue(id) { const e = editors[id]; return e ? e.editor.getValue() : null; }
function setValue(id, v) {
const e = editors[id]; if (!e) return;
if (e.editor.getValue() !== v) e.editor.setValue(v || "");
e.dirty = false; // the text now equals what .NET holds
}
// Reads the current text on demand (Save). Throws when it would not fit in one circuit message.
function getValue(id) {
const e = editors[id]; if (!e) return null;
const v = e.editor.getValue();
const err = tooLargeMessage(e, v);
if (err) throw new Error(err);
e.dirty = false;
return v;
}
function setMarkers(id, m) { const e = editors[id]; const model = e && e.editor.getModel(); if (model) monaco.editor.setModelMarkers(model, "otopcua", m || []); }
function dispose(id) { const e = editors[id]; if (!e) return; try { e.editor.dispose(); } catch (x) {} delete editors[id]; }
function dispose(id) {
const e = editors[id]; if (!e) return;
if (e.diagTimer) clearTimeout(e.diagTimer);
if (e.diagAbort) e.diagAbort.abort();
try { e.editor.dispose(); } catch (x) {}
delete editors[id];
}
window.MonacoBlazor = { createEditor, setValue, getValue, setMarkers, setEditorOption, format, revealLine, dispose };
})();