Skip to content

Commit a999e17

Browse files
chore(client): restructure abort controller binding
1 parent be6523d commit a999e17

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
@@ -532,7 +532,7 @@ export class Grid {
532532
controller: AbortController,
533533
): Promise<Response> {
534534
const { signal, method, ...options } = init || {};
535-
const abort = controller.abort.bind(controller);
535+
const abort = this._makeAbort(controller);
536536
if (signal) signal.addEventListener('abort', abort, { once: true });
537537

538538
const timeout = setTimeout(abort, ms);
@@ -558,6 +558,7 @@ export class Grid {
558558
return await this.fetch.call(undefined, url, fetchOptions);
559559
} finally {
560560
clearTimeout(timeout);
561+
if (signal) signal.removeEventListener('abort', abort);
561562
}
562563
}
563564

@@ -703,6 +704,12 @@ export class Grid {
703704
return headers.values;
704705
}
705706

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

0 commit comments

Comments
 (0)