Skip to content

Commit 10ae4cd

Browse files
committed
feat: implement abort signal support and upload progress tracking in upload process
1 parent f2ce24b commit 10ae4cd

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

samples/sample-app/views/index.pug

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ html
2323
script(type='text/javascript' src="./imagekit.min.js")
2424
script.
2525
try {
26+
window.controller = new AbortController();
2627
var imagekit = new ImageKit({
2728
publicKey: "!{publicKey}",
2829
urlEndpoint: "!{urlEndpoint}",
@@ -51,9 +52,11 @@ html
5152
var statusEl = document.getElementById("status");
5253
statusEl.innerHTML = "Uploading...";
5354

55+
5456
// Use this if you want to track upload progress
5557
var customXHR = new XMLHttpRequest();
5658
customXHR.upload.addEventListener('progress', function (e) {
59+
console.log("On progress event handler from customXHR");
5760
if (e.loaded <= fileSize) {
5861
var percent = Math.round(e.loaded / fileSize * 100);
5962
console.log(`Uploaded ${percent}%`);
@@ -94,6 +97,11 @@ html
9497
token: securityParametersObj.token,
9598
signature: securityParametersObj.signature,
9699
expire: securityParametersObj.expire,
100+
signal: window.controller.signal,
101+
onProgress: function(e) {
102+
console.log("On progress event handler from SDK");
103+
console.log(e.loaded);
104+
},
97105
//- extensions: [
98106
//- {
99107
//- name: "aws-auto-tagging",
@@ -102,6 +110,7 @@ html
102110
//- }
103111
//- ],
104112
}, function(err, result) {
113+
debugger;
105114
if (err) {
106115
statusEl.innerHTML = "Error uploading image. "+ err.message;
107116
console.log(err)

src/upload/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export const upload = (
9393
} else if (key === 'checks' && uploadOptions.checks) {
9494
formData.append("checks", uploadOptions.checks);
9595
} else if (uploadOptions[key] !== undefined) {
96+
if (["onProgress", "signal"].includes(key)) continue;
9697
formData.append(key, String(uploadOptions[key]));
9798
}
9899
}
@@ -106,15 +107,28 @@ export const upload = (
106107
};
107108
}
108109

109-
if (uploadOptions.signal && uploadOptions.signal) {
110+
function onAbortHandler() {
111+
xhr.abort();
112+
// Provide the reason or fallback error
113+
// @ts-ignore for TypeScript versions lacking `signal.reason`
114+
respond(true, uploadOptions.signal?.reason ?? errorMessages.UPLOAD_ABORTED, callback);
115+
}
116+
117+
if (uploadOptions.signal) {
110118
if (uploadOptions.signal.aborted) { // If the signal is already aborted, return immediately with the reason
119+
// @ts-ignore for TypeScript versions lacking `signal.reason`
111120
respond(true, uploadOptions.signal.reason ?? errorMessages.UPLOAD_ABORTED, callback);
112121
return;
113122
}
123+
114124
// If the signal is not already aborted, add an event listener to abort the request when the signal is aborted
115-
uploadOptions.signal.addEventListener("abort", function () {
116-
xhr.abort();
117-
respond(true, this.reason, callback);
125+
uploadOptions.signal.addEventListener("abort", onAbortHandler);
126+
127+
// On XHR completion (success, fail, or abort), remove just this abort handler
128+
xhr.addEventListener("loadend", () => {
129+
if (uploadOptions.signal) {
130+
uploadOptions.signal.removeEventListener("abort", onAbortHandler);
131+
}
118132
});
119133
}
120134

0 commit comments

Comments
 (0)