Skip to content

Commit 5dfa388

Browse files
committed
Speeding up loading lua.
1 parent dc8dbc5 commit 5dfa388

2 files changed

Lines changed: 68 additions & 14 deletions

File tree

wiktionary_pron/scripts/lua_init.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { fetchWithCache } from "./utils.js";
1+
import { fetchWithCache, fetchWithCacheMultiple } from "./utils.js";
22

33
const factory = await lb.factory;
44
const lua = await factory.createEngine();
55

66
// Set a JS function to be a global lua function
77

88
lua.global.set("fetch", (url) => fetchWithCache(url));
9+
lua.global.set("fetchMultiple", (url) => fetchWithCacheMultiple(url));
910

1011
async function mountFile(file_path, lua_path) {
1112
const content = await fetch(file_path).then((data) => data.text());
@@ -46,14 +47,9 @@ await lua.doString(`
4647
4748
updateLoadingText(path, extension)
4849
49-
local resp = fetch(string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension)):await()
50-
if resp.status == 404 then
51-
resp = fetch(string.format('https://cdn.statically.io/gh/hellpanderrr/hellpanderrr.github.io/master/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
52-
end
50+
resp = fetchMultiple({string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension),string.format('https://cdn.statically.io/gh/hellpanderrr/hellpanderrr.github.io/0.1.0/wiktionary_pron/lua_modules/%s.%s',path,extension), string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/hellpanderrr.github.io@0.1.0/wiktionary_pron/lua_modules/%s.%s',path,extension)}):await()
5351
54-
if resp.status == 404 then
55-
resp = fetch(string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/hellpanderrr.github.io@0.1.0/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
56-
end
52+
5753
5854
updateLoadingText("", "")
5955

wiktionary_pron/scripts/utils.js

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ function get_ipa_no_cache(text, args) {
164164
case "Czech":
165165
if (langForm === "Phonemic") {
166166
let dictRecord = globalThis.lexicon.get(
167-
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, ""),
167+
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, ""),
168168
);
169169
if (!dictRecord) {
170170
dictRecord = globalThis.lexicon.get(
171-
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, "").toLowerCase(),
171+
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, "").toLowerCase(),
172172
);
173173
}
174174
console.log(cleanText, dictRecord);
@@ -264,6 +264,63 @@ async function loadFileFromZipOrPath(zipPathOrBlob, filename) {
264264
});
265265
}
266266

267+
async function sendParallelRequests(urls) {
268+
const promises = urls.map((url) => fetch(url));
269+
270+
try {
271+
const results = await Promise.any(promises);
272+
return results;
273+
} catch (error) {
274+
return null; // Handle errors if no promises resolve successfully
275+
}
276+
}
277+
278+
async function fetchWithCacheMultiple(urls) {
279+
const url = urls[0].split("/").slice(-2).join("/");
280+
281+
console.log("reading cache", url);
282+
const cachedResponse = await localforage.getItem(url);
283+
if (cachedResponse) {
284+
console.log("reading from cache", url);
285+
if (cachedResponse instanceof Blob) {
286+
const response = new Response(cachedResponse);
287+
response.headers.set("X-From-Cache", "true");
288+
console.log("Returned cached blob ", url);
289+
return response;
290+
}
291+
const response = new Response(JSON.parse(cachedResponse));
292+
response.headers.set("X-From-Cache", "true");
293+
console.log("Returned cached string ", url);
294+
return response;
295+
}
296+
console.log("caching ", url);
297+
298+
// const response = await fetch(url);
299+
const response = await sendParallelRequests(urls);
300+
301+
const contentType = response.headers.get("content-type");
302+
let responseContent;
303+
let responseWithHeaders;
304+
305+
if (contentType == "application/zip") {
306+
responseContent = await response.blob();
307+
try {
308+
await localforage.setItem(url, responseContent);
309+
} catch (err) {
310+
console.log(err);
311+
}
312+
} else {
313+
responseContent = await response.text();
314+
try {
315+
await localforage.setItem(url, JSON.stringify(responseContent));
316+
} catch (err) {
317+
console.log(err);
318+
}
319+
}
320+
responseWithHeaders = new Response(responseContent, response);
321+
return responseWithHeaders;
322+
}
323+
267324
async function fetchWithCache(url) {
268325
console.log("reading cache", url);
269326
const cachedResponse = await localforage.getItem(url);
@@ -358,10 +415,10 @@ function enableAll(include_elements = []) {
358415
forms.forEach((form) => {
359416
// Enable all elements in the form
360417
Array.from(form.elements)
361-
.concat(include_elements)
362-
.forEach((element) => {
363-
element.disabled = false;
364-
});
418+
.concat(include_elements)
419+
.forEach((element) => {
420+
element.disabled = false;
421+
});
365422
});
366423
}
367424

@@ -376,6 +433,7 @@ export {
376433
loadFileFromZipOrPath,
377434
createElementFromHTML,
378435
fetchWithCache,
436+
fetchWithCacheMultiple,
379437
disableAll,
380438
enableAll,
381439
};

0 commit comments

Comments
 (0)