Kaynağa Gözat

use regular timeout if errors are detected (see #643)

Gildas 4 yıl önce
ebeveyn
işleme
d275b60a8b

+ 29 - 13
lib/single-file/processors/lazy/content/content-lazy-loader.js

@@ -178,27 +178,43 @@ async function setAsyncTimeout(type, callback, delay) {
 		if (!timeouts.get(type) || !timeouts.get(type).pending) {
 			const timeoutData = { callback, pending: true };
 			timeouts.set(type, timeoutData);
-			await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.setTimeout", type, delay });
+			try {
+				await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.setTimeout", type, delay });
+			} catch (error) {
+				setRegularTimeout(type, callback, delay);
+			}
 			timeoutData.pending = false;
 		}
 	} else {
-		const timeoutId = timeouts.get(type);
-		if (timeoutId) {
-			globalThis.clearTimeout(timeoutId);
-		}
-		timeouts.set(type, callback);
-		globalThis.setTimeout(callback, delay);
+		setRegularTimeout(type, callback, delay)
+	}
+}
+
+function setRegularTimeout(type, callback, delay) {
+	const timeoutId = timeouts.get(type);
+	if (timeoutId) {
+		globalThis.clearTimeout(timeoutId);
 	}
+	timeouts.set(type, callback);
+	globalThis.setTimeout(callback, delay);
 }
 
 async function clearAsyncTimeout(type) {
 	if (browser && browser.runtime && browser.runtime.sendMessage) {
-		await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.clearTimeout", type });
-	} else {
-		const previousTimeoutId = timeouts.get(type);
-		timeouts.delete(type);
-		if (previousTimeoutId) {
-			globalThis.clearTimeout(previousTimeoutId);
+		try {
+			await browser.runtime.sendMessage({ method: "singlefile.lazyTimeout.clearTimeout", type });
+		} catch (error) {
+			clearRegularTimeout(type);
 		}
+	} else {
+		clearRegularTimeout(type);
+	}
+}
+
+function clearRegularTimeout(type) {
+	const previousTimeoutId = timeouts.get(type);
+	timeouts.delete(type);
+	if (previousTimeoutId) {
+		globalThis.clearTimeout(previousTimeoutId);
 	}
 }