From 323dadfe29a020bfc45feb347ab51b4c8cd03e69 Mon Sep 17 00:00:00 2001 From: Matt Flax Date: Sun, 11 May 2025 20:06:56 +1000 Subject: [PATCH] SpeechToText js : merge stop event handlers --- aider/gui_speech_to_text.js | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/aider/gui_speech_to_text.js b/aider/gui_speech_to_text.js index 91a9dd250..436628de7 100644 --- a/aider/gui_speech_to_text.js +++ b/aider/gui_speech_to_text.js @@ -94,22 +94,27 @@ recognition.start(); }); - recognition.onresult = function(event) { - const transcript = event.results[0][0].transcript; + // Combined event handler function for speech recognition events + function handleSpeechEvent(eventType, event) { + // Set LED back to gray for all events led.style.backgroundColor = 'gray'; - // Try to populate the chat input directly - const success = populateChatInput(transcript); - if (!success) - console.error('populateChatInput failed'); - }; + if (eventType === 'result') { + const transcript = event.results[0][0].transcript; + + // Try to populate the chat input directly + const success = populateChatInput(transcript); + if (!success) + console.error('populateChatInput failed'); + } + else if (eventType === 'error') { + console.error('Speech recognition error', event.error); + } + // 'end' event requires no special handling beyond resetting the LED + } - recognition.onerror = function(event) { - console.error('Speech recognition error', event.error); - led.style.backgroundColor = 'gray'; - }; - - recognition.onend = function() { - led.style.backgroundColor = 'gray'; - }; + // Set up event handlers using the combined function + recognition.onresult = function(event) { handleSpeechEvent('result', event); }; + recognition.onerror = function(event) { handleSpeechEvent('error', event); }; + recognition.onend = function() { handleSpeechEvent('end'); }; })();