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

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

Loading…
Cancel
Save