Skip to content

Commit

Permalink
Lock and retry for grades cache
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhxNguyen7 committed Aug 26, 2023
1 parent ead9dad commit 7025957
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions apps/antalmanac/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Grades> {
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 = `{
Expand Down

0 comments on commit 7025957

Please sign in to comment.