|
1 | | -import fetch from 'cross-fetch'; |
| 1 | +import { fetch } from 'bun'; |
| 2 | + |
| 3 | +import { LibraryDataEntryType } from '~/types'; |
2 | 4 |
|
3 | 5 | import libraries from '../react-native-libraries.json'; |
4 | 6 | import { sleep } from './helpers'; |
5 | 7 |
|
6 | | -console.log('⬇️ Attempting to fetch examples and images'); |
| 8 | +async function runThrottledFetches(libraries: LibraryDataEntryType[], delayMs = 50) { |
| 9 | + const urlList: string[] = []; |
7 | 10 |
|
8 | | -libraries.forEach(lib => { |
9 | | - if (lib.examples) { |
10 | | - lib.examples.forEach(async (example: string, index: number) => { |
11 | | - await sleep(500); |
12 | | - setTimeout(() => { |
13 | | - fetch(example) |
14 | | - .then(response => { |
15 | | - if (response.status !== 200) { |
16 | | - console.warn(`EXAMPLE: ${example} returned ${response.status}`); |
17 | | - } |
18 | | - }) |
19 | | - .catch(error => { |
20 | | - console.warn(`EXAMPLE: errored! ${error}`); |
21 | | - }); |
22 | | - }, 150 * index); |
23 | | - }); |
| 11 | + for (const lib of libraries) { |
| 12 | + if (lib.examples) { |
| 13 | + for (const exampleUrl of lib.examples) { |
| 14 | + urlList.push(exampleUrl); |
| 15 | + } |
| 16 | + } |
| 17 | + if (lib.images) { |
| 18 | + for (const imgUrl of lib.images) { |
| 19 | + urlList.push(imgUrl); |
| 20 | + } |
| 21 | + } |
24 | 22 | } |
25 | 23 |
|
26 | | - if (lib.images) { |
27 | | - lib.images.forEach(async (img: string, index: number) => { |
28 | | - await sleep(500); |
29 | | - setTimeout(() => { |
30 | | - fetch(img) |
31 | | - .then(response => { |
32 | | - if (response.status !== 200) { |
33 | | - console.warn(`IMG: ${img} returned ${response.status}`); |
34 | | - } |
35 | | - }) |
36 | | - .catch(error => { |
37 | | - console.warn(`IMG: errored! ${error}`); |
38 | | - }); |
39 | | - }, 150 * index); |
40 | | - }); |
| 24 | + console.log(`⬇️ Attempting to fetch examples and images (${urlList.length} URLs)`); |
| 25 | + |
| 26 | + for (const url of urlList) { |
| 27 | + try { |
| 28 | + const response = await fetch(url); |
| 29 | + if (response.status !== 200) { |
| 30 | + console.warn(`${url} returned ${response.status}`); |
| 31 | + } |
| 32 | + } catch (err: any) { |
| 33 | + console.warn(`${url} errored!`, err); |
| 34 | + } |
| 35 | + await sleep(delayMs); |
41 | 36 | } |
| 37 | +} |
| 38 | + |
| 39 | +runThrottledFetches(libraries).catch(err => { |
| 40 | + console.error('❌ Unexpected error in throttled fetcher:', err); |
42 | 41 | }); |
0 commit comments