- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
import { binList } from "../cache.js";
import { crossOrigin, nocache, withCredentials } from "../settings.js";
/**
* parse/preload a Binary file
* @param {loader.Asset} data - asset data
* @param {Function} [onload] - function to be called when the asset is loaded
* @param {Function} [onerror] - function to be called in case of error
* @returns {number} the amount of corresponding resource parsed/preloaded
* @ignore
*/
export function preloadBinary(data, onload, onerror) {
let httpReq = new XMLHttpRequest();
// load our file
httpReq.open("GET", data.src + nocache, true);
httpReq.withCredentials = withCredentials;
httpReq.responseType = "arraybuffer";
httpReq.onerror = onerror;
httpReq.onload = function () {
let arrayBuffer = httpReq.response;
if (arrayBuffer) {
let byteArray = new Uint8Array(arrayBuffer);
let buffer = [];
for (let i = 0; i < byteArray.byteLength; i++) {
buffer[i] = String.fromCharCode(byteArray[i]);
}
binList[data.name] = buffer.join("");
if (typeof onload === "function") {
// callback
onload();
}
}
};
httpReq.send();
return 1;
}
/**
* parse/preload a Javascript files
* @param {loader.Asset} data - asset data
* @param {Function} [onload] - function to be called when the asset is loaded
* @param {Function} [onerror] - function to be called in case of error
* @ignore
*/
export function preloadJavascript(data, onload, onerror) {
let script = document.createElement("script");
script.src = data.src;
script.type = "text/javascript";
if (typeof (crossOrigin) === "string") {
script.crossOrigin = crossOrigin;
}
script.defer = true;
if (typeof onload === "function") {
script.onload = () => {
// callback
onload();
};
}
if (typeof onerror === "function") {
script.onerror = () => {
// callback
onerror(data.name);
};
}
document.getElementsByTagName("body")[0].appendChild(script);
return 1;
}