Skip to content

Commit

Permalink
code quality: remove timePassed condition
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed May 7, 2024
1 parent 3fea439 commit 139771d
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions frontend/src/components/Preload/Preload.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import * as webAudio from "../../util/webAudio";

// Preload is an experiment screen that continues after a given time or after an audio file has been preloaded
const Preload = ({ sections, playMethod, duration, preloadMessage, pageTitle, onNext }) => {
const [timePassed, setTimePassed] = useState(false);
const [audioAvailable, setAudioAvailable] = useState(false);
const [overtime, setOvertime] = useState(false);
const [loaderDuration, setLoaderDuration] = useState(duration);

const onTimePassed = () => {
setTimePassed(true)
setLoaderDuration(0);
setOvertime(true);
if (audioAvailable) {
Expand All @@ -39,23 +37,15 @@ const Preload = ({ sections, playMethod, duration, preloadMessage, pageTitle, on
// skip Preload if the section has already been loaded in the previous action
if (webAudio.checkSectionLoaded(section)) {
if (index === (sections.length - 1)) {
if (timePassed) {
setAudioAvailable(true);
}
setAudioAvailable(true);
}
return;
}
// Clear buffers if this is the first section
if (index === 0) {
webAudio.clearBuffers();
}

// Load sections in buffer
webAudio.loadBuffer(section.id, section.url, () => {
return webAudio.loadBuffer(section.id, section.url, () => {
if (index === (sections.length - 1)) {
if (timePassed) {
setAudioAvailable(true);
}
setAudioAvailable(true);
}
});
})
Expand All @@ -65,17 +55,18 @@ const Preload = ({ sections, playMethod, duration, preloadMessage, pageTitle, on
}
// Load audio until available
// Return remove listener
return audio.loadUntilAvailable(sections[0].url, () => {
setAudioAvailable(true);
if (timePassed) {
onNext();
return audio.loadUntilAvailable(section.url, () => {
if (index === (sections.length - 1)) {
setAudioAvailable(true);
}
});
}
}

preloadResources();
}, [sections, playMethod, onNext, timePassed]);
preloadResources();
// on destroy, clean up buffers
return webAudio.clearBuffers();
}, [sections, playMethod, onNext]);

return (
<ListenFeedback
Expand Down

0 comments on commit 139771d

Please sign in to comment.