(programIds, isRefresh = false, {priority = FOREGROUND_PRIORITY} = {})
| 140 | export async function getProgramDescs(programIds, isRefresh = false, {priority = FOREGROUND_PRIORITY} = {}) { |
| 141 | const uniqueProgramIds = [...new Set(programIds)].filter(Boolean); |
| 142 | const requestEpoch = getCacheEpoch(); |
| 143 | const entries = await Promise.all(uniqueProgramIds.map((programId) => ( |
| 144 | readCacheEntry(programDescCacheKey(programId), {legacyFields: ["description"], requestEpoch}) |
| 145 | ))); |
| 146 | if (requestEpoch !== getCacheEpoch()) { |
| 147 | return {}; |
| 148 | } |
| 149 | |
| 150 | const cachedDescriptions = Object.fromEntries(uniqueProgramIds.flatMap((programId, index) => ( |
| 151 | entries[index] ? [[programId, entries[index].value]] : [] |
| 152 | ))); |
| 153 | const expiredProgramIds = uniqueProgramIds.filter((programId, index) => ( |
| 154 | isCacheEntryExpired(entries[index], isRefresh) |
| 155 | )); |
| 156 | const request = beginCacheRequests(expiredProgramIds.map(programDescCacheKey), priority, requestEpoch); |
| 157 | const requestProgramIds = request.keys.map(programIdFromDescCacheKey); |
| 158 | |
| 159 | if (requestProgramIds.length === 0) { |
| 160 | return cachedDescriptions; |
| 161 | } |
| 162 | |
| 163 | try { |
| 164 | const response = await apiJson(PROGRAM_DESC_BATCH, { |
| 165 | body: {ProgramIDs: requestProgramIds}, |
| 166 | fetchPriority: priority, |
| 167 | }); |
| 168 | const refreshedDescriptions = requestProgramIds.reduce((descriptions, programId) => ({ |
| 169 | ...descriptions, |
| 170 | [programId]: response.data?.[programId] ?? null, |
| 171 | }), {}); |
| 172 | const cacheWrites = requestProgramIds.map((programId) => { |
| 173 | const key = programDescCacheKey(programId); |
| 174 | return writeCacheValue(key, refreshedDescriptions[programId], { |
| 175 | requestEpoch: request.epoch, |
| 176 | requestVersion: request.versionFor(key), |
| 177 | }); |
| 178 | }); |
| 179 | if (priority === BACKGROUND_PRIORITY) { |
| 180 | await Promise.all(cacheWrites); |
| 181 | } else { |
| 182 | cacheWrites.forEach((cacheWrite) => void cacheWrite.catch(() => { |
| 183 | })); |
| 184 | } |
| 185 | return { |
| 186 | ...cachedDescriptions, |
| 187 | ...refreshedDescriptions, |
| 188 | }; |
| 189 | } finally { |
| 190 | request.release(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | export async function getProgramContent(programId, isRefresh = false) { |
| 195 | /* |
| 196 | * Get the program (with description) from the server or local storage by programId |
| 197 | * @param programId [String]: programId |
no test coverage detected