Add asyncstorage query persistence

This commit is contained in:
Paul Frazee
2024-03-05 11:34:28 -08:00
parent 14a99cf3fc
commit e794e84b48
6 changed files with 73 additions and 10 deletions
+20 -1
View File
@@ -1,6 +1,11 @@
import {AppState, AppStateStatus} from 'react-native'
import {QueryClient, focusManager} from '@tanstack/react-query'
import {QueryClient, focusManager, Query} from '@tanstack/react-query'
import {isNative} from '#/platform/detection'
import {createAsyncStoragePersister} from '@tanstack/query-async-storage-persister'
import AsyncStorage from '@react-native-async-storage/async-storage'
// any query keys in this array will be persisted to AsyncStorage
const STORED_CACHE_QUERY_KEYS = ['labelers-detailed-info']
focusManager.setEventListener(onFocus => {
if (isNative) {
@@ -48,3 +53,17 @@ export const queryClient = new QueryClient({
},
},
})
export const asyncStoragePersister = createAsyncStoragePersister({
storage: AsyncStorage,
key: 'queryCache',
})
export const dehydrateOptions = {
shouldDehydrateMutation: (_: any) => false,
shouldDehydrateQuery: (query: any) => {
// something is wrong with useQuery's exported types, so we have to coerce here
const q = query as Query
return STORED_CACHE_QUERY_KEYS.includes(String(q.queryKey[0]))
},
}