Skip to content

Commit 8571f15

Browse files
authored
Update fluent-web to preserve resource insertion order (#163)
1 parent 2308683 commit 8571f15

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

fluent-web/src/index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,24 @@ function getResourceLinks(elem) {
3434
);
3535
}
3636

37-
async function generateContext(locale, resourceIds) {
37+
async function fetchResource(locale, id) {
38+
const url = id.replace("{locale}", locale);
39+
const response = await fetch(url);
40+
return response.text();
41+
}
42+
43+
async function createContext(locale, resourceIds) {
3844
const ctx = new MessageContext([locale]);
39-
await Promise.all(resourceIds.map(resourceId => {
40-
const url = resourceId.replace("{locale}", locale);
41-
return fetch(url)
42-
.then(d => d.text())
43-
.then(source => ctx.addMessages(source));
44-
}));
45+
46+
// First fetch all resources
47+
const resources = await Promise.all(
48+
resourceIds.map(id => fetchResource(locale, id))
49+
);
50+
51+
// Then apply them preserving order
52+
for (const resource of resources) {
53+
ctx.addMessages(resource);
54+
}
4555
return ctx;
4656
}
4757

@@ -56,7 +66,7 @@ function* generateMessages(resourceIds) {
5666
}
5767
);
5868
for (const locale of locales) {
59-
yield generateContext(locale, resourceIds);
69+
yield createContext(locale, resourceIds);
6070
}
6171
}
6272

0 commit comments

Comments
 (0)