Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rokups committed Oct 3, 2024
1 parent cad9d1d commit 5f44b2a
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 325 deletions.
91 changes: 49 additions & 42 deletions samples/Resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,51 +40,58 @@ var REMOTE_PACKAGE_SIZE = metadata['remote_package_size'];
});
return;
}
var xhr = new XMLHttpRequest();
xhr.open('GET', packageName, true);
xhr.responseType = 'arraybuffer';
xhr.onprogress = (event) => {
var url = packageName;
var size = packageSize;
if (event.total) size = event.total;
if (event.loaded) {
if (!xhr.addedTotal) {
xhr.addedTotal = true;
if (!Module['dataFileDownloads']) Module['dataFileDownloads'] = {};
Module['dataFileDownloads'][url] = {
loaded: event.loaded,
total: size
};
} else {
Module['dataFileDownloads'][url].loaded = event.loaded;
Module['dataFileDownloads'] ??= {};
fetch(packageName)
.catch((cause) => Promise.reject(new Error(`Network Error: ${packageName}`, {cause}))) // If fetch fails, rewrite the error to include the failing URL & the cause.
.then((response) => {
if (!response.ok) {
return Promise.reject(new Error(`${response.status}: ${response.url}`));
}
var total = 0;
var loaded = 0;
var num = 0;
for (var download in Module['dataFileDownloads']) {
var data = Module['dataFileDownloads'][download];
total += data.total;
loaded += data.loaded;
num++;

if (!response.body && response.arrayBuffer) { // If we're using the polyfill, readers won't be available...
return response.arrayBuffer().then(callback);
}
total = Math.ceil(total * Module['expectedDataFileDownloads']/num);
Module['setStatus']?.(`Downloading data... (${loaded}/${total})`);
} else if (!Module['dataFileDownloads']) {

const reader = response.body.getReader();
const iterate = () => reader.read().then(handleChunk).catch((cause) => {
return Promise.reject(new Error(`Unexpected error while handling : ${response.url} ${cause}`, {cause}));
});

const chunks = [];
const headers = response.headers;
const total = Number(headers.get('Content-Length') ?? packageSize);
let loaded = 0;

const handleChunk = ({done, value}) => {
if (!done) {
chunks.push(value);
loaded += value.length;
Module['dataFileDownloads'][packageName] = {loaded, total};

let totalLoaded = 0;
let totalSize = 0;

for (const download of Object.values(Module['dataFileDownloads'])) {
totalLoaded += download.loaded;
totalSize += download.total;
}

Module['setStatus']?.(`Downloading data... (${totalLoaded}/${totalSize})`);
return iterate();
} else {
const packageData = new Uint8Array(chunks.map((c) => c.length).reduce((a, b) => a + b, 0));
let offset = 0;
for (const chunk of chunks) {
packageData.set(chunk, offset);
offset += chunk.length;
}
callback(packageData.buffer);
}
};

Module['setStatus']?.('Downloading data...');
}
};
xhr.onerror = (event) => {
throw new Error("NetworkError for: " + packageName);
}
xhr.onload = (event) => {
if (xhr.status == 200 || xhr.status == 304 || xhr.status == 206 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
var packageData = xhr.response;
callback(packageData);
} else {
throw new Error(xhr.statusText + " : " + xhr.responseURL);
}
};
xhr.send(null);
return iterate();
});
};

function handleError(error) {
Expand Down
Loading

0 comments on commit 5f44b2a

Please sign in to comment.