Skip to content

Commit

Permalink
fix: abort the request when unmounting the component or changing the …
Browse files Browse the repository at this point in the history
…config while the changelog is loading
  • Loading branch information
leMaik authored and saschb2b committed Sep 27, 2024
1 parent fc4e4ab commit 21a700e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/changelog.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function useChangelogs(config: UpdateHiveConfig): UpdateHiveHookResult {
const apiKey = config.connection.API_KEY;

useEffect(() => {
const ac = new AbortController();

void (async () => {
setIsLoading(true);

Expand All @@ -40,6 +42,7 @@ export function useChangelogs(config: UpdateHiveConfig): UpdateHiveHookResult {
Authorization: `Bearer ${apiKey}`,
Accept: 'application/vnd.wertarbyte.changelog.v1+json',
},
signal: ac.signal,
});

if (!result.ok) {
Expand All @@ -58,6 +61,9 @@ export function useChangelogs(config: UpdateHiveConfig): UpdateHiveHookResult {
}
} catch (error) {
if (error instanceof Error) {
if (error.name === 'AbortError') {
return; // fetch aborted, ie. unmounted component or config changed
}
setError(error.message);
} else {
setError('An unknown error occurred.');
Expand All @@ -66,6 +72,10 @@ export function useChangelogs(config: UpdateHiveConfig): UpdateHiveHookResult {

setIsLoading(false);
})();

return () => {
ac.abort();
};
}, [apiKey, requestURL]);

return {
Expand Down

0 comments on commit 21a700e

Please sign in to comment.