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