1
- import { useCallback , useContext } from "react" ;
1
+ import { useCallback , useContext , useMemo , useRef } from "react" ;
2
2
import { GameContext } from "./GameContext" ;
3
3
import { CreateGameRequest } from "./Models" ;
4
4
5
5
type UpdateGameRequest =
6
6
| {
7
- type : "StartGame" ;
8
- }
7
+ type : "StartGame" ;
8
+ }
9
9
| {
10
- type : "LeaveGame" ;
11
- }
10
+ type : "LeaveGame" ;
11
+ }
12
12
| {
13
- type : "PickQuestion" ;
14
- questionId : string ;
15
- }
13
+ type : "PickQuestion" ;
14
+ questionId : string ;
15
+ }
16
16
| {
17
- type : "AllowAnswering" ;
18
- }
17
+ type : "AllowAnswering" ;
18
+ }
19
19
| {
20
- type : "AnswerQuestion" ;
21
- }
20
+ type : "AnswerQuestion" ;
21
+ }
22
22
| {
23
- type : "ConfirmAnswer" ;
24
- isCorrect : boolean ;
25
- }
23
+ type : "ConfirmAnswer" ;
24
+ isCorrect : boolean ;
25
+ }
26
26
| {
27
- type : "EndQuestion" ;
28
- }
27
+ type : "EndQuestion" ;
28
+ }
29
29
| {
30
- type : "UpdatePlayerScore" ;
31
- updateUsername : string ;
32
- newScore : number ;
33
- } ;
30
+ type : "UpdatePlayerScore" ;
31
+ updateUsername : string ;
32
+ newScore : number ;
33
+ } ;
34
34
35
35
export default function useApiClient ( ) {
36
36
const { addError, sendWsMessage } = useContext ( GameContext ) ;
@@ -74,8 +74,8 @@ export default function useApiClient() {
74
74
[ sendWsMessage ]
75
75
) ;
76
76
77
- return {
78
- createGame : useCallback (
77
+ const apiClient = useRef ( {
78
+ createGame :
79
79
( request : CreateGameRequest ) => {
80
80
return execute ( async ( ) => {
81
81
const res = await fetch ( `/api/games` , {
@@ -87,53 +87,43 @@ export default function useApiClient() {
87
87
return await res . json ( ) ;
88
88
} ) ;
89
89
} ,
90
- [ execute ]
91
- ) ,
92
- getGame : useCallback (
90
+ getGame :
93
91
( gameId : string ) => {
94
92
return execute ( async ( ) => {
95
93
const res = await fetch ( `/api/games/${ gameId } ` ) ;
96
94
if ( res . status >= 500 ) throw Error ( `Game could not be found` ) ;
97
95
return await res . json ( ) ;
98
96
} ) ;
99
97
} ,
100
- [ execute ]
101
- ) ,
102
- leaveGame : useCallback ( ( ) => {
98
+ leaveGame : ( ) => {
103
99
return executeWs ( { type : "LeaveGame" } ) ;
104
- } , [ executeWs ] ) ,
105
- startGame : useCallback ( ( ) => {
100
+ } ,
101
+ startGame : ( ) => {
106
102
return executeWs ( { type : "StartGame" } ) ;
107
- } , [ executeWs ] ) ,
108
- pickQuestion : useCallback (
103
+ } ,
104
+ pickQuestion :
109
105
( questionId : string ) => {
110
106
return executeWs ( { type : "PickQuestion" , questionId : questionId } ) ;
111
107
} ,
112
- [ executeWs ]
113
- ) ,
114
- answerQuestion : useCallback ( ( ) => {
108
+ answerQuestion : ( ) => {
115
109
return executeWs ( { type : "AnswerQuestion" } ) ;
116
- } , [ executeWs ] ) ,
117
- allowAnswering : useCallback ( ( ) => {
110
+ } ,
111
+ allowAnswering : ( ) => {
118
112
return executeWs ( { type : "AllowAnswering" } ) ;
119
- } , [ executeWs ] ) ,
120
- confirmAnswer : useCallback (
113
+ } ,
114
+ confirmAnswer :
121
115
( isCorrect : boolean ) => {
122
116
return executeWs ( { type : "ConfirmAnswer" , isCorrect : isCorrect } ) ;
123
117
} ,
124
- [ executeWs ]
125
- ) ,
126
- endQuestion : useCallback ( ( ) => {
118
+ endQuestion : ( ) => {
127
119
return executeWs ( { type : "EndQuestion" } ) ;
128
- } , [ executeWs ] ) ,
129
- updatePlayerScore : useCallback (
120
+ } ,
121
+ updatePlayerScore :
130
122
( updateUsername : string , newScore : number ) => {
131
123
return executeWs ( { type : "UpdatePlayerScore" , updateUsername : updateUsername , newScore : newScore } ) ;
132
124
} ,
133
- [ executeWs ]
134
- ) ,
135
125
136
- getQuestionsFromOpenTDB : useCallback (
126
+ getQuestionsFromOpenTDB :
137
127
( { category, difficulty } : { category : number ; difficulty : string } ) => {
138
128
return execute ( async ( ) => {
139
129
const res = await fetch (
@@ -144,9 +134,7 @@ export default function useApiClient() {
144
134
return await res . json ( ) ;
145
135
} ) ;
146
136
} ,
147
- [ execute ]
148
- ) ,
149
- getQuestionsFromTriviaApi : useCallback (
137
+ getQuestionsFromTriviaApi :
150
138
( category : string , difficulty : string ) => {
151
139
return execute ( async ( ) => {
152
140
const res = await fetch (
@@ -157,14 +145,14 @@ export default function useApiClient() {
157
145
return await res . json ( ) ;
158
146
} ) ;
159
147
} ,
160
- [ execute ]
161
- ) ,
162
- getStats : useCallback ( ( ) => {
148
+ getStats : ( ) => {
163
149
return execute ( async ( ) => {
164
150
const res = await fetch ( `/api/stats` ) ;
165
151
if ( res . status >= 500 ) throw Error ( `Error fetching stats` ) ;
166
152
return await res . json ( ) ;
167
153
} ) ;
168
- } , [ execute ] ) ,
169
- } ;
154
+ } ,
155
+ } ) ;
156
+
157
+ return apiClient . current ;
170
158
}
0 commit comments