Fix the speak status tracker and null option handling

This commit is contained in:
Fisto
2026-09-21 18:00:41 +02:00
parent 7701664bc6
commit 15901849a1
3 changed files with 64 additions and 17 deletions
+40
View File
@@ -1715,6 +1715,45 @@ function fillDeviceSelect(select, devices, defaultLabel) {
if ([...select.options].some(option => option.value === current)) select.value = current;
}
// The daemon plays asynchronously; keep the panel honest about queued vs speaking vs done.
let speechTracker = 0;
async function trackSpeech() {
const token = ++speechTracker;
for (let attempt = 0; attempt < 1200; attempt++) {
await new Promise(resolve => setTimeout(resolve, 500));
if (token !== speechTracker) return;
try {
const status = await VK.api.get("/api/audio");
if (token !== speechTracker) return;
const label = $("#tts-status");
if (status?.playing === true) {
label.textContent = "speaking\u2026";
continue;
}
const queue = Number(status?.queue ?? 0);
if (queue > 0) {
label.textContent = `queued (${queue})`;
continue;
}
label.textContent = "done";
setTimeout(() => {
if (token === speechTracker && $("#tts-status").textContent === "done") {
$("#tts-status").textContent = "idle";
}
}, 4000);
return;
} catch {
return;
}
}
}
async function refreshAudio() {
try {
const status = await VK.api.get("/api/audio");
@@ -2344,6 +2383,7 @@ async function speakText() {
$("#tts-status").textContent = "queued";
toast("Queued for speech", "ok");
void refreshAudio();
void trackSpeech();
} catch (error) {
toast(error.message, "error");
}