Skip to content

Commit 3ed49e5

Browse files
authored
refactor: use the fetch API (#33)
* refactor: swap out xmlhttprequest for fetch Resolves: #32 * perf: drop unnecessary async modifier Should drop a single microtask. Doesn't really make a difference in practice, but it doesn't hurt either. * refactor: use async-await to load the data * style: remove random spaces * refactor: don't use nested functions * fix: use strict mode, not sloppy mode This could theoretically change behavior for `{{flutter_js}}`, but I think it's fine.
1 parent f9b9125 commit 3ed49e5

File tree

1 file changed

+31
-34
lines changed

1 file changed

+31
-34
lines changed

__brick__/web/flutter_bootstrap.js

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"use strict";
12
{{=<% %>=}}{{flutter_js}}<%={{ }}=%>
23
{{=<% %>=}}{{flutter_build_config}}<%={{ }}=%>
34

@@ -31,48 +32,44 @@ async function beginPreloading() {
3132
progressIndicator.style.width = '0%';
3233
progressText.textContent = `Loaded ${loadedAssets} of ${totalAssets} assets`;
3334

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);
4138
}
39+
}
4240

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++;
5443

55-
req.onerror = function() {
56-
reject(new Error('Network error'));
57-
};
44+
const value = Math.floor((loadedAssets / totalAssets) * 100) + '%';
45+
progressIndicator.style.width = value;
5846

59-
req.open('GET', url);
60-
req.send();
61-
});
62-
}
47+
progressText.textContent = `Loaded ${loadedAssets} of ${totalAssets} assets`;
48+
}
6349

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+
);
7057
}
58+
return await response.text();
59+
} catch (error) {
60+
throw new Error("Network error");
7161
}
62+
}
7263

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);
7673
}
7774
}
7875

0 commit comments

Comments
 (0)