(
queryHash: string,
afterRestoreMacroTask?: (persistedQuery: PersistedQuery) => void,
)
| 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, |
no test coverage detected