Skip to content

Commit

Permalink
Pass AbortSignal to fetch to allow for cancelling in-flight requests …
Browse files Browse the repository at this point in the history
…on navigation

TanStack Query provides an `AbortSignal` instance to the query function which
can in turn be provided to `fetch` to cancel in-flight requests when the response
is no longer required, e.g. incomplete API calls in progress when the user navigates
to a different page.
  • Loading branch information
AlanGreene authored and tekton-robot committed Jun 24, 2024
1 parent 38a77eb commit 68feae1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/api/comms.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ export function get(uri, headers, options = {}) {
uri,
{
method: 'GET',
headers: getHeaders(headers)
headers: getHeaders(headers),
signal: options.signal
},
options.stream
);
Expand Down
4 changes: 2 additions & 2 deletions src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ export function getKubeAPI({
].join('');
}

export async function defaultQueryFn({ queryKey }) {
export async function defaultQueryFn({ queryKey, signal }) {
const [group, version, kind, params] = queryKey;
const url = getKubeAPI({ group, kind, params, version });
const response = await get(url);
const response = await get(url, undefined, { signal });
if (typeof response === 'undefined') {
return null;
}
Expand Down

0 comments on commit 68feae1

Please sign in to comment.