1- async function processGHRepoCards ( ) {
1+ async function processGHRepoCards ( ENABLE_CACHING ) {
22
33 const GITHUB_API_SETTINGS_GHREPOCARDS = {
44 method : 'GET' ,
@@ -9,8 +9,29 @@ async function processGHRepoCards() {
99 } ;
1010
1111 const GITHUB_API_ENDPOINT_GHREPOCARDS = "https://api.github.com/" ;
12+
13+ // Null as arg to clear all.
14+ function clearDataLocal ( repoName ) {
15+ if ( ! ENABLE_CACHING ) return ;
16+ let l = localStorage . length ;
17+ for ( let i = 0 ; i < l ; i ++ ) {
18+ const key = localStorage . key ( i ) ;
19+ const value = localStorage . getItem ( key ) ;
20+ try {
21+ const parsedValue = JSON . parse ( value ) ;
22+ if ( parsedValue && typeof parsedValue === 'object' ) {
23+ if ( parsedValue . expiry && parsedValue . repoData && ( repoName === null || repoName === parsedValue . repoData . name ) )
24+ localStorage . removeItem ( key ) ;
25+ }
26+ } catch ( error ) {
27+ localStorage . removeItem ( key ) ;
28+ throw new Error ( `Error in clearDataLocal(): ${ error } .` ) ;
29+ }
30+ }
31+ }
1232
1333 function saveDataLocal ( repoData ) {
34+ if ( ! ENABLE_CACHING ) return ;
1435 const now = new Date ( ) ;
1536 let expirationHours = 1 ;
1637 const item = {
@@ -23,6 +44,7 @@ async function processGHRepoCards() {
2344
2445 function getDataLocal ( ) {
2546 const items = { } ;
47+ if ( ! ENABLE_CACHING ) return items ;
2648 let l = localStorage . length ;
2749 for ( let i = 0 ; i < l ; i ++ ) {
2850 const key = localStorage . key ( i ) ;
@@ -251,14 +273,14 @@ async function processGHRepoCards() {
251273 cardTag . style . display = "block" ;
252274 }
253275
276+ // Export functions to call it directly outside if needed.
254277 processGHRepoCards . saveDataLocal = saveDataLocal ;
255278 processGHRepoCards . getDataLocal = getDataLocal ;
256279 processGHRepoCards . putData = putData ;
257280 processGHRepoCards . getData = getData ;
258281 processGHRepoCards . getAllRepos = getAllRepos ;
259282 processGHRepoCards . getSingleRepo = getSingleRepo ;
283+ processGHRepoCards . clearDataLocal = clearDataLocal ;
260284
261285}
262286
263- processGHRepoCards ( ) ;
264-
0 commit comments