Skip to content

Commit

Permalink
Merge pull request #1211 from Edwardscb/melon-js-changes
Browse files Browse the repository at this point in the history
updated XMLHttpRequest to fetch for binary file loading
  • Loading branch information
obiot authored Dec 16, 2023
2 parents 4173aa0 + 9a73930 commit c1adcaa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"core-js": "^3.34.0",
"earcut": "2.2.4",
"eventemitter3": "^5.0.1",
"howler": "2.2.4"
"howler": "2.2.4",
"whatwg-fetch": "^3.6.19"
},
"devDependencies": {
"@babel/eslint-parser": "^7.23.3",
Expand Down
40 changes: 40 additions & 0 deletions src/loader/parsers/binary.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { binList } from "../cache.js";
import { crossOrigin, nocache, withCredentials } from "../settings.js";
import "whatwg-fetch";

/**
* parse/preload a Binary file
Expand All @@ -10,6 +11,44 @@ import { crossOrigin, nocache, withCredentials } from "../settings.js";
* @ignore
*/
export function preloadBinary(data, onload, onerror) {

fetch(data.src + nocache, {
method: "GET",
credentials: withCredentials ? "include" : "omit"
})
.then(response => {
if (!response.ok) {
throw new Error(`Network response was not ok ${response.statusText}`);
}
return response.arrayBuffer();
})
.then(arrayBuffer => {
/* ===== This code is one way to do it, left it here just in case, since I don't know entire codebase, this might be the best way still =====
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("");
*/

// this method is native and might be slightly more efficient
const decoder = new TextDecoder(); // the default for this is 'utf-8'
binList[data.name] = decoder.decode(arrayBuffer);

if (typeof onload === "function") {
// callback
onload();
}
})
.catch(error => {
if (typeof onerror === "function") {
onerror(error);
}
});

return 1;
/* ===== This is the code that was here before changing to fetch =====
let httpReq = new XMLHttpRequest();
// load our file
Expand All @@ -36,6 +75,7 @@ export function preloadBinary(data, onload, onerror) {
httpReq.send();
return 1;
*/
}

/**
Expand Down

0 comments on commit c1adcaa

Please sign in to comment.