(displayNames, isRefresh = false, {priority = FOREGROUND_PRIORITY} = {})
| 199 | } |
| 200 | |
| 201 | export async function getMetadataBatch(displayNames, isRefresh = false, {priority = FOREGROUND_PRIORITY} = {}) { |
| 202 | const uniqueDisplayNames = [...new Set(displayNames)].filter(Boolean); |
| 203 | const requestEpoch = getCacheEpoch(); |
| 204 | const entries = await Promise.all(uniqueDisplayNames.map((displayName) => ( |
| 205 | readCacheEntry(metadataCacheKey(displayName), {legacyFields: ["result"], requestEpoch}) |
| 206 | ))); |
| 207 | if (requestEpoch !== getCacheEpoch()) { |
| 208 | return {}; |
| 209 | } |
| 210 | |
| 211 | const cachedMetadata = Object.fromEntries(uniqueDisplayNames.flatMap((displayName, index) => ( |
| 212 | entries[index] ? [[displayName, metadataWithLatestYear(entries[index].value)]] : [] |
| 213 | ))); |
| 214 | const expiredDisplayNames = uniqueDisplayNames.filter((displayName, index) => ( |
| 215 | isCacheEntryExpired(entries[index], isRefresh) |
| 216 | )); |
| 217 | const request = beginCacheRequests(expiredDisplayNames.map(metadataCacheKey), priority, requestEpoch); |
| 218 | const requestDisplayNames = request.keys.map(displayNameFromMetadataCacheKey); |
| 219 | |
| 220 | if (requestDisplayNames.length === 0) { |
| 221 | return cachedMetadata; |
| 222 | } |
| 223 | |
| 224 | try { |
| 225 | const response = await apiJson(GET_METADATA_BATCH, { |
| 226 | body: {display_names: requestDisplayNames}, |
| 227 | fetchPriority: priority, |
| 228 | }); |
| 229 | const refreshedRawMetadata = requestDisplayNames.reduce((metadataByName, displayName) => ({ |
| 230 | ...metadataByName, |
| 231 | [displayName]: response.data?.[displayName] ?? null, |
| 232 | }), {}); |
| 233 | const cacheWrites = requestDisplayNames.map((displayName) => { |
| 234 | const key = metadataCacheKey(displayName); |
| 235 | return writeCacheValue(key, refreshedRawMetadata[displayName], { |
| 236 | requestEpoch: request.epoch, |
| 237 | requestVersion: request.versionFor(key), |
| 238 | }); |
| 239 | }); |
| 240 | if (priority === BACKGROUND_PRIORITY) { |
| 241 | await Promise.all(cacheWrites); |
| 242 | } else { |
| 243 | cacheWrites.forEach((cacheWrite) => void cacheWrite.catch(() => { |
| 244 | })); |
| 245 | } |
| 246 | const refreshedMetadata = Object.fromEntries( |
| 247 | Object.entries(refreshedRawMetadata).map(([displayName, metadata]) => ( |
| 248 | [displayName, metadataWithLatestYear(metadata)] |
| 249 | )) |
| 250 | ); |
| 251 | return { |
| 252 | ...cachedMetadata, |
| 253 | ...refreshedMetadata, |
| 254 | }; |
| 255 | } finally { |
| 256 | request.release(); |
| 257 | } |
| 258 | } |
no test coverage detected