({
images,
onProgress,
cache,
loadingPromises,
queueLoad,
setBulkMode,
maxConcurrentLoads,
}: PrefetchAsyncImagesOptions<T>)
| 20 | } |
| 21 | |
| 22 | export async function prefetchAsyncImages<T>({ |
| 23 | images, |
| 24 | onProgress, |
| 25 | cache, |
| 26 | loadingPromises, |
| 27 | queueLoad, |
| 28 | setBulkMode, |
| 29 | maxConcurrentLoads, |
| 30 | }: PrefetchAsyncImagesOptions<T>): Promise<void> { |
| 31 | if (images.length === 0) { |
| 32 | onProgress?.(1); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | setBulkMode(true); |
| 37 | |
| 38 | let completed = 0; |
| 39 | const total = images.length; |
| 40 | let lastReportedProgress = 0; |
| 41 | |
| 42 | const reportProgress = () => { |
| 43 | const progress = getPrefetchProgress(completed, total); |
| 44 | if (shouldReportPrefetchProgress(completed, total, lastReportedProgress)) { |
| 45 | lastReportedProgress = progress; |
| 46 | onProgress?.(progress); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | try { |
| 51 | const chunkSize = getPrefetchChunkSize(maxConcurrentLoads); |
| 52 | for (let i = 0; i < images.length; i += chunkSize) { |
| 53 | const chunk = images.slice(i, i + chunkSize); |
| 54 | |
| 55 | const chunkPromises = chunk.map(async ({ file, name }) => { |
| 56 | if (cache.has(name)) { |
| 57 | completed++; |
| 58 | reportProgress(); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | let promise = loadingPromises.get(name); |
| 63 | if (!promise) { |
| 64 | promise = queueLoad(file, name); |
| 65 | loadingPromises.set(name, promise); |
| 66 | } |
| 67 | |
| 68 | await promise; |
| 69 | completed++; |
| 70 | reportProgress(); |
| 71 | }); |
| 72 | |
| 73 | await Promise.all(chunkPromises); |
| 74 | } |
| 75 | } finally { |
| 76 | setBulkMode(false); |
| 77 | } |
| 78 | } |
no test coverage detected