[fix](trx-frontend-http): guard CW auto UI against concurrent SSE updates
Add cwAutoLocalOverride flag in cw.js to block server-state snapshots from overriding the checkbox while a user-initiated POST is in-flight. Expose applyCwAutoUiFromServer for app.js render() to call instead of applyCwAutoUi, preventing a racing SSE event carrying the old cw_auto value from immediately undoing the user's toggle. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Stan Grams <sjg@haxx.space>
This commit is contained in:
@@ -21,6 +21,11 @@ let cwLastAppendTime = 0;
|
||||
let cwTonePickerRaf = null;
|
||||
let cwPaused = false;
|
||||
let cwBufferedWhilePaused = 0;
|
||||
// Tracks a user-initiated auto toggle that is in-flight (POST not yet
|
||||
// acknowledged). While set, server-state updates must not override the
|
||||
// checkbox so that a concurrent SSE event carrying the *old* cw_auto value
|
||||
// does not immediately undo the user's choice.
|
||||
let cwAutoLocalOverride = null;
|
||||
|
||||
function applyCwAutoUi(enabled) {
|
||||
if (cwAutoInput) cwAutoInput.checked = enabled;
|
||||
@@ -38,6 +43,13 @@ function applyCwAutoUi(enabled) {
|
||||
}
|
||||
window.applyCwAutoUi = applyCwAutoUi;
|
||||
|
||||
// Called by app.js render() when a server-state snapshot arrives. Ignores
|
||||
// the update while cwAutoLocalOverride is set (user change still in-flight).
|
||||
window.applyCwAutoUiFromServer = function(enabled) {
|
||||
if (cwAutoLocalOverride !== null) return;
|
||||
applyCwAutoUi(enabled);
|
||||
};
|
||||
|
||||
function clampCwWpm(wpm) {
|
||||
const numeric = Number(wpm);
|
||||
if (!Number.isFinite(numeric)) return 15;
|
||||
@@ -232,12 +244,15 @@ async function setCwTone(tone, { syncInput = true } = {}) {
|
||||
if (cwAutoInput) {
|
||||
cwAutoInput.addEventListener("change", async () => {
|
||||
const enabled = cwAutoInput.checked;
|
||||
cwAutoLocalOverride = enabled;
|
||||
applyCwAutoUi(enabled);
|
||||
try {
|
||||
await postPath(`/set_cw_auto?enabled=${enabled ? "true" : "false"}`);
|
||||
drawCwTonePicker();
|
||||
} catch (e) {
|
||||
console.error("CW auto toggle failed", e);
|
||||
} finally {
|
||||
cwAutoLocalOverride = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user