Skip to content

Commit

Permalink
resolves katspaugh#660; cancel ajax on destroy/empty
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Feb 29, 2016
1 parent 28a8143 commit a4645a9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/wavesurfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ var WaveSurfer = {
// Used to save the current volume when muting so we can
// restore once unmuted
this.savedVolume = 0;

// The current muted state
this.isMuted = false;

// Will hold a list of event descriptors that need to be
// cancelled on subsequent loads of audio
this.tmpEvents = [];

// Holds any running audio downloads
this.currentAjax = null;

this.createDrawer();
this.createBackend();
},
Expand Down Expand Up @@ -390,19 +395,28 @@ var WaveSurfer = {

getArrayBuffer: function (url, callback) {
var my = this;

var ajax = WaveSurfer.util.ajax({
url: url,
responseType: 'arraybuffer'
});

this.currentAjax = ajax;

this.tmpEvents.push(
ajax.on('progress', function (e) {
my.onProgress(e);
}),
ajax.on('success', callback),
ajax.on('success', function (data, e) {
callback(data);
my.currentAjax = null;
}),
ajax.on('error', function (e) {
my.fireEvent('error', 'XHR error: ' + e.target.statusText);
my.currentAjax = null;
})
);

return ajax;
},

Expand Down Expand Up @@ -436,6 +450,13 @@ var WaveSurfer = {
return json;
},

cancelAjax: function () {
if (this.currentAjax) {
this.currentAjax.xhr.abort();
this.currentAjax = null;
}
},

clearTmpEvents: function () {
this.tmpEvents.forEach(function (e) { e.un(); });
},
Expand All @@ -448,6 +469,7 @@ var WaveSurfer = {
this.stop();
this.backend.disconnectSource();
}
this.cancelAjax();
this.clearTmpEvents();
this.drawer.progress(0);
this.drawer.setWidth(0);
Expand All @@ -459,6 +481,7 @@ var WaveSurfer = {
*/
destroy: function () {
this.fireEvent('destroy');
this.cancelAjax();
this.clearTmpEvents();
this.unAll();
this.backend.destroy();
Expand Down

0 comments on commit a4645a9

Please sign in to comment.