Skip to content

Commit

Permalink
Remove refetch attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliveriver committed Aug 7, 2024
1 parent 109c6dc commit 76ce465
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
35 changes: 16 additions & 19 deletions client/src/components/context/WorldContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import World from '../../types/world';
import Order, { OrderStatus } from '../../types/order';
import useGetWorld from '../../hooks/api/useGetWorld';
import useSubmitOrders from '../../hooks/api/useSubmitOrders';
import { refetchAttempts, refetchInterval } from '../../utils/constants';
import { refetchInterval } from '../../utils/constants';
import GameContext from './GameContext';
import Nation from '../../types/enums/nation';

Expand Down Expand Up @@ -43,24 +43,21 @@ export const WorldContextProvider = ({ children }: PropsWithChildren) => {
const isWaitingForAdjudication =
world?.orders.some((order) => order.status === OrderStatus.New) ?? false;

const refetchUntilUpdate = useCallback(
async (attempts: number) => {
setIsRefetching(true);
const currentIteration = world?.iteration;
const refetched = await refetch();
if (attempts <= 1 || refetched.error || currentIteration !== refetched.data?.iteration) {
setIsRefetching(false);
return refetched;
}
const refetchUntilUpdate = useCallback(async () => {
setIsRefetching(true);
const currentIteration = world?.iteration;
const refetched = await refetch();
if (refetched.error || currentIteration !== refetched.data?.iteration) {
setIsRefetching(false);
return refetched;
}

await new Promise((resolve) => {
setTimeout(resolve, refetchInterval);
});
await new Promise((resolve) => {
setTimeout(resolve, refetchInterval);
});

return refetchUntilUpdate(attempts - 1);
},
[world?.iteration, refetch],
);
return refetchUntilUpdate();
}, [world?.iteration, refetch]);

const contextValue = useMemo(
() => ({
Expand All @@ -69,11 +66,11 @@ export const WorldContextProvider = ({ children }: PropsWithChildren) => {
if (!game || !world) return;
const players = game.player ? [game.player] : Object.values(Nation);
submitOrders({ gameId: game.id, players, orders });
await refetchUntilUpdate(refetchAttempts);
await refetchUntilUpdate();
},
isLoading: isLoading || isSubmitting || isRefetching || isWaitingForAdjudication,
error: worldError || submissionError,
retry: () => refetchUntilUpdate(refetchAttempts),
retry: () => refetchUntilUpdate(),
}),
[
game,
Expand Down
2 changes: 0 additions & 2 deletions client/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ export const orderArrowEndSeparation = 10;

// API

// TODO change both of these for production
export const refetchInterval = 2000;
export const refetchAttempts = 3;

0 comments on commit 76ce465

Please sign in to comment.