Skip to content

Commit

Permalink
try using a useRef to prevent multiple new game calls
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Dec 10, 2024
1 parent 03e1936 commit 26bf6fb
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/components/DoomCanvas/DoomCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import { HydraMultiplayerClient } from "../../utils/HydraMultiplayer/client.js";
import cx from "classnames";
import { NETWORK_ID } from "../../constants.js";

let alreadyFetched: NewGameResponse | undefined = undefined;

const DoomCanvas: React.FC = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const {
Expand Down Expand Up @@ -50,14 +48,10 @@ const DoomCanvas: React.FC = () => {
}
const url =
type === EGameType.HOST ? newGame(address!) : addPlayer(address!, code);
if (!!alreadyFetched) {
return alreadyFetched;
} else {
console.log("fetching", mutationKey);
const response = await fetch(url);
alreadyFetched = await response.json();
return alreadyFetched as any;
}
console.log("fetching", mutationKey);
const response = await fetch(url);
const json = await response.json();
return json;
},
});

Expand All @@ -70,10 +64,12 @@ const DoomCanvas: React.FC = () => {
[],
);

const hasFetched = useRef(false);
useEffect(() => {
if (!address || !region) return;
if (!address || !region || hasFetched.current) return;

fetchGameData();
hasFetched.current = true;
}, [address, fetchGameData, region]);

useEffect(() => {
Expand Down

0 comments on commit 26bf6fb

Please sign in to comment.