Skip to content

Commit

Permalink
prevent infinite "idleTimeout" messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas-lormeau committed Sep 17, 2021
1 parent 7cb7c60 commit 7a4cb1c
Showing 1 changed file with 5 additions and 1 deletion.
Expand Up @@ -33,6 +33,7 @@ const helper = {
SINGLE_FILE_UI_ELEMENT_CLASS
};

const MAX_IDLE_TIMEOUT_CALLS = 10;
const ATTRIBUTES_MUTATION_TYPE = "attributes";

const browser = globalThis.browser;
Expand All @@ -42,6 +43,8 @@ const addEventListener = (type, listener, options) => globalThis.addEventListene
const removeEventListener = (type, listener, options) => globalThis.removeEventListener(type, listener, options);
const timeouts = new Map();

let idleTimeoutCalls;

if (browser && browser.runtime && browser.runtime.onMessage && browser.runtime.onMessage.addListener) {
browser.runtime.onMessage.addListener(message => {
if (message.method == "singlefile.lazyTimeout.onTimeout") {
Expand Down Expand Up @@ -79,6 +82,7 @@ function resetZoomLevel(options) {
}

function triggerLazyLoading(options) {
idleTimeoutCalls = 0;
return new Promise(async resolve => { // eslint-disable-line no-async-promise-executor
let loadingImages;
const pendingImages = new Set();
Expand Down Expand Up @@ -116,7 +120,7 @@ function triggerLazyLoading(options) {
clearAsyncTimeout("loadTimeout");
clearAsyncTimeout("maxTimeout");
lazyLoadEnd(observer, options, cleanupAndResolve);
} else {
} else if (idleTimeoutCalls < MAX_IDLE_TIMEOUT_CALLS) {
clearAsyncTimeout("idleTimeout");
await setIdleTimeout(Math.max(500, delay / 2));
}
Expand Down

0 comments on commit 7a4cb1c

Please sign in to comment.