Skip to content

Commit b48b2b0

Browse files
chore(client): restructure abort controller binding
1 parent 3169d51 commit b48b2b0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/client.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ export class Morphik {
575575
controller: AbortController,
576576
): Promise<Response> {
577577
const { signal, method, ...options } = init || {};
578-
const abort = controller.abort.bind(controller);
578+
const abort = this._makeAbort(controller);
579579
if (signal) signal.addEventListener('abort', abort, { once: true });
580580

581581
const timeout = setTimeout(abort, ms);
@@ -601,6 +601,7 @@ export class Morphik {
601601
return await this.fetch.call(undefined, url, fetchOptions);
602602
} finally {
603603
clearTimeout(timeout);
604+
if (signal) signal.removeEventListener('abort', abort);
604605
}
605606
}
606607

@@ -745,6 +746,12 @@ export class Morphik {
745746
return headers.values;
746747
}
747748

749+
private _makeAbort(controller: AbortController) {
750+
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
751+
// would capture all request options, and cause a memory leak.
752+
return () => controller.abort();
753+
}
754+
748755
private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
749756
bodyHeaders: HeadersLike;
750757
body: BodyInit | undefined;

0 commit comments

Comments
 (0)