Skip to content

Commit 133b5ca

Browse files
authored
feat: Allow aborting uploads using AbortSignal (#110)
1 parent ca25de7 commit 133b5ca

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

source/session.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ export class Session {
926926
* @param {?object} [options = {}] - Options
927927
* @param {?string} options.name - Component name. Defaults get from file object.
928928
* @param {?number} options.data - Component data. Defaults to {}.
929+
* @param {XMLHttpRequest} options.xhr - Custom XHR object, deprecated in favor of options.signal.
930+
* @param {AbortSignal} options.signal - Abort signal
929931
* @return {Promise} Promise resolved with the response when creating
930932
* Component and ComponentLocation.
931933
*/
@@ -949,6 +951,12 @@ export class Session {
949951
const defaultProgress = (progress: number) => progress;
950952
const defaultAbort = () => {};
951953

954+
if (options.xhr) {
955+
logger.warn(
956+
"[session.createComponent] options.xhr is deprecated, use options.signal for aborting uploads."
957+
);
958+
}
959+
952960
const data = options.data || {};
953961
const onProgress = options.onProgress || defaultProgress;
954962
const xhr = options.xhr || new XMLHttpRequest();
@@ -962,6 +970,12 @@ export class Session {
962970
let url: string;
963971
let headers: Record<string, string> = {};
964972

973+
const handleAbortSignal = () => {
974+
xhr.abort();
975+
options.signal?.removeEventListener("abort", handleAbortSignal);
976+
};
977+
options.signal?.addEventListener("abort", handleAbortSignal);
978+
965979
const updateOnProgressCallback = (
966980
oEvent: ProgressEvent<XMLHttpRequestEventTarget>
967981
) => {

source/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface CreateComponentOptions {
1919
data?: Data;
2020
onProgress?: (progress: number) => unknown;
2121
xhr?: XMLHttpRequest;
22+
signal?: AbortSignal;
2223
onAborted?: () => unknown;
2324
}
2425

test/session.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ describe("Session", () => {
289289
});
290290
});
291291

292-
it("Should support abort of uploading file", async () => {
292+
it("Should support abort of uploading file using xhr", async () => {
293293
const data = { foo: "bar" };
294294
const blob = new Blob([JSON.stringify(data)], {
295295
type: "application/json",
@@ -313,6 +313,30 @@ describe("Session", () => {
313313
await expect(promise).resolves.toEqual(true);
314314
});
315315

316+
it("Should support abort of uploading file using signal", async () => {
317+
const data = { foo: "bar" };
318+
const blob = new Blob([JSON.stringify(data)], {
319+
type: "application/json",
320+
});
321+
322+
const controller = new AbortController();
323+
const promise = new Promise((resolve) => {
324+
const onAborted = () => {
325+
resolve(true);
326+
};
327+
328+
session.createComponent(blob, {
329+
signal: controller.signal,
330+
name: "data.json",
331+
onProgress: () => {
332+
controller.abort();
333+
},
334+
onAborted,
335+
});
336+
});
337+
await expect(promise).resolves.toEqual(true);
338+
});
339+
316340
it.skip("Should support ensure with create", async () => {
317341
const identifyingKeys = ["key", "parent_id", "parent_type"];
318342
const key = uuidV4();

0 commit comments

Comments
 (0)