Skip to content

Commit

Permalink
feat(main.js): add async/await for better error handling and readability
Browse files Browse the repository at this point in the history
This commit refactors main.js to use async/await for better error handling and readability. It specifically modifies the initiateAudioPlay, sendRequest, checkAudioStatus, and playAudioWhenReady functions to use async/await. It also adds error handling to fetch calls in the checkAudioStatus and initiateAudioPlay functions.
  • Loading branch information
IRedDragonICY committed May 25, 2024
1 parent 803340a commit 937d69e
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let model, video, audioPlaying = false, isProcessing = false, audioLoopRunning =
startBlinking();
setupVideo();
setupAudio();
initiateAudioPlay();
await initiateAudioPlay();
})();

function setupVideo() {
Expand Down Expand Up @@ -45,28 +45,29 @@ function sendFrameToServer(blob) {
const formData = new FormData();
formData.append('image', blob, 'frame.jpg');

const sendRequest = (retryCount = 0) => {
fetch('/api/upload_frame', { method: 'POST', body: formData })
.then(response => console.log('Frame uploaded:', response))
.catch(error => {
console.error('Failed to upload frame:', error);
if (retryCount < 3) {
setTimeout(() => sendRequest(retryCount + 1), 2000);
}
});
const sendRequest = async (retryCount = 0) => {
try {
const response = await fetch('/api/upload_frame', { method: 'POST', body: formData });
console.log('Frame uploaded:', response);
} catch (error) {
console.error('Failed to upload frame:', error);
if (retryCount < 3) {
setTimeout(() => sendRequest(retryCount + 1), 2000);
}
}
};

sendRequest();
}

async function initiateAudioPlay() {
if (!audioPlaying && !audioLoopRunning) playAudioWhenReady();
if (!audioPlaying && !audioLoopRunning) await playAudioWhenReady();
}

async function playAudioWhenReady() {
audioLoopRunning = true;
while (!(await checkAudioStatus())) {
await new Promise(resolve => setTimeout(resolve, 500)); // Reduce wait time
await new Promise(resolve => setTimeout(resolve, 500));
}
audioPlaying = true;
const audio = new Audio(audio_link);
Expand All @@ -76,9 +77,14 @@ async function playAudioWhenReady() {
}

async function checkAudioStatus() {
const response = await fetch('/api/audio_status');
const data = await response.json();
return data.audio_ready;
try {
const response = await fetch('/api/audio_status');
const data = await response.json();
return data.audio_ready;
} catch (error) {
console.error('Failed to check audio status:', error);
return false;
}
}

function setupAnalyser(audio) {
Expand All @@ -92,7 +98,11 @@ function setupAnalyser(audio) {
analyseVolume(analyser, dataArray, audio);

audio.onended = async () => {
await fetch('/api/reset_audio_status', { method: 'POST' });
try {
await fetch('/api/reset_audio_status', { method: 'POST' });
} catch (error) {
console.error('Failed to reset audio status:', error);
}
audioPlaying = false;
if (!isProcessing) recognition.start();
};
Expand Down

0 comments on commit 937d69e

Please sign in to comment.