Skip to content
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

Correct loaded & progress values when file is uploaded #1002

Merged
merged 6 commits into from
Oct 23, 2023
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
14 changes: 10 additions & 4 deletions ember-file-upload/src/system/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ function parseResponse(request: XMLHttpRequest): Response {
}

export default class HTTPRequest {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onloadstart: (event?: ProgressEvent<EventTarget>) => void | undefined;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onprogress: (event?: ProgressEvent<EventTarget>) => void | undefined;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onloadend: (event?: ProgressEvent<EventTarget>) => void | undefined;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
ontimeout: (event?: ProgressEvent<EventTarget>) => void | undefined;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down Expand Up @@ -83,19 +89,19 @@ export default class HTTPRequest {
});

this.request.onloadstart = bind(this, function (evt) {
this.onprogress?.(evt);
this.onloadstart?.(evt);
});
this.request.onprogress = bind(this, function (evt) {
this.onprogress?.(evt);
});
this.request.onloadend = bind(this, function (evt) {
this.onprogress?.(evt);
this.onloadend?.(evt);
});

if (this.request.upload) {
this.request.upload.onloadstart = this.request.onprogress;
this.request.upload.onloadstart = this.request.onloadstart;
this.request.upload.onprogress = this.request.onprogress;
this.request.upload.onloadend = this.request.onprogress;
this.request.upload.onloadend = this.request.onloadend;
}

this.request.onload = bind(this, function () {
Expand Down
20 changes: 18 additions & 2 deletions ember-file-upload/src/system/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,29 @@ export function upload(
request.timeout = options.timeout;
}

request.onprogress = function (evt) {
request.onloadstart = function (evt) {
if (!evt) return;
if (!evt.lengthComputable || evt.total === 0) return;

file.loaded = evt.loaded;
file.size = evt.total;
file.progress = (evt.loaded / evt.total) * 100;
file.progress = (file.loaded / file.size) * 100;
};

request.onprogress = function (evt) {
if (!evt) return;
if (!evt.lengthComputable || evt.total === 0) return;

file.loaded = evt.loaded;
file.progress = (file.loaded / file.size) * 100;
};

request.onloadend = function (evt) {
if (!evt) return;
if (!evt.lengthComputable || evt.total === 0) return;

file.loaded = file.size;
file.progress = (file.loaded / file.size) * 100;
};

request.ontimeout = () => {
Expand Down
2 changes: 1 addition & 1 deletion test-app/tests/integration/progress-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FileTracker {
});
}

module.skip('Integration | progress', function (hooks) {
module('Integration | progress', function (hooks) {
setupRenderingTest(hooks);
setupMirage(hooks);

Expand Down