@@ -249,35 +249,38 @@ function allIncluded() {
249
249
log ( 'Incoming cacheKey:' , cacheKey ) ;
250
250
log ( 'Has data:' , ! ! githubCache . data ) ;
251
251
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
-
260
252
// Check if we need to load from storage
261
253
if ( ! githubCache . data && ! githubCache . fetching ) {
262
254
await loadFromStorage ( ) ;
263
255
} ;
264
256
265
257
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
+
266
274
// if fetching is in progress, queue the calls and return a promise resolved when done
267
275
if ( githubCache . fetching ) {
276
+ log ( 'Fetch in progress, queuing requests' ) ;
268
277
return new Promise ( ( resolve , reject ) => {
269
278
githubCache . queue . push ( { resolve, reject } ) ;
270
279
} ) ;
271
280
}
272
281
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
-
280
282
githubCache . fetching = true ;
283
+ githubCache . cacheKey = cacheKey ;
281
284
282
285
const issueUrl = `https://api.github.com/search/issues?q=author%3A${ githubUsername } +org%3Afossasia+created%3A${ startingDate } ..${ endingDate } &per_page=100` ;
283
286
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() {
304
307
// Cache the data
305
308
githubCache . data = { githubIssuesData, githubPrsReviewData, githubUserData } ;
306
309
githubCache . timestamp = Date . now ( ) ;
307
- githubCache . cacheKey = cacheKey ;
308
310
309
311
// updateCache({ githubIssuesData, githubPrsReviewData, githubUserData });
310
312
await saveToStorage ( githubCache . data ) ; // Save to storage
@@ -313,14 +315,15 @@ function allIncluded() {
313
315
// Resolve queued calls
314
316
githubCache . queue . forEach ( ( { resolve } ) => resolve ( ) ) ;
315
317
githubCache . queue = [ ] ;
316
- githubCache . fetching = false ;
317
318
} catch ( err ) {
318
319
logError ( 'Fetch Failed:' , err ) ;
319
320
// Reject queued calls on error
320
321
githubCache . queue . forEach ( ( { reject } ) => reject ( err ) ) ;
321
322
githubCache . queue = [ ] ;
322
323
githubCache . fetching = false ;
323
324
throw err ;
325
+ } finally {
326
+ githubCache . fetching = false ;
324
327
}
325
328
}
326
329
@@ -415,16 +418,18 @@ function allIncluded() {
415
418
} ) ;
416
419
}
417
420
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
+ // }
420
432
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
- }
428
433
//load initial scrum subject
429
434
function scrumSubjectLoaded ( ) {
430
435
if ( ! enableToggle ) return ;
0 commit comments