Skip to content

feat: Allow aborting uploads using AbortSignal #110

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
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: 14 additions & 0 deletions source/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
Expand All @@ -962,6 +970,12 @@ export class Session {
let url: string;
let headers: Record<string, string> = {};

const handleAbortSignal = () => {
xhr.abort();
options.signal?.removeEventListener("abort", handleAbortSignal);
};
options.signal?.addEventListener("abort", handleAbortSignal);

const updateOnProgressCallback = (
oEvent: ProgressEvent<XMLHttpRequestEventTarget>
) => {
Expand Down
1 change: 1 addition & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface CreateComponentOptions {
data?: Data;
onProgress?: (progress: number) => unknown;
xhr?: XMLHttpRequest;
signal?: AbortSignal;
onAborted?: () => unknown;
}

Expand Down
26 changes: 25 additions & 1 deletion test/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -313,6 +313,30 @@ describe("Session", () => {
await expect(promise).resolves.toEqual(true);
});

it("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();
Expand Down