Skip to content

Commit 46c04e1

Browse files
chore(client): restructure abort controller binding
1 parent 5f6c688 commit 46c04e1

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ export class ImageKit {
599599
controller: AbortController,
600600
): Promise<Response> {
601601
const { signal, method, ...options } = init || {};
602-
const abort = controller.abort.bind(controller);
602+
const abort = this._makeAbort(controller);
603603
if (signal) signal.addEventListener('abort', abort, { once: true });
604604

605605
const timeout = setTimeout(abort, ms);
@@ -625,6 +625,7 @@ export class ImageKit {
625625
return await this.fetch.call(undefined, url, fetchOptions);
626626
} finally {
627627
clearTimeout(timeout);
628+
if (signal) signal.removeEventListener('abort', abort);
628629
}
629630
}
630631

@@ -769,6 +770,12 @@ export class ImageKit {
769770
return headers.values;
770771
}
771772

773+
private _makeAbort(controller: AbortController) {
774+
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
775+
// would capture all request options, and cause a memory leak.
776+
return () => controller.abort();
777+
}
778+
772779
private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
773780
bodyHeaders: HeadersLike;
774781
body: BodyInit | undefined;

0 commit comments

Comments
 (0)