Skip to content

Commit

Permalink
reduce delays
Browse files Browse the repository at this point in the history
  • Loading branch information
jemikanegara committed Nov 29, 2023
1 parent 7670268 commit bca7672
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
3 changes: 1 addition & 2 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ if (isBrowser) {
const apiKey = window.weployScriptTag.getAttribute("data-weploy-key");
const disableAutoTranslateAttr = window.weployScriptTag.getAttribute("data-disable-auto-translate")
const disableAutoTranslate = disableAutoTranslateAttr == "true";
getTranslations(apiKey, {disableAutoTranslate});
createLanguageSelect(apiKey);
getTranslations(apiKey, {disableAutoTranslate, createSelector: true})
});
}
31 changes: 23 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if (isBrowser) {
window.weployTimer = null;
}

const debounce = (mainFunction, delay = 3000) => {
const debounce = (mainFunction, delay = 2000) => {
if (!isBrowser) return mainFunction(...args);

// Return an anonymous function that takes in any number of arguments
Expand Down Expand Up @@ -258,9 +258,14 @@ function modifyHtmlStrings(rootElement, language, apiKey) {
});
}

async function startTranslationCycle(node, apiKey) {
async function startTranslationCycle(node, apiKey, delay) {
const lang = await getLanguageFromLocalStorage();
debounce(async () => modifyHtmlStrings(node, lang, apiKey))();

if (!delay) {
modifyHtmlStrings(node, lang, apiKey)
} else {
debounce(async () => modifyHtmlStrings(node, lang, apiKey), delay)();
}
// window.cacheAlreadyChecked = true;
}

Expand All @@ -272,24 +277,34 @@ async function getTranslations(apiKey, optsArgs = {}) {
try {
if (!isBrowser) {
weployOptions = {
timeout: optsArgs.timeout == null ? 1000 : optsArgs.timeout,
timeout: optsArgs.timeout == null ? 0 : optsArgs.timeout,
pathOptions: optsArgs.pathOptions || {},
apiKey
}
} else {
window.weployOptions = {
timeout: optsArgs.timeout == null ? 1000 : optsArgs.timeout,
timeout: optsArgs.timeout == null ? 0 : optsArgs.timeout,
pathOptions: optsArgs.pathOptions || {},
apiKey
}
}

// save language to local storage & delay 1 second to wait google translate
await Promise.allSettled([
fetchLanguageList(apiKey),
delay(1000)
]);

if (optsArgs.createSelector) {
await createLanguageSelect(apiKey);
}

// handle google translate
await delay(1000)
if (isBrowser && (document.querySelector('html.translated-ltr') || document.querySelector('html.translated-rtl'))) return;

await new Promise((resolve, reject) => {
startTranslationCycle(document.body, apiKey, null).catch(reject);
const timeout = getWeployOptions().timeout;
startTranslationCycle(document.body, apiKey, timeout).catch(reject);

if (isBrowser && !isDomListenerAdded) {
// Select the target node
Expand All @@ -307,7 +322,7 @@ async function getTranslations(apiKey, optsArgs = {}) {
}
}

startTranslationCycle(document.body, apiKey, null).catch(reject)
startTranslationCycle(document.body, apiKey, 2000).catch(reject)

// function getTextNodes(rootElement) {
// if (hasExcludedParent(rootElement)) {
Expand Down

0 comments on commit bca7672

Please sign in to comment.