Skip to content

Commit c0d254d

Browse files
committed
impr: make leaderboard requests in parallel
1 parent 7b80bf8 commit c0d254d

File tree

1 file changed

+75
-37
lines changed

1 file changed

+75
-37
lines changed

frontend/src/ts/pages/leaderboards.ts

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,26 @@ async function requestData(update = false): Promise<void> {
203203
mode2: state.mode2,
204204
};
205205

206-
let response;
206+
const requests: {
207+
data:
208+
| ReturnType<typeof Ape.leaderboards.get>
209+
| ReturnType<typeof Ape.leaderboards.getDaily>
210+
| undefined;
211+
rank:
212+
| ReturnType<typeof Ape.leaderboards.getRank>
213+
| ReturnType<typeof Ape.leaderboards.getDailyRank>
214+
| undefined;
215+
} = {
216+
data: undefined,
217+
rank: undefined,
218+
};
207219

208220
if (state.type === "allTime") {
209-
response = await Ape.leaderboards.get({
221+
requests.data = Ape.leaderboards.get({
210222
query: { ...baseQuery, page: state.page },
211223
});
212224
} else {
213-
response = await Ape.leaderboards.getDaily({
225+
requests.data = Ape.leaderboards.getDaily({
214226
query: {
215227
...baseQuery,
216228
page: state.page,
@@ -219,40 +231,45 @@ async function requestData(update = false): Promise<void> {
219231
});
220232
}
221233

222-
if (response.status === 200) {
223-
state.data = response.body.data.entries;
224-
state.count = response.body.data.count;
225-
state.pageSize = response.body.data.pageSize;
234+
if (isAuthenticated() && state.userData === null) {
235+
if (state.type === "allTime") {
236+
requests.rank = Ape.leaderboards.getRank({
237+
query: { ...baseQuery },
238+
});
239+
} else {
240+
requests.rank = Ape.leaderboards.getDailyRank({
241+
query: {
242+
...baseQuery,
243+
},
244+
});
245+
}
246+
}
247+
248+
const [dataResponse, rankResponse] = await Promise.all([
249+
requests.data,
250+
requests.rank,
251+
]);
252+
253+
if (dataResponse.status === 200) {
254+
state.data = dataResponse.body.data.entries;
255+
state.count = dataResponse.body.data.count;
256+
state.pageSize = dataResponse.body.data.pageSize;
226257

227258
if (state.type === "daily") {
228259
//@ts-ignore not sure why this is causing errors when it's clearly defined in the schema
229260
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
230-
state.minWpm = response.body.data.minWpm;
261+
state.minWpm = dataResponse.body.data.minWpm;
231262
}
232263
} else {
233264
state.data = null;
234265
state.error = "Something went wrong";
235266
Notifications.add(
236-
"Failed to get leaderboard: " + response.body.message,
267+
"Failed to get leaderboard: " + dataResponse.body.message,
237268
-1
238269
);
239270
}
240271

241-
if (isAuthenticated() && state.userData === null) {
242-
let rankResponse;
243-
244-
if (state.type === "allTime") {
245-
rankResponse = await Ape.leaderboards.getRank({
246-
query: { ...baseQuery },
247-
});
248-
} else {
249-
rankResponse = await Ape.leaderboards.getDailyRank({
250-
query: {
251-
...baseQuery,
252-
},
253-
});
254-
}
255-
272+
if (state.userData === null && rankResponse !== undefined) {
256273
if (rankResponse.status === 200) {
257274
if (rankResponse.body.data !== null) {
258275
state.userData = rankResponse.body.data;
@@ -288,31 +305,52 @@ async function requestData(update = false): Promise<void> {
288305
}
289306
return;
290307
} else if (state.type === "weekly") {
291-
const data = await Ape.leaderboards.getWeeklyXp({
308+
const requests: {
309+
data: ReturnType<typeof Ape.leaderboards.getWeeklyXp> | undefined;
310+
rank: ReturnType<typeof Ape.leaderboards.getWeeklyXpRank> | undefined;
311+
} = {
312+
data: undefined,
313+
rank: undefined,
314+
};
315+
316+
requests.data = Ape.leaderboards.getWeeklyXp({
292317
query: { page: state.page, weeksBefore: state.lastWeek ? 1 : undefined },
293318
});
294319

295-
if (data.status === 200) {
296-
state.data = data.body.data.entries;
297-
state.count = data.body.data.count;
298-
state.pageSize = data.body.data.pageSize;
320+
if (isAuthenticated() && state.userData === null) {
321+
requests.rank = Ape.leaderboards.getWeeklyXpRank();
322+
}
323+
324+
const [dataResponse, rankResponse] = await Promise.all([
325+
requests.data,
326+
requests.rank,
327+
]);
328+
329+
if (dataResponse.status === 200) {
330+
state.data = dataResponse.body.data.entries;
331+
state.count = dataResponse.body.data.count;
332+
state.pageSize = dataResponse.body.data.pageSize;
299333
} else {
300334
state.data = null;
301335
state.error = "Something went wrong";
302-
Notifications.add("Failed to get leaderboard: " + data.body.message, -1);
336+
Notifications.add(
337+
"Failed to get leaderboard: " + dataResponse.body.message,
338+
-1
339+
);
303340
}
304341

305-
if (isAuthenticated() && state.userData === null) {
306-
const userData = await Ape.leaderboards.getWeeklyXpRank();
307-
308-
if (userData.status === 200) {
309-
if (userData.body.data !== null) {
310-
state.userData = userData.body.data;
342+
if (state.userData === null && rankResponse !== undefined) {
343+
if (rankResponse.status === 200) {
344+
if (rankResponse.body.data !== null) {
345+
state.userData = rankResponse.body.data;
311346
}
312347
} else {
313348
state.userData = null;
314349
state.error = "Something went wrong";
315-
Notifications.add("Failed to get rank: " + userData.body.message, -1);
350+
Notifications.add(
351+
"Failed to get rank: " + rankResponse.body.message,
352+
-1
353+
);
316354
}
317355
}
318356

0 commit comments

Comments
 (0)