Chrome's XHR progress event can't compute total with compression, so let's estimate it

This commit is contained in:
Thomas Weber committed 2021-06-24 14:09:26 -05:00
1 parent 6d104ccc33
commit afccb775f8
3 files changed
+9 -1

No files matched your search

+2
View File
@@ -20,10 +20,12 @@ module.exports = {
},
scaffolding: {
src: 'scaffolding.js',
estimatedSize: 4775503,
type: 'text'
},
addons: {
src: 'addons.js',
estimatedSize: 14339,
type: 'text'
}
};
+6 -1
View File
@@ -1,8 +1,11 @@
const clampProgress = (n) => Math.max(0, Math.min(1, n));
const xhr = ({
url,
type,
progressCallback,
timeout
timeout,
estimatedSize
}) => new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = () => {
@@ -19,6 +22,8 @@ const xhr = ({
xhr.onprogress = (e) => {
if (e.lengthComputable) {
progressCallback(e.loaded / e.total);
} else if (estimatedSize) {
progressCallback(clampProgress(e.loaded / estimatedSize));
}
};
}
+1
View File
@@ -83,6 +83,7 @@ class Packager extends EventTarget {
result = await xhr({
url: asset.src,
type: asset.type,
estimatedSize: asset.estimatedSize,
progressCallback: (progress) => {
dispatchProgress(progress);
}