MCPcopy Create free account
hub / github.com/TanStack/query / retrieveQuery

Function retrieveQuery

packages/query-persist-client-core/src/createPersister.ts:125–167  ·  view source on GitHub ↗
(
    queryHash: string,
    afterRestoreMacroTask?: (persistedQuery: PersistedQuery) => void,
  )

Source from the content-addressed store, hash-verified

123 }
124
125 async function retrieveQuery<T>(
126 queryHash: string,
127 afterRestoreMacroTask?: (persistedQuery: PersistedQuery) => void,
128 ) {
129 if (storage != null) {
130 const storageKey = `${prefix}-${queryHash}`
131 try {
132 const storedData = await storage.getItem(storageKey)
133 if (storedData) {
134 let persistedQuery: PersistedQuery
135 try {
136 persistedQuery = await deserialize(storedData)
137 } catch {
138 await storage.removeItem(storageKey)
139 return
140 }
141
142 if (isExpiredOrBusted(persistedQuery)) {
143 await storage.removeItem(storageKey)
144 } else {
145 if (afterRestoreMacroTask) {
146 // Just after restoring we want to get fresh data from the server if it's stale
147 notifyManager.schedule(() =>
148 afterRestoreMacroTask(persistedQuery),
149 )
150 }
151 // We must resolve the promise here, as otherwise we will have `loading` state in the app until `queryFn` resolves
152 return persistedQuery.state.data as T
153 }
154 }
155 } catch (err) {
156 if (process.env.NODE_ENV === 'development') {
157 console.error(err)
158 console.warn(
159 'Encountered an error attempting to restore query cache from persisted location.',
160 )
161 }
162 await storage.removeItem(storageKey)
163 }
164 }
165
166 return
167 }
168
169 async function persistQueryByKey(
170 queryKey: QueryKey,

Callers 1

persisterFnFunction · 0.85

Calls 1

isExpiredOrBustedFunction · 0.85

Tested by

no test coverage detected