Skip to content

Commit 4701c08

Browse files
committed
add: serializer and deserializer for virtual contest fetcher
1 parent 6fbfa47 commit 4701c08

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

atcoder-problems-frontend/src/api/APIClient.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -295,25 +295,45 @@ export const useVirtualContestSubmissions = (
295295
) => {
296296
const PROBLEM_CHUNK_SIZE = 10;
297297
const USER_CHUNK_SIZE = 10;
298-
const requestCount =
299-
Math.ceil(users.length / USER_CHUNK_SIZE) *
300-
Math.ceil(problems.length / PROBLEM_CHUNK_SIZE);
301-
302-
const refreshInterval = enableAutoRefresh
303-
? Math.max(1, requestCount / 10) * 60_000
304-
: 1_000_000_000;
298+
const SEPARATOR = "###";
305299

306-
const userChunks = toChunks(users, USER_CHUNK_SIZE);
307-
const problemChunks = toChunks(problems, PROBLEM_CHUNK_SIZE);
308300
const singleFetch = async (users: UserId[], problems: ProblemId[]) => {
309301
const userList = users.join(",");
310302
const problemList = problems.join(",");
311303
const url = `${ATCODER_API_URL}/v3/users_and_time?users=${userList}&problems=${problemList}&from=${fromSecond}&to=${toSecond}`;
312304
const submissions = await fetchTypedArray(url, isSubmission);
313305
return submissions.filter((submission) => isValidResult(submission.result));
314306
};
315-
316-
const fetcher = async () => {
307+
const serialize = (users: UserId[], problems: ProblemId[]) => {
308+
const sortedUsers = Array.from(users);
309+
sortedUsers.sort();
310+
const userKey = sortedUsers.join(",");
311+
312+
const sortedProblems = Array.from(problems);
313+
sortedProblems.sort();
314+
const problemKey = sortedProblems.join(",");
315+
316+
return (
317+
"useVirtualContestSubmissions" +
318+
SEPARATOR +
319+
userKey +
320+
SEPARATOR +
321+
problemKey
322+
);
323+
};
324+
const deserialize = (key: string) => {
325+
const keys = key.split("###");
326+
const userKey = keys[1];
327+
const problemKey = keys[2];
328+
329+
const users = userKey.split(",");
330+
const problems = problemKey.split(",");
331+
return { users, problems };
332+
};
333+
const fetcher = async (key: string) => {
334+
const { users, problems } = deserialize(key);
335+
const userChunks = toChunks(users, USER_CHUNK_SIZE);
336+
const problemChunks = toChunks(problems, PROBLEM_CHUNK_SIZE);
317337
const promises = userChunks
318338
.flatMap((users) =>
319339
problemChunks.map((problems) => ({ users, problems }))
@@ -323,20 +343,15 @@ export const useVirtualContestSubmissions = (
323343
return submissionChunks.flatMap((x) => x);
324344
};
325345

326-
const custom_key =
327-
"useVirtualContestSubmissions&" +
328-
(users.length === 0
329-
? "empty"
330-
: users.length === 1
331-
? users[0]
332-
: "multiuser");
333-
return useSWRData(
334-
custom_key,
335-
() => (users.length > 0 ? fetcher() : Promise.resolve([])),
336-
{
337-
refreshInterval,
338-
}
339-
);
346+
const requestCount =
347+
Math.ceil(users.length / USER_CHUNK_SIZE) *
348+
Math.ceil(problems.length / PROBLEM_CHUNK_SIZE);
349+
const refreshInterval = enableAutoRefresh
350+
? Math.max(1, requestCount / 10) * 60_000
351+
: 1_000_000_000;
352+
353+
const customKey = serialize(users, problems);
354+
return useSWRData(customKey, fetcher, { refreshInterval });
340355
};
341356

342357
export const useRecentSubmissions = () => {

0 commit comments

Comments
 (0)