Skip to content

Improve file upload progress #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions src/RESTController.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,18 @@ const RESTController = {
for (const key in customHeaders) {
headers[key] = customHeaders[key];
}
if(options && typeof options.progress === 'function') {
if (xhr.upload) {
xhr.upload.addEventListener('progress', (oEvent) => {
if (oEvent.lengthComputable) {
options.progress(oEvent.loaded / oEvent.total);
} else {
options.progress(null);
}
});
} else if (xhr.addEventListener) {
xhr.addEventListener('progress', (oEvent) => {
if (oEvent.lengthComputable) {
options.progress(oEvent.loaded / oEvent.total);
} else {
options.progress(null);
}
});
}
}
xhr.onabort = () => {
aborted = true;
};
xhr.onprogress = (event) => {
if(options && typeof options.progress === 'function') {
if (event.lengthComputable) {
options.progress(event.loaded / event.total, event.loaded, event.total);
} else {
options.progress(null);
}
}
};
xhr.open(method, url, true);

for (const h in headers) {
Expand Down
11 changes: 10 additions & 1 deletion src/Xhr.weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = class XhrWeapp {
this.method = '';
this.url = '';
this.onabort = () => {};
this.onprogress = () => {};
this.onerror = () => {};
this.onreadystatechange = () => {};
this.requestTask = null;
Expand Down Expand Up @@ -72,6 +73,14 @@ module.exports = class XhrWeapp {
this.requestTask = null;
this.onerror(err);
}
})
});
this.requestTask.onProgressUpdate((res) => {
const event = {
lengthComputable: (res.totalBytesExpectedToWrite !== 0),
loaded: res.totalBytesWritten,
total: res.totalBytesExpectedToWrite,
};
this.onprogress(event);
});
}
};
39 changes: 29 additions & 10 deletions src/__tests__/RESTController-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,11 @@ describe('RESTController', () => {
});

it('reports upload progress of the AJAX request when callback is provided', (done) => {
const xhr = mockXHR([{ status: 200, response: { success: true }}], {
addEventListener: (name, callback) => {
if(name === "progress") {
callback({
lengthComputable: true,
loaded: 5,
total: 10
});
}
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
progress: {
lengthComputable: true,
loaded: 5,
total: 10
}
});
RESTController._setXHR(xhr);
Expand All @@ -448,7 +444,30 @@ describe('RESTController', () => {
jest.spyOn(options, 'progress');

RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
expect(options.progress).toHaveBeenCalledWith(0.5);
expect(options.progress).toHaveBeenCalledWith(0.5, 5, 10);
expect(response).toEqual({ success: true });
expect(status).toBe(200);
done();
});
});

it('does not upload progress when total is uncomputable', (done) => {
const xhr = mockXHR([{ status: 200, response: { success: true } }], {
progress: {
lengthComputable: false,
loaded: 5,
total: 0
}
});
RESTController._setXHR(xhr);

const options = {
progress: function(){}
};
jest.spyOn(options, 'progress');

RESTController.ajax('POST', 'files/upload.txt', {}, {}, options).then(({ response, status }) => {
expect(options.progress).toHaveBeenCalledWith(null);
expect(response).toEqual({ success: true });
expect(status).toBe(200);
done();
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/test_helpers/mockXHR.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* alongside it.
* `upload` can be provided to mock the XMLHttpRequest.upload property.
*/
function mockXHR(results, upload) {
function mockXHR(results, options = {}) {
const XHR = function() { };
let attempts = 0;
XHR.prototype = {
Expand All @@ -28,8 +28,8 @@ function mockXHR(results, upload) {
this.readyState = 4;
attempts++;
this.onreadystatechange();
},
upload: upload
this.onprogress(options.progress);
}
};
return XHR;
}
Expand Down