(displayNames, isRefresh = false, {priority = FOREGROUND_PRIORITY} = {})
| 199 | ))); |
| 200 | const expiredDisplayNames = uniqueDisplayNames.filter((displayName, index) => ( |
| 201 | isCacheEntryExpired(entries[index], isRefresh) |
| 202 | )); |
| 203 | const request = beginCacheRequests(expiredDisplayNames.map(metadataCacheKey), priority, requestEpoch); |
| 204 | const requestDisplayNames = request.keys.map(displayNameFromMetadataCacheKey); |
| 205 | |
| 206 | if (requestDisplayNames.length === 0) { |
| 207 | return cachedMetadata; |
| 208 | } |
| 209 | |
| 210 | try { |
| 211 | const response = await apiJson(GET_METADATA_BATCH, { |
| 212 | body: {display_names: requestDisplayNames}, |
| 213 | fetchPriority: priority, |
| 214 | }); |
| 215 | const refreshedRawMetadata = requestDisplayNames.reduce((metadataByName, displayName) => ({ |
| 216 | ...metadataByName, |
| 217 | [displayName]: response.data?.[displayName] ?? null, |
| 218 | }), {}); |
| 219 | const cacheWrites = requestDisplayNames.map((displayName) => { |
| 220 | const key = metadataCacheKey(displayName); |
| 221 | return writeCacheValue(key, refreshedRawMetadata[displayName], { |
| 222 | requestEpoch: request.epoch, |
| 223 | requestVersion: request.versionFor(key), |
| 224 | }); |
| 225 | }); |
| 226 | if (priority === BACKGROUND_PRIORITY) { |
| 227 | await Promise.all(cacheWrites); |
| 228 | } else { |
| 229 | cacheWrites.forEach((cacheWrite) => void cacheWrite.catch(() => { |
| 230 | })); |
| 231 | } |
| 232 | const refreshedMetadata = Object.fromEntries( |
| 233 | Object.entries(refreshedRawMetadata).map(([displayName, metadata]) => ( |
| 234 | [displayName, metadataWithLatestYear(metadata)] |
| 235 | )) |
| 236 | ); |
| 237 | return { |
| 238 | ...cachedMetadata, |
| 239 | ...refreshedMetadata, |
| 240 | }; |
| 241 | } finally { |
| 242 | request.release(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Find the latest year with data for a given applicant |
| 248 | * @param {string[]} applicantIDs - Applicant IDs in the form "displayName@year" |
| 249 | * @returns {Promise<number|null>} - Latest year or null if no data found |
| 250 | */ |
| 251 | function findLatestYearForApplicant(applicantIDs = []) { |
| 252 | const validYears = applicantIDs |
| 253 | .map(id => Number.parseInt(id.split('@')[1], 10)) |
| 254 | .filter(Number.isFinite); |
| 255 | return validYears.length > 0 ? Math.max(...validYears) : null; |
| 256 | } |
| 257 | |
| 258 | function metadataWithLatestYear(metadata) { |
no test coverage detected