Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not call onNext() from within map #1003

Merged
merged 4 commits into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 18 additions & 23 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 @@ -34,26 +32,20 @@ const Preload = ({ sections, playMethod, duration, preloadMessage, pageTitle, on
}

if (playMethod === 'BUFFER') {

// Use Web-audio and preload sections in buffers
sections.map((section, index) => {
sections.forEach((section, index) => {
// skip Preload if the section has already been loaded in the previous action
if (webAudio.checkSectionLoaded(section)) {
onNext();
return undefined;
}
// Clear buffers if this is the first section
if (index === 0) {
webAudio.clearBuffers();
if (index === (sections.length - 1)) {
setAudioAvailable(true);
}
return;
}

// Load sections in buffer
return webAudio.loadBuffer(section.id, section.url, () => {
if (index === (sections.length - 1)) {
if (timePassed) {
setAudioAvailable(true);
onNext();
}
setAudioAvailable(true);
}
});
})
Expand All @@ -62,18 +54,21 @@ const Preload = ({ sections, playMethod, duration, preloadMessage, pageTitle, on
webAudio.closeWebAudio();
}
// Load audio until available
// Return remove listener
return audio.loadUntilAvailable(sections[0].url, () => {
setAudioAvailable(true);
if (timePassed) {
onNext();
}
});
// Return remove listener
sections.forEach((section, index) => {
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
Loading