Skip to content

Commit 1a83b30

Browse files
chore(client): restructure abort controller binding
1 parent bf937d4 commit 1a83b30

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
@@ -534,7 +534,7 @@ export class Arcade {
534534
controller: AbortController,
535535
): Promise<Response> {
536536
const { signal, method, ...options } = init || {};
537-
const abort = controller.abort.bind(controller);
537+
const abort = this._makeAbort(controller);
538538
if (signal) signal.addEventListener('abort', abort, { once: true });
539539

540540
const timeout = setTimeout(abort, ms);
@@ -560,6 +560,7 @@ export class Arcade {
560560
return await this.fetch.call(undefined, url, fetchOptions);
561561
} finally {
562562
clearTimeout(timeout);
563+
if (signal) signal.removeEventListener('abort', abort);
563564
}
564565
}
565566

@@ -704,6 +705,12 @@ export class Arcade {
704705
return headers.values;
705706
}
706707

708+
private _makeAbort(controller: AbortController) {
709+
// note: we can't just inline this method inside `fetchWithTimeout()` because then the closure
710+
// would capture all request options, and cause a memory leak.
711+
return () => controller.abort();
712+
}
713+
707714
private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
708715
bodyHeaders: HeadersLike;
709716
body: BodyInit | undefined;

0 commit comments

Comments
 (0)