Skip to content

Commit 59bbe0a

Browse files
chore(client): restructure abort controller binding
1 parent b12bb38 commit 59bbe0a

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
@@ -574,7 +574,7 @@ export class Together {
574574
controller: AbortController,
575575
): Promise<Response> {
576576
const { signal, method, ...options } = init || {};
577-
const abort = controller.abort.bind(controller);
577+
const abort = this._makeAbort(controller);
578578
if (signal) signal.addEventListener('abort', abort, { once: true });
579579

580580
const timeout = setTimeout(abort, ms);
@@ -600,6 +600,7 @@ export class Together {
600600
return await this.fetch.call(undefined, url, fetchOptions);
601601
} finally {
602602
clearTimeout(timeout);
603+
if (signal) signal.removeEventListener('abort', abort);
603604
}
604605
}
605606

@@ -745,6 +746,12 @@ export class Together {
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)