({
forceRefresh = false,
key,
legacyFields = [],
load,
priority = FOREGROUND_PRIORITY,
})
| 175 | } |
| 176 | requestKeys.forEach((key) => { |
| 177 | const remaining = (foregroundRequests.get(key) ?? 1) - 1; |
| 178 | if (remaining > 0) { |
| 179 | foregroundRequests.set(key, remaining); |
| 180 | } else { |
| 181 | foregroundRequests.delete(key); |
| 182 | } |
| 183 | }); |
| 184 | }, |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | export async function loadCachedValue({ |
| 189 | forceRefresh = false, |
| 190 | key, |
| 191 | legacyFields = [], |
| 192 | load, |
| 193 | priority = FOREGROUND_PRIORITY, |
| 194 | }) { |
| 195 | const isBackground = priority === BACKGROUND_PRIORITY; |
| 196 | const requestEpoch = cacheEpoch; |
| 197 | const entry = await readCacheEntry(key, {legacyFields, requestEpoch}); |
| 198 | if (requestEpoch !== cacheEpoch) { |
| 199 | return entry?.value; |
| 200 | } |
| 201 | if (!isCacheEntryExpired(entry, forceRefresh)) { |
| 202 | return entry.value; |
| 203 | } |
| 204 | if (isBackground && foregroundRequests.has(key)) { |
| 205 | return entry?.value; |
| 206 | } |
| 207 | const loadMap = isBackground ? backgroundLoads : foregroundLoads; |
| 208 | if (loadMap.has(key)) { |
| 209 | return loadMap.get(key); |
| 210 | } |
| 211 | |
| 212 | const request = beginCacheRequests([key], priority, requestEpoch); |
| 213 | if (request.keys.length === 0) { |
| 214 | return entry?.value; |
| 215 | } |
| 216 | const pendingLoad = (async () => { |
| 217 | try { |
| 218 | const value = await load(); |
| 219 | const cacheWrite = writeCacheValue(key, value, { |
| 220 | requestEpoch: request.epoch, |
| 221 | requestVersion: request.versionFor(key), |
| 222 | }); |
| 223 | if (isBackground) { |
| 224 | await cacheWrite; |
| 225 | } else { |
| 226 | void cacheWrite.catch(() => { |
| 227 | }); |
| 228 | } |
| 229 | return value; |
| 230 | } finally { |
| 231 | request.release(); |
| 232 | } |
no test coverage detected