Fix local load, add some closures

master
William Toohey 10 years ago
parent 9fe8fd1594
commit 0965d80ed9
  1. 56
      src/js/ResourceManager.js
  2. 14
      src/js/ResourcePack.js

@ -71,7 +71,6 @@ function Resources(core) {
this.remotes = null; this.remotes = null;
this.fileInput = null; this.fileInput = null;
this.fileParseQueue = []; this.fileParseQueue = [];
this.currentlyParsing = false;
if(!core.settings.defaults.noUI) { if(!core.settings.defaults.noUI) {
this.initUI(); this.initUI();
} }
@ -84,14 +83,17 @@ Resources.prototype.addAll = function(urls, progressCallback) {
this.progressCallback = progressCallback; this.progressCallback = progressCallback;
this.progressState = Array.apply(null, Array(urls.length)).map(Number.prototype.valueOf,0); this.progressState = Array.apply(null, Array(urls.length)).map(Number.prototype.valueOf,0);
} }
var r;
var respackPromises = [] var respackPromises = []
for(var i = 0; i < urls.length; i++) { for(var i = 0; i < urls.length; i++) {
var r = new Respack(); r = new Respack();
respackPromises.push(r.loadFromURL(urls[i], function(index, progress, pack) { (function(r) {
this.progressState[index] = progress; respackPromises.push(r.loadFromURL(urls[i], function(index, progress, pack) {
this.updateProgress(pack); this.progressState[index] = progress;
}.bind(this, i))); this.updateProgress(pack);
}.bind(this, i)));
})(r);
} }
// Start all the promises at once, but add in sequence // Start all the promises at once, but add in sequence
return respackPromises.reduce((sequence, packPromise) => { return respackPromises.reduce((sequence, packPromise) => {
@ -217,33 +219,27 @@ Resources.prototype.getSongNames = function() {
Resources.prototype.loadLocal = function() { Resources.prototype.loadLocal = function() {
console.log("Loading local zip(s)"); console.log("Loading local zip(s)");
var r;
var files = this.fileInput.files; var files = this.fileInput.files;
var p = Promise.resolve();
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
this.fileParseQueue.push(files[i]); r = new Respack();
} // closure required
this.parseLocalQueue(); ((file, r) => {
}; p = p.then(() => {
return r.loadFromBlob(file, (progress, respack) => {
Resources.prototype.parseLocalQueue = function(recursing) { this.localProgress(progress, respack);
// avoid race conditions });
if(this.currentlyParsing && !recursing) { }).then(pack => {
return; this.addPack(pack);
}
this.currentlyParsing = true;
if(this.fileParseQueue.length) {
var r = new Respack();
r.loadBlob(this.fileParseQueue.shift(),
() => {
this.addPack(r);
this.localComplete(); this.localComplete();
this.parseLocalQueue(true); });
}, })(files[i], r);
(progress, respack) => {this.localProgress(progress, respack);},
() => {this.parseLocalQueue(true);});
} else {
console.log("Local respack parsing complete");
this.currentlyParsing = false;
} }
return p.then(() => {
console.log("Local respack parsing complete");
});
}; };
Resources.prototype.localProgress = function(progress, respack) { Resources.prototype.localProgress = function(progress, respack) {
@ -307,7 +303,7 @@ Resources.prototype.initUI = function() {
var loadLocal = document.createElement("div"); var loadLocal = document.createElement("div");
loadLocal.className = "hues-button"; loadLocal.className = "hues-button";
loadLocal.textContent = "LOAD ZIPS"; loadLocal.textContent = "LOAD ZIPS";
loadLocal.onclick = () => {this.fileInput.click}; loadLocal.onclick = () => {this.fileInput.click()};
buttons.appendChild(loadLocal); buttons.appendChild(loadLocal);
buttons.appendChild(loadRemote); buttons.appendChild(loadRemote);
this.packsView.loadRemote = loadRemote; this.packsView.loadRemote = loadRemote;

@ -79,11 +79,7 @@ Respack.prototype.loadFromURL = function(url, progress) {
return this.getBlob(url) return this.getBlob(url)
.then(response => { .then(response => {
return this.loadBlob(response); return this.loadFromBlob(response);
}).then(zip => {
return this.parseZip(zip);
}).then(() => {
return this;
}); });
}; };
@ -118,7 +114,7 @@ Respack.prototype.getBlob = function(url, progress) {
}); });
} }
Respack.prototype.loadBlob = function(blob, progress) { Respack.prototype.loadFromBlob = function(blob, progress) {
if(progress) { if(progress) {
this.progressCallback = progress; this.progressCallback = progress;
} }
@ -133,7 +129,11 @@ Respack.prototype.loadBlob = function(blob, progress) {
reject(Error("Respack error:", error.toString())); reject(Error("Respack error:", error.toString()));
} }
); );
}); }).then(zip => {
return this.parseZip(zip);
}).then(() => {
return this;
});;
}; };
Respack.prototype.parseZip = function(zip) { Respack.prototype.parseZip = function(zip) {

Loading…
Cancel
Save