Chrome's XHR progress event can't compute total with compression, so let's estimate it
This commit is contained in:
3 files changed
+9
-1
No files matched your search
@@ -20,10 +20,12 @@ module.exports = {
|
||||
},
|
||||
scaffolding: {
|
||||
src: 'scaffolding.js',
|
||||
estimatedSize: 4775503,
|
||||
type: 'text'
|
||||
},
|
||||
addons: {
|
||||
src: 'addons.js',
|
||||
estimatedSize: 14339,
|
||||
type: 'text'
|
||||
}
|
||||
};
|
||||
@@ -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));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ class Packager extends EventTarget {
|
||||
result = await xhr({
|
||||
url: asset.src,
|
||||
type: asset.type,
|
||||
estimatedSize: asset.estimatedSize,
|
||||
progressCallback: (progress) => {
|
||||
dispatchProgress(progress);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user