From d41c649a5c28d8454f39282cbdb5a5e53ef122da Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Sun, 21 May 2017 19:12:14 -0700 Subject: [PATCH] Buffer onload and onprogress only rely on downloadQueue and not currentDownloads count --- Tone/core/Buffer.js | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/Tone/core/Buffer.js b/Tone/core/Buffer.js index 9ab8ea0d..2382d43a 100644 --- a/Tone/core/Buffer.js +++ b/Tone/core/Buffer.js @@ -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,11 +428,13 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone) function onProgress(){ //calculate the progress - var totalProgress = 0; - for (var i = 0; i < Tone.Buffer._downloadQueue.length; i++){ - totalProgress += Tone.Buffer._downloadQueue[i].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); } - Tone.Buffer.emit("progress", totalProgress / Tone.Buffer._downloadQueue.length); } var request = new XMLHttpRequest(); @@ -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; };