Use getLazyAsset for preload

This commit is contained in:
Varun Patil 2020-10-20 21:11:06 +05:30
parent 34c58d5439
commit 262a2254b6
1 changed files with 31 additions and 25 deletions

View File

@ -172,11 +172,15 @@ function preloadList(jsonArray) {
const filename = mappingValue.substring(mappingValue.lastIndexOf("/") + 1).split("?")[0]; const filename = mappingValue.substring(mappingValue.lastIndexOf("/") + 1).split("?")[0];
// Preload the asset // Preload the asset
FS.createPreloadedFile(path, filename, "gameasync/" + mappingValue, true, true, function() { getLazyAsset("gameasync/" + mappingValue, filename, (data) => {
if (!data) return;
FS.createPreloadedFile(path, filename, new Uint8Array(data), true, true, function() {
window.fileAsyncCache[mappingKey] = 1; window.fileAsyncCache[mappingKey] = 1;
}, console.error, false, false, () => { }, console.error, false, false, () => {
try { FS.unlink(path + "/" + filename); } catch (err) {} try { FS.unlink(path + "/" + filename); } catch (err) {}
}); });
}, true);
}); });
} }
@ -196,28 +200,36 @@ window.fileLoadedAsync = function(file) {
}); });
}; };
var hideTimer = 0; var activeStreams = [];
function getLazyAsset(url, filename, callback) { function getLazyAsset(url, filename, callback, noretry) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.responseType = "arraybuffer"; xhr.responseType = "arraybuffer";
const pdiv = document.getElementById("progress"); const pdiv = document.getElementById("progress");
let showTimer = 0;
let abortTimer = 0; let abortTimer = 0;
const end = (message) => {
pdiv.innerHTML = `${filename} - ${message}`;
activeStreams.splice(activeStreams.indexOf(filename), 1);
if (activeStreams.length === 0) {
pdiv.style.opacity = '0';
}
clearTimeout(abortTimer);
}
const retry = () => { const retry = () => {
xhr.abort(); xhr.abort();
if (noretry) {
end('skip'); callback(null);
} else {
activeStreams.splice(activeStreams.indexOf(filename), 1);
getLazyAsset(url, filename, callback); getLazyAsset(url, filename, callback);
} }
}
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status >= 200 && xhr.status < 400) { if (xhr.readyState == XMLHttpRequest.DONE && xhr.status >= 200 && xhr.status < 400) {
pdiv.innerHTML = `${filename} - done`; end('done');
hideTimer = setTimeout(() => {
pdiv.style.opacity = '0';
hideTimer = 0;
}, 500);
clearTimeout(showTimer);
clearTimeout(abortTimer);
callback(xhr.response); callback(xhr.response);
} }
} }
@ -232,16 +244,10 @@ function getLazyAsset(url, filename, callback) {
xhr.open('GET', url); xhr.open('GET', url);
xhr.send(); xhr.send();
pdiv.innerHTML = `${filename} - starting`; pdiv.innerHTML = `${filename} - start`;
showTimer = setTimeout(() => {
pdiv.style.opacity = '0.5'; pdiv.style.opacity = '0.5';
}, 100);
activeStreams.push(filename);
abortTimer = setTimeout(retry, 10000); abortTimer = setTimeout(retry, 10000);
if (hideTimer) {
clearTimeout(hideTimer);
hideTimer = 0;
}
} }