|
| 1 | +"use strict"; |
1 | 2 | {{=<% %>=}}{{flutter_js}}<%={{ }}=%>
|
2 | 3 | {{=<% %>=}}{{flutter_build_config}}<%={{ }}=%>
|
3 | 4 |
|
@@ -31,48 +32,44 @@ async function beginPreloading() {
|
31 | 32 | progressIndicator.style.width = '0%';
|
32 | 33 | progressText.textContent = `Loaded ${loadedAssets} of ${totalAssets} assets`;
|
33 | 34 |
|
34 |
| - async function reportProgress() { |
35 |
| - loadedAssets++; |
36 |
| - |
37 |
| - const value = Math.floor((loadedAssets / totalAssets) * 100) + '%'; |
38 |
| - progressIndicator.style.width = value; |
39 |
| - |
40 |
| - progressText.textContent = `Loaded ${loadedAssets} of ${totalAssets} assets`; |
| 35 | + for (let i = 0; i < assets.length; i += batchSize) { |
| 36 | + const batch = assets.slice(i, i + batchSize); |
| 37 | + await loadBatch(batch); |
41 | 38 | }
|
| 39 | +} |
42 | 40 |
|
43 |
| - async function load(url) { |
44 |
| - return new Promise((resolve, reject) => { |
45 |
| - const req = new XMLHttpRequest(); |
46 |
| - |
47 |
| - req.onload = function() { |
48 |
| - if (req.status >= 200 && req.status < 300) { |
49 |
| - resolve(req.response); |
50 |
| - } else { |
51 |
| - reject(new Error(`Failed to load: ${req.status} ${req.statusText}`)); |
52 |
| - } |
53 |
| - }; |
| 41 | +function reportProgress() { |
| 42 | + loadedAssets++; |
54 | 43 |
|
55 |
| - req.onerror = function() { |
56 |
| - reject(new Error('Network error')); |
57 |
| - }; |
| 44 | + const value = Math.floor((loadedAssets / totalAssets) * 100) + '%'; |
| 45 | + progressIndicator.style.width = value; |
58 | 46 |
|
59 |
| - req.open('GET', url); |
60 |
| - req.send(); |
61 |
| - }); |
62 |
| - } |
| 47 | + progressText.textContent = `Loaded ${loadedAssets} of ${totalAssets} assets`; |
| 48 | +} |
63 | 49 |
|
64 |
| - async function loadBatch(urls) { |
65 |
| - const loadPromises = urls.map(url => load(url).then(reportProgress())); |
66 |
| - try { |
67 |
| - return await Promise.all(loadPromises); |
68 |
| - } catch (error) { |
69 |
| - console.error('Error loading one or more asset:', error); |
| 50 | +async function load(url) { |
| 51 | + try { |
| 52 | + const response = await fetch(url); |
| 53 | + if (!response.ok) { |
| 54 | + throw new Error( |
| 55 | + `Failed to load: ${response.status} ${response.statusText}`, |
| 56 | + ); |
70 | 57 | }
|
| 58 | + return await response.text(); |
| 59 | + } catch (error) { |
| 60 | + throw new Error("Network error"); |
71 | 61 | }
|
| 62 | +} |
72 | 63 |
|
73 |
| - for (let i = 0; i < assets.length; i += batchSize) { |
74 |
| - const batch = assets.slice(i, i + batchSize); |
75 |
| - await loadBatch(batch); |
| 64 | +async function loadBatch(urls) { |
| 65 | + const loadPromises = urls.map(async (url) => { |
| 66 | + await load(url); |
| 67 | + reportProgress(); |
| 68 | + }); |
| 69 | + try { |
| 70 | + return await Promise.all(loadPromises); |
| 71 | + } catch (error) { |
| 72 | + console.error('Error loading one or more asset:', error); |
76 | 73 | }
|
77 | 74 | }
|
78 | 75 |
|
|
0 commit comments