diff options
Diffstat (limited to 'src/state/queries')
-rw-r--r-- | src/state/queries/notifications/types.ts | 1 | ||||
-rw-r--r-- | src/state/queries/notifications/unread.tsx | 31 | ||||
-rw-r--r-- | src/state/queries/preferences/index.ts | 2 | ||||
-rw-r--r-- | src/state/queries/profile.ts | 2 |
4 files changed, 31 insertions, 5 deletions
diff --git a/src/state/queries/notifications/types.ts b/src/state/queries/notifications/types.ts index 86a9bb139..812236cf0 100644 --- a/src/state/queries/notifications/types.ts +++ b/src/state/queries/notifications/types.ts @@ -35,4 +35,5 @@ export interface CachedFeedPage { usableInFeed: boolean syncedAt: Date data: FeedPage | undefined + unreadCount: number } diff --git a/src/state/queries/notifications/unread.tsx b/src/state/queries/notifications/unread.tsx index d604e8fe0..49bb5a29d 100644 --- a/src/state/queries/notifications/unread.tsx +++ b/src/state/queries/notifications/unread.tsx @@ -25,7 +25,10 @@ type StateContext = string interface ApiContext { markAllRead: () => Promise<void> - checkUnread: (opts?: {invalidate?: boolean}) => Promise<void> + checkUnread: (opts?: { + invalidate?: boolean + isPoll?: boolean + }) => Promise<void> getCachedUnreadPage: () => FeedPage | undefined } @@ -50,6 +53,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { usableInFeed: false, syncedAt: new Date(), data: undefined, + unreadCount: 0, }) // periodic sync @@ -58,7 +62,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) { return } checkUnreadRef.current() // fire on init - const interval = setInterval(checkUnreadRef.current, UPDATE_INTERVAL) + const interval = setInterval( + () => checkUnreadRef.current?.({isPoll: true}), + UPDATE_INTERVAL, + ) return () => clearInterval(interval) }, [hasSession]) @@ -69,6 +76,12 @@ export function Provider({children}: React.PropsWithChildren<{}>) { usableInFeed: false, syncedAt: new Date(), data: undefined, + unreadCount: + data.event === '30+' + ? 30 + : data.event === '' + ? 0 + : parseInt(data.event, 10) || 1, } setNumUnread(data.event) } @@ -95,13 +108,24 @@ export function Provider({children}: React.PropsWithChildren<{}>) { } }, - async checkUnread({invalidate}: {invalidate?: boolean} = {}) { + async checkUnread({ + invalidate, + isPoll, + }: {invalidate?: boolean; isPoll?: boolean} = {}) { try { if (!getAgent().session) return if (AppState.currentState !== 'active') { return } + // reduce polling if unread count is set + if (isPoll && cacheRef.current?.unreadCount !== 0) { + // if hit 30+ then don't poll, otherwise reduce polling by 50% + if (cacheRef.current?.unreadCount >= 30 || Math.random() >= 0.5) { + return + } + } + // count const page = await fetchPage({ cursor: undefined, @@ -133,6 +157,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { usableInFeed: !!invalidate, // will be used immediately data: page, syncedAt: !lastIndexed || now > lastIndexed ? now : lastIndexed, + unreadCount, } // update & broadcast diff --git a/src/state/queries/preferences/index.ts b/src/state/queries/preferences/index.ts index a9aa7f26e..632d31a13 100644 --- a/src/state/queries/preferences/index.ts +++ b/src/state/queries/preferences/index.ts @@ -31,7 +31,7 @@ export function usePreferencesQuery() { return useQuery({ staleTime: STALE.SECONDS.FIFTEEN, structuralSharing: true, - refetchInterval: STALE.SECONDS.FIFTEEN, + refetchOnWindowFocus: true, queryKey: preferencesQueryKey, queryFn: async () => { const agent = getAgent() diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index 40ba0653c..21e2e5775 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -35,7 +35,7 @@ export function useProfileQuery({did}: {did: string | undefined}) { // if you remove it, the UI infinite-loops // -prf staleTime: isCurrentAccount ? STALE.SECONDS.THIRTY : STALE.MINUTES.FIVE, - refetchInterval: STALE.MINUTES.FIVE, + refetchOnWindowFocus: true, queryKey: RQKEY(did || ''), queryFn: async () => { const res = await getAgent().getProfile({actor: did || ''}) |