From 7025957256bba6bac5467be828b1d04432d0c28c Mon Sep 17 00:00:00 2001 From: MinhxNguyen7 Date: Sat, 26 Aug 2023 15:06:44 +0700 Subject: [PATCH] Lock and retry for grades cache --- apps/antalmanac/src/lib/helpers.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/antalmanac/src/lib/helpers.ts b/apps/antalmanac/src/lib/helpers.ts index 25ea77d23..34d2af0ec 100644 --- a/apps/antalmanac/src/lib/helpers.ts +++ b/apps/antalmanac/src/lib/helpers.ts @@ -229,7 +229,7 @@ export interface Grades { gradeNPCount: number; } -const gradesCache: { [key: string]: Grades } = {}; +const gradesCache: { [key: string]: Grades | null } = {}; /* * Query the PeterPortal GraphQL API for a course's grades with caching @@ -240,14 +240,18 @@ const gradesCache: { [key: string]: Grades } = {}; * * @returns Grades */ -export async function queryGrades(deptCode: string, courseNumber: string, instructor = '') { +export async function queryGrades(deptCode: string, courseNumber: string, instructor = ''): Promise { instructor = instructor.replace('STAFF', '').trim(); // Ignore STAFF const instructorFilter = instructor ? `instructor: "${instructor}"` : ''; const cacheKey = deptCode + courseNumber + instructor; if (gradesCache[cacheKey]) { - return gradesCache[cacheKey]; + // If cache is undefined, there's a request in progress + while (gradesCache[cacheKey] !== null) { + await new Promise((resolve) => setTimeout(resolve, 200)); // Wait before checking cache again + } + return gradesCache[cacheKey] as Grades; } const queryString = `{