Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix polling behavior #2779

Merged
merged 1 commit into from
Nov 1, 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
3 changes: 3 additions & 0 deletions app/Base/configs/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ const apolloOptions: ApolloClientOptions<NormalizedCacheObject> = {
AssistedTaggingQueryType: {
keyFields: [],
},
AssistedTaggingMutationType: {
keyFields: [],
},
},
}),
assumeImmutableResults: true,
Expand Down
43 changes: 26 additions & 17 deletions app/views/EntryEdit/LeftPane/AssistItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,19 @@ function AssistItem(props: Props) {
]);

const [draftEntryId, setDraftEntryId] = useState<string | undefined>(undefined);
// FIXME: randomId is used to create different query variables after each poll
// so that apollo doesn't create unnecessary cache
const [randomId, setRandomId] = useState<string>(randomString());
const [predictionsLoading, setPredictionsLoading] = useState(false);

const queryVariables = useMemo(() => (
draftEntryId && projectId ? ({
projectId,
draftEntryId,
randomId,
}) : undefined
), [
randomId,
projectId,
draftEntryId,
]);
Expand All @@ -486,8 +491,7 @@ function AssistItem(props: Props) {
const {
loading: draftEntryFetchPending,
data,
startPolling,
stopPolling,
refetch,
error: fetchErrors,
} = useQuery<ProjectDraftEntryQuery, ProjectDraftEntryQueryVariables>(
PROJECT_DRAFT_ENTRY,
Expand All @@ -496,6 +500,7 @@ function AssistItem(props: Props) {
variables: queryVariables,
onCompleted: (response) => {
const result = response?.project?.assistedTagging?.draftEntry;
setPredictionsLoading(true);

// FIXME: Handle errors more gracefully
if (!result) {
Expand Down Expand Up @@ -548,28 +553,32 @@ function AssistItem(props: Props) {
},
);

const shouldPoll = useMemo(() => {
const draftEntry = data?.project?.assistedTagging?.draftEntry;
return draftEntry?.predictionStatus === 'PENDING' || draftEntry?.predictionStatus === 'STARTED';
}, [data]);

useEffect(
() => {
if (!shouldPoll) {
return undefined;
}
setPredictionsLoading(true);
startPolling(2000);
const timeout = setTimeout(
() => {
const draftEntry = data?.project?.assistedTagging?.draftEntry;
const shouldPoll = draftEntry?.predictionStatus === 'PENDING'
|| draftEntry?.predictionStatus === 'STARTED';

if (shouldPoll) {
setPredictionsLoading(true);
setRandomId(randomString());
refetch();
} else {
setPredictionsLoading(false);
}
},
2000,
);

return () => {
setPredictionsLoading(false);
stopPolling();
clearTimeout(timeout);
};
},
[
shouldPoll,
startPolling,
stopPolling,
data,
refetch,
],
);

Expand Down
53 changes: 34 additions & 19 deletions app/views/ExploreDeep/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext, useEffect, useMemo, useState, useCallback } from 'react';
import {
isDefined,
randomString,
formatDateToString,
} from '@togglecorp/fujs';
import {
Expand Down Expand Up @@ -447,18 +448,28 @@ function ExploreDeep(props: Props) {
variables,
},
);
// FIXME: randomId is used to create different query variables after each poll
// so that apollo doesn't create unnecessary cache
const [randomId, setRandomId] = useState<string>(randomString());

const queryVariables = useMemo(() => (
exportIdToDownload ? ({
id: exportIdToDownload,
randomId,
}) : undefined
), [
exportIdToDownload,
randomId,
]);

const {
data: genericExportData,
startPolling,
stopPolling,
refetch,
} = useQuery<GenericExportQuery, GenericExportQueryVariables>(
GENERIC_EXPORT,
{
skip: !exportIdToDownload,
variables: exportIdToDownload ? {
id: exportIdToDownload,
} : undefined,
skip: !queryVariables,
variables: queryVariables,
onCompleted: (response) => {
if (!response?.genericExport) {
setExportIdToDownload(undefined);
Expand Down Expand Up @@ -543,22 +554,26 @@ function ExploreDeep(props: Props) {

useEffect(
() => {
const shouldPoll = exportIdToDownload
&& genericExportData?.genericExport?.status !== 'SUCCESS'
&& genericExportData?.genericExport?.status !== 'FAILURE';

if (shouldPoll) {
startPolling(5000);
} else {
stopPolling();
}
const timeout = setTimeout(
() => {
const shouldPoll = genericExportData?.genericExport?.status === 'PENDING'
|| genericExportData?.genericExport?.status === 'STARTED';

if (shouldPoll) {
setRandomId(randomString());
refetch();
}
},
2000,
);

return () => {
clearTimeout(timeout);
};
},
[
removeAlert,
exportIdToDownload,
genericExportData,
startPolling,
stopPolling,
refetch,
],
);

Expand Down
Loading