2020-11-23 13:27:13 +01:00
|
|
|
const Preloader = /** @constructor */ function () { // eslint-disable-line no-unused-vars
|
2021-03-01 11:26:22 +01:00
|
|
|
function getTrackedResponse(response, load_status) {
|
|
|
|
function onloadprogress(reader, controller) {
|
|
|
|
return reader.read().then(function (result) {
|
|
|
|
if (load_status.done) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
if (result.value) {
|
|
|
|
controller.enqueue(result.value);
|
|
|
|
load_status.loaded += result.value.length;
|
|
|
|
}
|
|
|
|
if (!result.done) {
|
|
|
|
return onloadprogress(reader, controller);
|
|
|
|
}
|
|
|
|
load_status.done = true;
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const reader = response.body.getReader();
|
|
|
|
return new Response(new ReadableStream({
|
|
|
|
start: function (controller) {
|
|
|
|
onloadprogress(reader, controller).then(function () {
|
|
|
|
controller.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
}), { headers: response.headers });
|
|
|
|
}
|
|
|
|
|
2021-03-01 17:53:56 +01:00
|
|
|
function loadFetch(file, tracker, fileSize, raw) {
|
2020-11-19 16:54:07 +01:00
|
|
|
tracker[file] = {
|
2021-03-01 17:53:56 +01:00
|
|
|
total: fileSize || 0,
|
2020-11-19 16:54:07 +01:00
|
|
|
loaded: 0,
|
2021-03-01 11:26:22 +01:00
|
|
|
done: false,
|
2020-11-19 16:54:07 +01:00
|
|
|
};
|
2021-03-01 11:26:22 +01:00
|
|
|
return fetch(file).then(function (response) {
|
|
|
|
if (!response.ok) {
|
|
|
|
return Promise.reject(new Error(`Failed loading file '${file}'`));
|
|
|
|
}
|
|
|
|
const tr = getTrackedResponse(response, tracker[file]);
|
|
|
|
if (raw) {
|
|
|
|
return Promise.resolve(tr);
|
|
|
|
}
|
|
|
|
return tr.arrayBuffer();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function retry(func, attempts = 1) {
|
|
|
|
function onerror(err) {
|
2020-11-19 16:54:07 +01:00
|
|
|
if (attempts <= 1) {
|
2021-03-01 11:26:22 +01:00
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
|
|
|
return new Promise(function (resolve, reject) {
|
2020-11-19 16:54:07 +01:00
|
|
|
setTimeout(function () {
|
2021-03-01 11:26:22 +01:00
|
|
|
retry(func, attempts - 1).then(resolve).catch(reject);
|
2020-11-19 16:54:07 +01:00
|
|
|
}, 1000);
|
2021-03-01 11:26:22 +01:00
|
|
|
});
|
2020-11-19 16:54:07 +01:00
|
|
|
}
|
2021-03-01 11:26:22 +01:00
|
|
|
return func().catch(onerror);
|
|
|
|
}
|
2020-11-19 16:54:07 +01:00
|
|
|
|
|
|
|
const DOWNLOAD_ATTEMPTS_MAX = 4;
|
|
|
|
const loadingFiles = {};
|
|
|
|
const lastProgress = { loaded: 0, total: 0 };
|
|
|
|
let progressFunc = null;
|
|
|
|
|
2020-11-23 13:27:13 +01:00
|
|
|
const animateProgress = function () {
|
|
|
|
let loaded = 0;
|
|
|
|
let total = 0;
|
|
|
|
let totalIsValid = true;
|
|
|
|
let progressIsFinal = true;
|
2020-11-19 16:54:07 +01:00
|
|
|
|
2020-11-23 13:27:13 +01:00
|
|
|
Object.keys(loadingFiles).forEach(function (file) {
|
2020-11-19 16:54:07 +01:00
|
|
|
const stat = loadingFiles[file];
|
2021-03-01 11:26:22 +01:00
|
|
|
if (!stat.done) {
|
2020-11-19 16:54:07 +01:00
|
|
|
progressIsFinal = false;
|
|
|
|
}
|
|
|
|
if (!totalIsValid || stat.total === 0) {
|
|
|
|
totalIsValid = false;
|
|
|
|
total = 0;
|
|
|
|
} else {
|
|
|
|
total += stat.total;
|
|
|
|
}
|
|
|
|
loaded += stat.loaded;
|
|
|
|
});
|
|
|
|
if (loaded !== lastProgress.loaded || total !== lastProgress.total) {
|
|
|
|
lastProgress.loaded = loaded;
|
|
|
|
lastProgress.total = total;
|
2020-11-23 13:27:13 +01:00
|
|
|
if (typeof progressFunc === 'function') {
|
2020-11-19 16:54:07 +01:00
|
|
|
progressFunc(loaded, total);
|
2020-11-23 13:27:13 +01:00
|
|
|
}
|
2020-11-19 16:54:07 +01:00
|
|
|
}
|
2020-11-23 13:27:13 +01:00
|
|
|
if (!progressIsFinal) {
|
2020-11-19 16:54:07 +01:00
|
|
|
requestAnimationFrame(animateProgress);
|
2020-11-23 13:27:13 +01:00
|
|
|
}
|
|
|
|
};
|
2020-11-19 16:54:07 +01:00
|
|
|
|
|
|
|
this.animateProgress = animateProgress;
|
|
|
|
|
2020-11-23 13:27:13 +01:00
|
|
|
this.setProgressFunc = function (callback) {
|
2020-11-19 16:54:07 +01:00
|
|
|
progressFunc = callback;
|
2020-11-23 13:27:13 +01:00
|
|
|
};
|
2020-11-19 16:54:07 +01:00
|
|
|
|
2021-03-01 17:53:56 +01:00
|
|
|
this.loadPromise = function (file, fileSize, raw = false) {
|
|
|
|
return retry(loadFetch.bind(null, file, loadingFiles, fileSize, raw), DOWNLOAD_ATTEMPTS_MAX);
|
2020-11-23 13:27:13 +01:00
|
|
|
};
|
2020-11-19 16:54:07 +01:00
|
|
|
|
|
|
|
this.preloadedFiles = [];
|
2021-03-01 17:53:56 +01:00
|
|
|
this.preload = function (pathOrBuffer, destPath, fileSize) {
|
2020-11-19 16:54:07 +01:00
|
|
|
let buffer = null;
|
|
|
|
if (typeof pathOrBuffer === 'string') {
|
2020-11-23 13:27:13 +01:00
|
|
|
const me = this;
|
2021-03-01 17:53:56 +01:00
|
|
|
return this.loadPromise(pathOrBuffer, fileSize).then(function (buf) {
|
2020-11-19 16:54:07 +01:00
|
|
|
me.preloadedFiles.push({
|
|
|
|
path: destPath || pathOrBuffer,
|
2021-03-01 11:26:22 +01:00
|
|
|
buffer: buf,
|
2020-11-19 16:54:07 +01:00
|
|
|
});
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
} else if (pathOrBuffer instanceof ArrayBuffer) {
|
|
|
|
buffer = new Uint8Array(pathOrBuffer);
|
|
|
|
} else if (ArrayBuffer.isView(pathOrBuffer)) {
|
|
|
|
buffer = new Uint8Array(pathOrBuffer.buffer);
|
|
|
|
}
|
|
|
|
if (buffer) {
|
|
|
|
this.preloadedFiles.push({
|
|
|
|
path: destPath,
|
2020-11-23 13:27:13 +01:00
|
|
|
buffer: pathOrBuffer,
|
2020-11-19 16:54:07 +01:00
|
|
|
});
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2020-11-23 13:27:13 +01:00
|
|
|
return Promise.reject(new Error('Invalid object for preloading'));
|
2020-11-19 16:54:07 +01:00
|
|
|
};
|
|
|
|
};
|