From fabe4de7db159a0eab5e4ec8236f84cc1ef0c31a Mon Sep 17 00:00:00 2001 From: Sadaf-A Date: Fri, 12 Jul 2024 18:05:12 +0530 Subject: [PATCH 1/5] feat: fetch from network if there is an error in cache --- .../fetch-and-display-previews.ts | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index 4dfc9b6c..f70e7a16 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -16,39 +16,43 @@ export type Options = { }; export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, options = { ordering: "normal" }) { - let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems; - const _accessToken = await getGitHubAccessToken(); + try { + let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems; + const _accessToken = await getGitHubAccessToken(); - // Refresh the storage if there is no logged-in object in cachedTasks but there is one now. - if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) { - localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); - return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } + // Refresh the storage if there is no logged-in object in cachedTasks but there is one now. + if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } - // If previously logged in but not anymore, clear cache and fetch from network. - if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) { - localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); - return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } + // If previously logged in but not anymore, clear cache and fetch from network. + if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } - // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes - if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { - _cachedTasks = { - timestamp: Date.now(), - tasks: [], - loggedIn: _accessToken !== null, - }; - } + // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes + if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { + _cachedTasks = { + timestamp: Date.now(), + tasks: [], + loggedIn: _accessToken !== null, + }; + } - const cachedTasks = _cachedTasks.tasks as TaskMaybeFull[]; - taskManager.syncTasks(cachedTasks); + const cachedTasks = _cachedTasks.tasks as TaskMaybeFull[]; + taskManager.syncTasks(cachedTasks); - if (!cachedTasks.length) { - // load from network if there are no cached issues + if (!cachedTasks.length) { + // load from network if there are no cached issues + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } else { + displayGitHubIssues(sorting, options); + return fetchAvatars(); + } + } catch (cacheError) { return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } else { - displayGitHubIssues(sorting, options); - return fetchAvatars(); } } From 7509cecab218fb2e86058c75f8917f947d26aa9e Mon Sep 17 00:00:00 2001 From: Sadaf-A Date: Fri, 12 Jul 2024 18:06:14 +0530 Subject: [PATCH 2/5] feat: fetch from network if there is a cache error --- src/home/fetch-github/fetch-and-display-previews.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index f70e7a16..0b3aeffa 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -50,7 +50,7 @@ export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, option } else { displayGitHubIssues(sorting, options); return fetchAvatars(); - } + } } catch (cacheError) { return fetchAndDisplayPreviewsFromNetwork(sorting, options); } From b3c6bef533b8064eb4c22fa3fbcf17337a0c13db Mon Sep 17 00:00:00 2001 From: Sadaf-A Date: Fri, 12 Jul 2024 18:26:35 +0530 Subject: [PATCH 3/5] fix: formatting code and using try and catch block for relevant parts only --- .../fetch-and-display-previews.ts | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index 0b3aeffa..a07f4143 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -16,31 +16,30 @@ export type Options = { }; export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, options = { ordering: "normal" }) { - try { - let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems; - const _accessToken = await getGitHubAccessToken(); - - // Refresh the storage if there is no logged-in object in cachedTasks but there is one now. - if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) { - localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); - return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } + let _cachedTasks = getLocalStore(GITHUB_TASKS_STORAGE_KEY) as TaskStorageItems; + const _accessToken = await getGitHubAccessToken(); - // If previously logged in but not anymore, clear cache and fetch from network. - if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) { - localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); - return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } + // Refresh the storage if there is no logged-in object in cachedTasks but there is one now. + if (_cachedTasks && !_cachedTasks.loggedIn && _accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } - // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes - if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { - _cachedTasks = { - timestamp: Date.now(), - tasks: [], - loggedIn: _accessToken !== null, - }; - } + // If previously logged in but not anymore, clear cache and fetch from network. + if (_cachedTasks && _cachedTasks.loggedIn && !_accessToken) { + localStorage.removeItem(GITHUB_TASKS_STORAGE_KEY); + return fetchAndDisplayPreviewsFromNetwork(sorting, options); + } + // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes + if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { + _cachedTasks = { + timestamp: Date.now(), + tasks: [], + loggedIn: _accessToken !== null, + }; + } + try { const cachedTasks = _cachedTasks.tasks as TaskMaybeFull[]; taskManager.syncTasks(cachedTasks); @@ -53,8 +52,8 @@ export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, option } } catch (cacheError) { return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } -} + } +} export async function fetchAndDisplayPreviewsFromNetwork(sorting?: Sorting, options = { ordering: "normal" }) { const fetchedPreviews = await fetchIssuePreviews(); From 6ba7c8542c0f1b3621191dd23870ada50142284d Mon Sep 17 00:00:00 2001 From: Sadaf-A Date: Fri, 12 Jul 2024 18:27:23 +0530 Subject: [PATCH 4/5] fix: formatting fix and using try catch block for relevant part only --- src/home/fetch-github/fetch-and-display-previews.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index a07f4143..56366635 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -52,8 +52,8 @@ export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, option } } catch (cacheError) { return fetchAndDisplayPreviewsFromNetwork(sorting, options); - } -} + } +} export async function fetchAndDisplayPreviewsFromNetwork(sorting?: Sorting, options = { ordering: "normal" }) { const fetchedPreviews = await fetchIssuePreviews(); From fa8fa98a454feae074d91f5a4a9540e6e77fa591 Mon Sep 17 00:00:00 2001 From: Sadaf-A Date: Fri, 12 Jul 2024 18:35:51 +0530 Subject: [PATCH 5/5] fix: code fix --- .../fetch-github/fetch-and-display-previews.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/home/fetch-github/fetch-and-display-previews.ts b/src/home/fetch-github/fetch-and-display-previews.ts index 56366635..4b33bd8a 100644 --- a/src/home/fetch-github/fetch-and-display-previews.ts +++ b/src/home/fetch-github/fetch-and-display-previews.ts @@ -31,15 +31,15 @@ export async function fetchAndDisplayPreviewsFromCache(sorting?: Sorting, option return fetchAndDisplayPreviewsFromNetwork(sorting, options); } - // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes - if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { - _cachedTasks = { - timestamp: Date.now(), - tasks: [], - loggedIn: _accessToken !== null, - }; - } try { + // makes sure tasks have a timestamp to know how old the cache is, or refresh if older than 15 minutes + if (!_cachedTasks || !_cachedTasks.timestamp || _cachedTasks.timestamp + 60 * 1000 * 15 <= Date.now()) { + _cachedTasks = { + timestamp: Date.now(), + tasks: [], + loggedIn: _accessToken !== null, + }; + } const cachedTasks = _cachedTasks.tasks as TaskMaybeFull[]; taskManager.syncTasks(cachedTasks);