Skip to content

Commit

Permalink
Fix stale Notifications after push (#3507)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon authored Apr 12, 2024
1 parent 14208ee commit 835f2e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/lib/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {QueryClient} from '@tanstack/react-query'

import {logger} from '#/logger'
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed'
import {invalidateCachedUnreadPage} from '#/state/queries/notifications/unread'
import {truncateAndInvalidate} from '#/state/queries/util'
import {getAgent, SessionAccount} from '#/state/session'
import {track} from 'lib/analytics/analytics'
Expand Down Expand Up @@ -87,6 +88,7 @@ export function useNotificationsListener(queryClient: QueryClient) {
// handle notifications that are received, both in the foreground or background
// NOTE: currently just here for debug logging
const sub1 = Notifications.addNotificationReceivedListener(event => {
invalidateCachedUnreadPage()
logger.debug(
'Notifications: received',
{event},
Expand Down Expand Up @@ -131,11 +133,13 @@ export function useNotificationsListener(queryClient: QueryClient) {
)
track('Notificatons:OpenApp')
logEvent('notifications:openApp', {})
invalidateCachedUnreadPage()
truncateAndInvalidate(queryClient, RQKEY_NOTIFS())
resetToTab('NotificationsTab') // open notifications tab
}
},
)

return () => {
sub1.remove()
sub2.remove()
Expand Down
34 changes: 27 additions & 7 deletions src/state/queries/notifications/unread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
*/

import React from 'react'
import {AppState} from 'react-native'
import * as Notifications from 'expo-notifications'
import {useQueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'

import BroadcastChannel from '#/lib/broadcast'
import {useSession, getAgent} from '#/state/session'
import {useModerationOpts} from '../preferences'
import {fetchPage} from './util'
import {CachedFeedPage, FeedPage} from './types'
import {logger} from '#/logger'
import {isNative} from '#/platform/detection'
import {useMutedThreads} from '#/state/muted-threads'
import {RQKEY as RQKEY_NOTIFS} from './feed'
import {logger} from '#/logger'
import {getAgent, useSession} from '#/state/session'
import {useModerationOpts} from '../preferences'
import {truncateAndInvalidate} from '../util'
import {AppState} from 'react-native'
import {RQKEY as RQKEY_NOTIFS} from './feed'
import {CachedFeedPage, FeedPage} from './types'
import {fetchPage} from './util'

const UPDATE_INTERVAL = 30 * 1e3 // 30sec

const broadcast = new BroadcastChannel('NOTIFS_BROADCAST_CHANNEL')

const emitter = new EventEmitter()

type StateContext = string

interface ApiContext {
Expand Down Expand Up @@ -56,6 +60,18 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
unreadCount: 0,
})

React.useEffect(() => {
function markAsUnusable() {
if (cacheRef.current) {
cacheRef.current.usableInFeed = false
}
}
emitter.addListener('invalidate', markAsUnusable)
return () => {
emitter.removeListener('invalidate', markAsUnusable)
}
}, [])

// periodic sync
React.useEffect(() => {
if (!hasSession || !checkUnreadRef.current) {
Expand Down Expand Up @@ -214,3 +230,7 @@ function countUnread(page: FeedPage) {
}
return num
}

export function invalidateCachedUnreadPage() {
emitter.emit('invalidate')
}

0 comments on commit 835f2e6

Please sign in to comment.