Skip to content

Commit

Permalink
Speeding up loading lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellpanderrr committed Jun 10, 2024
1 parent dc8dbc5 commit 5dfa388
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
12 changes: 4 additions & 8 deletions wiktionary_pron/scripts/lua_init.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { fetchWithCache } from "./utils.js";
import { fetchWithCache, fetchWithCacheMultiple } from "./utils.js";

const factory = await lb.factory;
const lua = await factory.createEngine();

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

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

async function mountFile(file_path, lua_path) {
const content = await fetch(file_path).then((data) => data.text());
Expand Down Expand Up @@ -46,14 +47,9 @@ await lua.doString(`
updateLoadingText(path, extension)
local resp = fetch(string.format('../wiktionary_pron/lua_modules/%s.%s',path,extension)):await()
if resp.status == 404 then
resp = fetch(string.format('https://cdn.statically.io/gh/hellpanderrr/hellpanderrr.github.io/master/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
end
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/[email protected]/wiktionary_pron/lua_modules/%s.%s',path,extension)}):await()
if resp.status == 404 then
resp = fetch(string.format('https://cdn.jsdelivr.net/gh/hellpanderrr/[email protected]/wiktionary_pron/lua_modules/%s.%s',path,extension) ):await()
end
updateLoadingText("", "")
Expand Down
70 changes: 64 additions & 6 deletions wiktionary_pron/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ function get_ipa_no_cache(text, args) {
case "Czech":
if (langForm === "Phonemic") {
let dictRecord = globalThis.lexicon.get(
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, ""),
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, ""),
);
if (!dictRecord) {
dictRecord = globalThis.lexicon.get(
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, "").toLowerCase(),
cleanText.replace(/[^\p{Letter}\p{Mark}-]+/gu, "").toLowerCase(),
);
}
console.log(cleanText, dictRecord);
Expand Down Expand Up @@ -264,6 +264,63 @@ async function loadFileFromZipOrPath(zipPathOrBlob, filename) {
});
}

async function sendParallelRequests(urls) {
const promises = urls.map((url) => fetch(url));

try {
const results = await Promise.any(promises);
return results;
} catch (error) {
return null; // Handle errors if no promises resolve successfully
}
}

async function fetchWithCacheMultiple(urls) {
const url = urls[0].split("/").slice(-2).join("/");

console.log("reading cache", url);
const cachedResponse = await localforage.getItem(url);
if (cachedResponse) {
console.log("reading from cache", url);
if (cachedResponse instanceof Blob) {
const response = new Response(cachedResponse);
response.headers.set("X-From-Cache", "true");
console.log("Returned cached blob ", url);
return response;
}
const response = new Response(JSON.parse(cachedResponse));
response.headers.set("X-From-Cache", "true");
console.log("Returned cached string ", url);
return response;
}
console.log("caching ", url);

// const response = await fetch(url);
const response = await sendParallelRequests(urls);

const contentType = response.headers.get("content-type");
let responseContent;
let responseWithHeaders;

if (contentType == "application/zip") {
responseContent = await response.blob();
try {
await localforage.setItem(url, responseContent);
} catch (err) {
console.log(err);
}
} else {
responseContent = await response.text();
try {
await localforage.setItem(url, JSON.stringify(responseContent));
} catch (err) {
console.log(err);
}
}
responseWithHeaders = new Response(responseContent, response);
return responseWithHeaders;
}

async function fetchWithCache(url) {
console.log("reading cache", url);
const cachedResponse = await localforage.getItem(url);
Expand Down Expand Up @@ -358,10 +415,10 @@ function enableAll(include_elements = []) {
forms.forEach((form) => {
// Enable all elements in the form
Array.from(form.elements)
.concat(include_elements)
.forEach((element) => {
element.disabled = false;
});
.concat(include_elements)
.forEach((element) => {
element.disabled = false;
});
});
}

Expand All @@ -376,6 +433,7 @@ export {
loadFileFromZipOrPath,
createElementFromHTML,
fetchWithCache,
fetchWithCacheMultiple,
disableAll,
enableAll,
};

0 comments on commit 5dfa388

Please sign in to comment.