Skip to content

Commit 486fab5

Browse files
committed
fixed datchange bug, new request being made on cacheKey change
Signed-off-by: Vedansh Saini <[email protected]>
1 parent 5d73047 commit 486fab5

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/scripts/scrumHelper.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,38 @@ function allIncluded() {
249249
log('Incoming cacheKey:', cacheKey);
250250
log('Has data:', !!githubCache.data);
251251

252-
// If cache exists but key differs, invalidate
253-
if(githubCache.cacheKey !== cacheKey && cacheKey !== null){ // add the condition of cacheKey not being null here
254-
log('Cache key mismatch, invalidating cache');
255-
githubCache.data = null;
256-
githubCache.cacheKey = cacheKey;
257-
}
258-
githubCache.cacheKey = cacheKey;
259-
260252
// Check if we need to load from storage
261253
if (!githubCache.data && !githubCache.fetching) {
262254
await loadFromStorage();
263255
};
264256

265257
const now = Date.now();
258+
const isCacheFresh = (now - githubCache.timestamp) < githubCache.ttl;
259+
const isCacheKeyMatch = githubCache.cacheKey === cacheKey;
260+
261+
if(githubCache.data && isCacheFresh & isCacheKeyMatch) {
262+
log('Using cached data - cache is fresh and key matches');
263+
processGithubData(githubCache.data);
264+
return Promise.resolve();
265+
}
266+
// if cache key does not match our cache is stale, fetch new data
267+
if(!isCacheKeyMatch) {
268+
log('Cache key mismatch - fetching new Data');
269+
githubCache.data = null;
270+
} else if(!isCacheFresh) {
271+
log('Cache is stale - fetching new data');
272+
}
273+
266274
// if fetching is in progress, queue the calls and return a promise resolved when done
267275
if (githubCache.fetching) {
276+
log('Fetch in progress, queuing requests');
268277
return new Promise((resolve, reject) => {
269278
githubCache.queue.push({ resolve, reject });
270279
});
271280
}
272281

273-
// use cached data if still fresh
274-
if(githubCache.data && (now - githubCache.timestamp < githubCache.ttl)) {
275-
log(`Using cached data`);
276-
processGithubData(githubCache.data);
277-
return Promise.resolve();
278-
}
279-
280282
githubCache.fetching = true;
283+
githubCache.cacheKey = cacheKey;
281284

282285
const issueUrl = `https://api.github.com/search/issues?q=author%3A${githubUsername}+org%3Afossasia+created%3A${startingDate}..${endingDate}&per_page=100`;
283286
const prUrl = `https://api.github.com/search/issues?q=author%3A${githubUsername}+org%3Afossasia+updated%3A${startingDate}..${endingDate}&per_page=100`;
@@ -304,7 +307,6 @@ function allIncluded() {
304307
// Cache the data
305308
githubCache.data = { githubIssuesData, githubPrsReviewData, githubUserData };
306309
githubCache.timestamp = Date.now();
307-
githubCache.cacheKey = cacheKey;
308310

309311
// updateCache({ githubIssuesData, githubPrsReviewData, githubUserData });
310312
await saveToStorage(githubCache.data); // Save to storage
@@ -313,14 +315,15 @@ function allIncluded() {
313315
// Resolve queued calls
314316
githubCache.queue.forEach(({ resolve }) => resolve());
315317
githubCache.queue = [];
316-
githubCache.fetching = false;
317318
} catch(err) {
318319
logError('Fetch Failed:', err);
319320
// Reject queued calls on error
320321
githubCache.queue.forEach(({ reject }) => reject(err));
321322
githubCache.queue = [];
322323
githubCache.fetching = false;
323324
throw err;
325+
} finally {
326+
githubCache.fetching = false;
324327
}
325328
}
326329

@@ -415,16 +418,18 @@ function allIncluded() {
415418
});
416419
}
417420

418-
function getProject() {
419-
if (projectName != '') return projectName;
421+
// depriciate this
422+
// function getProject() {
423+
// if (projectName != '') return projectName;
424+
425+
// let project = '<project name>';
426+
// let url = window.location.href;
427+
// let projectUrl = url.substr(url.lastIndexOf('/') + 1);
428+
// if (projectUrl === 'susiai') project = 'SUSI.AI';
429+
// else if (projectUrl === 'open-event') project = 'Open Event';
430+
// return project;
431+
// }
420432

421-
let project = '<project name>';
422-
let url = window.location.href;
423-
let projectUrl = url.substr(url.lastIndexOf('/') + 1);
424-
if (projectUrl === 'susiai') project = 'SUSI.AI';
425-
else if (projectUrl === 'open-event') project = 'Open Event';
426-
return project;
427-
}
428433
//load initial scrum subject
429434
function scrumSubjectLoaded() {
430435
if (!enableToggle) return;

0 commit comments

Comments
 (0)