Buffer onload and onprogress only rely on downloadQueue and not currentDownloads count

This commit is contained in:
Yotam Mann 2017-05-21 19:12:14 -07:00
parent 8a692145f6
commit d41c649a5c

View file

@ -162,8 +162,7 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
Tone.prototype.dispose.call(this);
this._buffer = null;
if (this._xhr){
Tone.Buffer._currentDownloads--;
Math.max(Tone.Buffer._currentDownloads, 0);
Tone.Buffer._removeFromDownloadQueue(this._xhr);
this._xhr.abort();
this._xhr = null;
}
@ -377,13 +376,6 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
*/
Tone.Buffer._downloadQueue = [];
/**
* the total number of downloads
* @type {Number}
* @private
*/
Tone.Buffer._currentDownloads = 0;
/**
* A path which is prefixed before every url.
* @type {String}
@ -401,6 +393,17 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
return (new Tone.Buffer()).fromArray(array);
};
/**
* Remove an xhr request from the download queue
* @private
*/
Tone.Buffer._removeFromDownloadQueue = function(request){
var index = Tone.Buffer._downloadQueue.indexOf(request);
if (index !== -1){
Tone.Buffer._downloadQueue.splice(index, 1);
}
};
/**
* Loads a url using XMLHttpRequest.
* @param {String} url
@ -414,6 +417,7 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
onload = onload || Tone.noOp;
function onError(e){
Tone.Buffer._removeFromDownloadQueue(request);
if (onerror){
onerror(e);
Tone.Buffer.emit("error", e);
@ -424,12 +428,14 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
function onProgress(){
//calculate the progress
if (Tone.Buffer._downloadQueue.length > 0){
var totalProgress = 0;
for (var i = 0; i < Tone.Buffer._downloadQueue.length; i++){
totalProgress += Tone.Buffer._downloadQueue[i].progress;
}
Tone.Buffer.emit("progress", totalProgress / Tone.Buffer._downloadQueue.length);
}
}
var request = new XMLHttpRequest();
request.open("GET", Tone.Buffer.baseUrl + url, true);
@ -437,10 +443,10 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
//start out as 0
request.progress = 0;
Tone.Buffer._currentDownloads++;
Tone.Buffer._downloadQueue.push(request);
request.addEventListener("load", function(){
if (request.status === 200){
Tone.context.decodeAudioData(request.response, function(buff) {
@ -448,15 +454,13 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
onProgress();
onload(buff);
Tone.Buffer._currentDownloads--;
if (Tone.Buffer._currentDownloads === 0){
// clear the downloads
Tone.Buffer._downloadQueue = [];
Tone.Buffer._removeFromDownloadQueue(request);
if (Tone.Buffer._downloadQueue.length === 0){
//emit the event at the end
Tone.Buffer.emit("load");
}
}, function(){
Tone.Buffer._removeFromDownloadQueue(request);
onError("Tone.Buffer: could not decode audio data: "+url);
});
} else {
@ -485,9 +489,9 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
*/
Tone.Buffer.cancelDownloads = function(){
Tone.Buffer._downloadQueue.forEach(function(request){
Tone.Buffer._removeFromDownloadQueue(request);
request.abort();
});
Tone.Buffer._currentDownloads = 0;
return Tone.Buffer;
};