Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Abort loading audio files if show has been unloaded or reset #12
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchirls committed Mar 6, 2017
1 parent c5033e6 commit dc2d506
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/js/puppet-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,29 @@ function PuppetShow(options) {

audioAssets.set(audioId, audioObject);

/*
Abort at each step if audio asset is no longer valid
*/

function isInvalid() {
return id !== showId || !audioAssets.has(audioId);
}

const audioFileRef = audioAssetsRef.child(audioId + '.wav');
audioFileRef.getDownloadURL().then(url => {
// todo: load this as a SoundEffect?
// todo: adjust playable state?

if (id !== showId) {
// we've since unloaded this show
if (isInvalid()) {
return;
}

const xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
if (isInvalid()) {
return;
}
audioContext.decodeAudioData(xhr.response, decodedBuffer => {
audioObject.buffer = decodedBuffer;
console.log('loaded buffer', url, decodedBuffer);
Expand Down Expand Up @@ -271,11 +281,11 @@ function PuppetShow(options) {
// todo: Decode and add to audioObject.buffer
const fileReader = new FileReader();
fileReader.onloadend = () => {
if (!audioAssets.has(id)) {
// in case asset has been removed
return;
}
audioContext.decodeAudioData(fileReader.result).then(decodedData => {
if (!audioAssets.has(id)) {
// in case asset has been removed
return;
}

audioObject.buffer = decodedData;
// todo: set up audio source or whatever
Expand Down

0 comments on commit dc2d506

Please sign in to comment.