Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Feature/global update last timestamp use stale data while fetching #473

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/components/library/UpdateChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';
import IconButton from '@mui/material/IconButton';
import RefreshIcon from '@mui/icons-material/Refresh';
import { useTranslation } from 'react-i18next';
Expand All @@ -29,10 +29,12 @@ const calcProgress = (status: UpdaterSubscription['updateStatusChanged'] | undef
return Number.isNaN(progress) ? 0 : progress;
};

export function UpdateChecker({ handleFinishedUpdate }: { handleFinishedUpdate: () => void }) {
let lastRunningState = false;

export function UpdateChecker({ handleFinishedUpdate }: { handleFinishedUpdate?: () => void }) {
const { t } = useTranslation();

const { data: lastUpdateTimestampData, refetch: refetchlastTimestamp } =
const { data: lastUpdateTimestampData, refetch: reFetchLastTimestamp } =
requestManager.useGetLastGlobalUpdateTimestamp();
const lastUpdateTimestamp = lastUpdateTimestampData?.lastUpdateTimestamp.timestamp;
const { data: updaterData } = requestManager.useUpdaterSubscription();
Expand All @@ -49,16 +51,25 @@ export function UpdateChecker({ handleFinishedUpdate }: { handleFinishedUpdate:
],
);

const isUpdateFinished = progress === 100;
if (isUpdateFinished) {
refetchlastTimestamp();
handleFinishedUpdate();
}
useEffect(() => {
const isUpdateFinished = lastRunningState && progress === 100;
if (!isUpdateFinished) {
return;
}

lastRunningState = false;
handleFinishedUpdate?.();
// this re-fetch is necessary since a running update could have been triggered by the server or another client
reFetchLastTimestamp().catch(() => {});
}, [status?.isRunning]);

const onClick = async () => {
try {
lastRunningState = true;
await requestManager.startGlobalUpdate().response;
reFetchLastTimestamp().catch(() => {});
} catch (e) {
lastRunningState = false;
makeToast(t('global.error.label.update_failed'), 'error');
}
};
Expand Down
10 changes: 1 addition & 9 deletions src/lib/requests/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1987,15 +1987,7 @@ export class RequestManager {
public useGetLastGlobalUpdateTimestamp(
options?: QueryHookOptions<GetLastUpdateTimestampQuery, GetLastUpdateTimestampQueryVariables>,
): AbortableApolloUseQueryResponse<GetLastUpdateTimestampQuery, GetLastUpdateTimestampQueryVariables> {
return this.doRequest(
GQLMethod.USE_QUERY,
GET_LAST_UPDATE_TIMESTAMP,
{},
{
fetchPolicy: 'network-only',
...options,
},
);
return this.doRequest(GQLMethod.USE_QUERY, GET_LAST_UPDATE_TIMESTAMP, {}, options);
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/screens/Updates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ export const Updates: React.FC = () => {
const { data: downloaderData } = requestManager.useDownloadSubscription();
const queue = (downloaderData?.downloadChanged.queue as DownloadType[]) ?? [];

const { data: lastUpdateTimestampData, refetch: refetchlastTimestamp } =
requestManager.useGetLastGlobalUpdateTimestamp();
const { data: lastUpdateTimestampData } = requestManager.useGetLastGlobalUpdateTimestamp({
/**
* The {@link UpdateChecker} is responsible for updating the timestamp
*/
fetchPolicy: 'cache-only',
});
const lastUpdateTimestamp = lastUpdateTimestampData?.lastUpdateTimestamp.timestamp;

useEffect(() => {
Expand All @@ -132,7 +136,7 @@ export const Updates: React.FC = () => {
date: lastUpdateTimestamp ? new Date(+lastUpdateTimestamp).toLocaleString() : '-',
})}
</Typography>
<UpdateChecker handleFinishedUpdate={() => refetchlastTimestamp()} />
<UpdateChecker />
</>,
);
}, [t, lastUpdateTimestamp]);
Expand Down