-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update notifications-fetching to occur less frequently and share resu…
…lts across tabs
- Loading branch information
Showing
3 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as session from './session.js' | ||
|
||
export async function getClearedAt () { | ||
const cached = getCache('cleared-at') | ||
if (cached) return cached | ||
setCache('cleared-at', undefined) // "lock" by updating the cache ttl | ||
const res = await session.ctzn.view('ctzn.network/notifications-cleared-at-view') | ||
setCache('cleared-at', res?.notificationsClearedAt) | ||
return res?.notificationsClearedAt | ||
} | ||
|
||
export async function updateClearedAt () { | ||
const cached = getCache('updated-cleared-at') | ||
if (!cached && document.hasFocus()) { | ||
setCache('updated-cleared-at', true) | ||
await session.api.notifications.updateNotificationsClearedAt() | ||
} | ||
} | ||
|
||
export async function countUnread () { | ||
const cached = getCache('unread') | ||
if (cached) return cached | ||
setCache('unread', undefined) // "lock" by updating the cache ttl | ||
const clearedAt = await getClearedAt() | ||
const count = (await session.ctzn.view('ctzn.network/notifications-count-view', {after: clearedAt})).count | ||
setCache('unread', count) | ||
return count | ||
} | ||
|
||
function setCache (key, value, ttl = 30e3/* 30 seconds */) { | ||
localStorage.setItem(`notes-cache:${key}`, JSON.stringify({ | ||
expires: Date.now() + ttl, | ||
value | ||
})) | ||
} | ||
|
||
function getCache (key) { | ||
try { | ||
const obj = JSON.parse(localStorage.getItem(`notes-cache:${key}`)) | ||
if (document.hasFocus() && obj.expires < Date.now()) { | ||
// NOTE: | ||
// we used the cached value if the tab isnt focused | ||
// ...or if it's still fresh | ||
// this is to reduce the number of server pings | ||
// -prf | ||
return undefined | ||
} | ||
return obj.value | ||
} catch (e) { | ||
return undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65dc487
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: