Skip to content

Commit

Permalink
Prevent infinite re-renders in extensions (#457)
Browse files Browse the repository at this point in the history
Regression introduced with 593acc7
  • Loading branch information
schroda authored Nov 18, 2023
1 parent 8d46874 commit 482db46
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions src/lib/requests/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
useSubscription,
} from '@apollo/client';
import { OperationVariables } from '@apollo/client/core';
import { useEffect, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { IRestClient, RestClient } from '@/lib/requests/client/RestClient.ts';
import * as storage from '@/util/localStorage.tsx';
import { GraphQLClient } from '@/lib/requests/client/GraphQLClient.ts';
Expand Down Expand Up @@ -876,33 +876,48 @@ export class RequestManager {
{},
{ refetchQueries: [GET_EXTENSIONS], ...options },
);
const [, setUpdatedCache] = useState({});

useEffect(() => {
if (result.loading) {
return;
}

if (!result.data?.fetchExtensions.extensions) {
return;
}

if (result.data?.fetchExtensions.extensions) {
this.cache.cacheResponse(EXTENSION_LIST_CACHE_KEY, undefined, result);
}
setUpdatedCache({});
}, [result.loading]);

const cachedResult = this.cache.getResponseFor<typeof result>(EXTENSION_LIST_CACHE_KEY, undefined, 1000 * 60);
const normalizedCachedResult = !cachedResult
? result
: {
...cachedResult,
data: !cachedResult?.data?.fetchExtensions.extensions
? cachedResult?.data
: {
...cachedResult.data,
fetchExtensions: {
...cachedResult.data.fetchExtensions,
extensions: cachedResult.data.fetchExtensions.extensions.map(
(extension) =>
this.graphQLClient.client.cache.readFragment<
GetExtensionsFetchMutation['fetchExtensions']['extensions'][0]
>({
id: this.graphQLClient.client.cache.identify(extension),
fragment: FULL_EXTENSION_FIELDS,
}) ?? extension,
),
},
},
};
const normalizedCachedResult = useMemo(
() =>
!cachedResult
? result
: {
...cachedResult,
data: !cachedResult?.data?.fetchExtensions.extensions
? cachedResult?.data
: {
...cachedResult.data,
fetchExtensions: {
...cachedResult.data.fetchExtensions,
extensions: cachedResult.data.fetchExtensions.extensions.map(
(extension) =>
this.graphQLClient.client.cache.readFragment<
GetExtensionsFetchMutation['fetchExtensions']['extensions'][0]
>({
id: this.graphQLClient.client.cache.identify(extension),
fragment: FULL_EXTENSION_FIELDS,
}) ?? extension,
),
},
},
},
[this.cache.getFetchTimestampFor(EXTENSION_LIST_CACHE_KEY, undefined)],
);

const wrappedMutate = async (mutateOptions: Parameters<typeof mutate>[0]) => {
if (cachedResult) {
Expand Down

0 comments on commit 482db46

Please sign in to comment.