Skip to content

Commit e49882d

Browse files
committed
Fix issue with dependencies always updating
1 parent e97a583 commit e49882d

File tree

1 file changed

+44
-56
lines changed

1 file changed

+44
-56
lines changed

ui/src/useApiClient.ts

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
1-
import { useCallback, useContext } from "react";
1+
import { useCallback, useContext, useMemo, useRef } from "react";
22
import { GameContext } from "./GameContext";
33
import { CreateGameRequest } from "./Models";
44

55
type UpdateGameRequest =
66
| {
7-
type: "StartGame";
8-
}
7+
type: "StartGame";
8+
}
99
| {
10-
type: "LeaveGame";
11-
}
10+
type: "LeaveGame";
11+
}
1212
| {
13-
type: "PickQuestion";
14-
questionId: string;
15-
}
13+
type: "PickQuestion";
14+
questionId: string;
15+
}
1616
| {
17-
type: "AllowAnswering";
18-
}
17+
type: "AllowAnswering";
18+
}
1919
| {
20-
type: "AnswerQuestion";
21-
}
20+
type: "AnswerQuestion";
21+
}
2222
| {
23-
type: "ConfirmAnswer";
24-
isCorrect: boolean;
25-
}
23+
type: "ConfirmAnswer";
24+
isCorrect: boolean;
25+
}
2626
| {
27-
type: "EndQuestion";
28-
}
27+
type: "EndQuestion";
28+
}
2929
| {
30-
type: "UpdatePlayerScore";
31-
updateUsername: string;
32-
newScore: number;
33-
};
30+
type: "UpdatePlayerScore";
31+
updateUsername: string;
32+
newScore: number;
33+
};
3434

3535
export default function useApiClient() {
3636
const { addError, sendWsMessage } = useContext(GameContext);
@@ -74,8 +74,8 @@ export default function useApiClient() {
7474
[sendWsMessage]
7575
);
7676

77-
return {
78-
createGame: useCallback(
77+
const apiClient = useRef({
78+
createGame:
7979
(request: CreateGameRequest) => {
8080
return execute(async () => {
8181
const res = await fetch(`/api/games`, {
@@ -87,53 +87,43 @@ export default function useApiClient() {
8787
return await res.json();
8888
});
8989
},
90-
[execute]
91-
),
92-
getGame: useCallback(
90+
getGame:
9391
(gameId: string) => {
9492
return execute(async () => {
9593
const res = await fetch(`/api/games/${gameId}`);
9694
if (res.status >= 500) throw Error(`Game could not be found`);
9795
return await res.json();
9896
});
9997
},
100-
[execute]
101-
),
102-
leaveGame: useCallback(() => {
98+
leaveGame: () => {
10399
return executeWs({ type: "LeaveGame" });
104-
}, [executeWs]),
105-
startGame: useCallback(() => {
100+
},
101+
startGame: () => {
106102
return executeWs({ type: "StartGame" });
107-
}, [executeWs]),
108-
pickQuestion: useCallback(
103+
},
104+
pickQuestion:
109105
(questionId: string) => {
110106
return executeWs({ type: "PickQuestion", questionId: questionId });
111107
},
112-
[executeWs]
113-
),
114-
answerQuestion: useCallback(() => {
108+
answerQuestion: () => {
115109
return executeWs({ type: "AnswerQuestion" });
116-
}, [executeWs]),
117-
allowAnswering: useCallback(() => {
110+
},
111+
allowAnswering: () => {
118112
return executeWs({ type: "AllowAnswering" });
119-
}, [executeWs]),
120-
confirmAnswer: useCallback(
113+
},
114+
confirmAnswer:
121115
(isCorrect: boolean) => {
122116
return executeWs({ type: "ConfirmAnswer", isCorrect: isCorrect });
123117
},
124-
[executeWs]
125-
),
126-
endQuestion: useCallback(() => {
118+
endQuestion: () => {
127119
return executeWs({ type: "EndQuestion" });
128-
}, [executeWs]),
129-
updatePlayerScore: useCallback(
120+
},
121+
updatePlayerScore:
130122
(updateUsername: string, newScore: number) => {
131123
return executeWs({ type: "UpdatePlayerScore", updateUsername: updateUsername, newScore: newScore });
132124
},
133-
[executeWs]
134-
),
135125

136-
getQuestionsFromOpenTDB: useCallback(
126+
getQuestionsFromOpenTDB:
137127
({ category, difficulty }: { category: number; difficulty: string }) => {
138128
return execute(async () => {
139129
const res = await fetch(
@@ -144,9 +134,7 @@ export default function useApiClient() {
144134
return await res.json();
145135
});
146136
},
147-
[execute]
148-
),
149-
getQuestionsFromTriviaApi: useCallback(
137+
getQuestionsFromTriviaApi:
150138
(category: string, difficulty: string) => {
151139
return execute(async () => {
152140
const res = await fetch(
@@ -157,14 +145,14 @@ export default function useApiClient() {
157145
return await res.json();
158146
});
159147
},
160-
[execute]
161-
),
162-
getStats: useCallback(() => {
148+
getStats: () => {
163149
return execute(async () => {
164150
const res = await fetch(`/api/stats`);
165151
if (res.status >= 500) throw Error(`Error fetching stats`);
166152
return await res.json();
167153
});
168-
}, [execute]),
169-
};
154+
},
155+
});
156+
157+
return apiClient.current;
170158
}

0 commit comments

Comments
 (0)