Skip to content

Commit d12c7fa

Browse files
chore(client): restructure abort controller binding
1 parent d6641ad commit d12c7fa

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
@@ -639,7 +639,7 @@ export class Kernel {
639639
controller: AbortController,
640640
): Promise<Response> {
641641
const { signal, method, ...options } = init || {};
642-
const abort = controller.abort.bind(controller);
642+
const abort = this._makeAbort(controller);
643643
if (signal) signal.addEventListener('abort', abort, { once: true });
644644

645645
const timeout = setTimeout(abort, ms);
@@ -665,6 +665,7 @@ export class Kernel {
665665
return await this.fetch.call(undefined, url, fetchOptions);
666666
} finally {
667667
clearTimeout(timeout);
668+
if (signal) signal.removeEventListener('abort', abort);
668669
}
669670
}
670671

@@ -809,6 +810,12 @@ export class Kernel {
809810
return headers.values;
810811
}
811812

813+
private _makeAbort(controller: AbortController) {
814+
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
815+
// would capture all request options, and cause a memory leak.
816+
return () => controller.abort();
817+
}
818+
812819
private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
813820
bodyHeaders: HeadersLike;
814821
body: BodyInit | undefined;

0 commit comments

Comments
 (0)