fix(race-day): keep local state while starting or timing a heat, keep count-down voice on time (1.46.1)

This commit is contained in:
larssand committed 2026-11-01 16:16:46 +01:00
1 parent 8d27072567
commit c4163441fc
8 files changed
+119 -10

No files matched your search

+23 -2
View File
@@ -212,6 +212,8 @@ export function createRaceAudioEngine(options = {}) {
if (currentVoice || !voiceQueue.length) return;
voiceQueue.sort((a, b) => b.priority - a.priority || a.sequence - b.sequence);
const task = voiceQueue.shift();
// A count-down call that could not be said on time is skipped: "8" after the start is wrong.
if (task.expiresAt && now() > task.expiresAt) { task.resolve(false); pumpVoiceQueue(); return; }
currentVoice = task;
Promise.resolve(speak(task.text, { language: task.language, volume: task.volume, interrupt: task.interrupt }))
.catch(() => false).finally(() => {
@@ -225,7 +227,18 @@ export function createRaceAudioEngine(options = {}) {
const voice = await resolveVoice(key, eventOptions);
if (!voice) return false;
return new Promise((resolve) => {
const task = { ...voice, sequence: voiceSequence += 1, resolve };
const group = String(eventOptions.group || "");
const task = { ...voice, group, sequence: voiceSequence += 1, resolve, expiresAt: eventOptions.maxDelayMs ? Number(eventOptions.queuedAt ?? now()) + Number(eventOptions.maxDelayMs) : 0 };
// A newer call in the same group (the count-down) replaces the older one instead of
// queueing behind it, so the voice never falls behind the clock.
if (group) {
dropQueuedGroup(group);
if (currentVoice?.group === group) {
currentVoice.resolve(false);
currentVoice = null;
task.interrupt = true;
}
}
if (task.interrupt && currentVoice && task.priority > currentVoice.priority) {
currentVoice.resolve(false);
currentVoice = null;
@@ -235,6 +248,12 @@ export function createRaceAudioEngine(options = {}) {
});
}
function dropQueuedGroup(group) {
for (let index = voiceQueue.length - 1; index >= 0; index -= 1) {
if (voiceQueue[index].group === group) voiceQueue.splice(index, 1)[0].resolve(false);
}
}
function isCoolingDown(key, eventOptions, definition) {
if (eventOptions.force) return false;
const cooldownMs = Number(eventOptions.cooldownMs ?? definition?.cooldownMs ?? 0);
@@ -251,7 +270,8 @@ export function createRaceAudioEngine(options = {}) {
const definition = EVENT_BY_KEY.get(key);
if (!definition || isCoolingDown(key, eventOptions, definition)) return { effectDone: Promise.resolve(false), voiceDone: Promise.resolve(false) };
const effectDone = playEffect(key, eventOptions);
const voiceDone = effectDone.catch(() => false).then(() => enqueueVoice(key, eventOptions));
const queuedAt = now();
const voiceDone = effectDone.catch(() => false).then(() => enqueueVoice(key, { ...eventOptions, queuedAt }));
try { log({ timestamp: now(), event: key, audioFile: safeEffectPath(eventOptions.effectPath || definition.effect), priority: definition.priority, language: normalizeRaceAudioSettings(getSettings()).language, ...eventOptions.logData }); } catch { /* non-critical */ }
return { effectDone, voiceDone };
}
@@ -336,6 +356,7 @@ export function createRaceAudioEngine(options = {}) {
loadManifest, playEffect, trigger, runStartSequence,
subscribeStartLights(listener) { lightSubscribers.add(listener); listener(lightState, {}); return () => lightSubscribers.delete(listener); },
cancelStartSequence() { startSequenceGeneration += 1; emitLightState("IDLE"); },
dropVoiceGroup(group) { dropQueuedGroup(String(group || "")); },
resetStartLights() { emitLightState("IDLE"); },
getStartLightState: () => lightState,
getStatus: () => ({ loaded: Boolean(manifest), missingEffects: [...missingEffects], queuedVoices: voiceQueue.length, currentVoice: currentVoice?.key || "", lightState }),