From 8bf7f851a084608059bfba3c4cc0508be315295d Mon Sep 17 00:00:00 2001 From: Lucas Correia Date: Thu, 25 May 2023 15:01:42 +0200 Subject: [PATCH 1/3] feat: Allow aborting uploads using AbortSignal --- source/session.ts | 12 ++++++++++++ source/types.ts | 1 + test/session.test.js | 26 +++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/source/session.ts b/source/session.ts index 9634c149..b70a3405 100644 --- a/source/session.ts +++ b/source/session.ts @@ -926,6 +926,8 @@ export class Session { * @param {?object} [options = {}] - Options * @param {?string} options.name - Component name. Defaults get from file object. * @param {?number} options.data - Component data. Defaults to {}. + * @param {XMLHttpRequest} options.xhr - Custom XHR object, deprecated in favor of options.signal. + * @param {AbortSignal} options.signal - Abort signal * @return {Promise} Promise resolved with the response when creating * Component and ComponentLocation. */ @@ -949,6 +951,12 @@ export class Session { const defaultProgress = (progress: number) => progress; const defaultAbort = () => {}; + if (options.xhr) { + logger.warn( + "[session.createComponent] options.xhr is deprecated, use options.signal for aborting uploads." + ); + } + const data = options.data || {}; const onProgress = options.onProgress || defaultProgress; const xhr = options.xhr || new XMLHttpRequest(); @@ -962,6 +970,10 @@ export class Session { let url: string; let headers: Record = {}; + options.signal?.addEventListener("abort", () => { + xhr.abort(); + }); + const updateOnProgressCallback = ( oEvent: ProgressEvent ) => { diff --git a/source/types.ts b/source/types.ts index e8170064..0b28895b 100644 --- a/source/types.ts +++ b/source/types.ts @@ -19,6 +19,7 @@ export interface CreateComponentOptions { data?: Data; onProgress?: (progress: number) => unknown; xhr?: XMLHttpRequest; + signal?: AbortSignal; onAborted?: () => unknown; } diff --git a/test/session.test.js b/test/session.test.js index 14794d50..7a2e5932 100755 --- a/test/session.test.js +++ b/test/session.test.js @@ -289,7 +289,7 @@ describe("Session", () => { }); }); - it("Should support abort of uploading file", async () => { + it("Should support abort of uploading file using xhr", async () => { const data = { foo: "bar" }; const blob = new Blob([JSON.stringify(data)], { type: "application/json", @@ -313,6 +313,30 @@ describe("Session", () => { await expect(promise).resolves.toEqual(true); }); + it.only("Should support abort of uploading file using signal", async () => { + const data = { foo: "bar" }; + const blob = new Blob([JSON.stringify(data)], { + type: "application/json", + }); + + const controller = new AbortController(); + const promise = new Promise((resolve) => { + const onAborted = () => { + resolve(true); + }; + + session.createComponent(blob, { + signal: controller.signal, + name: "data.json", + onProgress: () => { + controller.abort(); + }, + onAborted, + }); + }); + await expect(promise).resolves.toEqual(true); + }); + it.skip("Should support ensure with create", async () => { const identifyingKeys = ["key", "parent_id", "parent_type"]; const key = uuidV4(); From 1c06f6d8887f841153004485cc2b2fdfce025c42 Mon Sep 17 00:00:00 2001 From: Lucas Correia Date: Fri, 26 May 2023 13:10:30 +0200 Subject: [PATCH 2/3] Remove only for tests --- test/session.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/session.test.js b/test/session.test.js index 7a2e5932..fe574d7d 100755 --- a/test/session.test.js +++ b/test/session.test.js @@ -313,7 +313,7 @@ describe("Session", () => { await expect(promise).resolves.toEqual(true); }); - it.only("Should support abort of uploading file using signal", async () => { + it("Should support abort of uploading file using signal", async () => { const data = { foo: "bar" }; const blob = new Blob([JSON.stringify(data)], { type: "application/json", From 144c33842292ad486564f402d36b87979e17f1db Mon Sep 17 00:00:00 2001 From: Lucas Correia Date: Fri, 26 May 2023 16:03:39 +0200 Subject: [PATCH 3/3] Remove abort handler once fired once --- source/session.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/session.ts b/source/session.ts index b70a3405..1ba77730 100644 --- a/source/session.ts +++ b/source/session.ts @@ -970,9 +970,11 @@ export class Session { let url: string; let headers: Record = {}; - options.signal?.addEventListener("abort", () => { + const handleAbortSignal = () => { xhr.abort(); - }); + options.signal?.removeEventListener("abort", handleAbortSignal); + }; + options.signal?.addEventListener("abort", handleAbortSignal); const updateOnProgressCallback = ( oEvent: ProgressEvent