fix: enhance fetch fallback mechanism with retry logic and exponential back-off

This commit is contained in:
fynks 2026-04-19 14:30:42 +05:00
parent 69bd604279
commit 499100810a

53
dist/js/app.js vendored
View file

@ -462,12 +462,17 @@ const Utils = (() => {
const errors = []; const errors = [];
for (const url of urls) { for (const url of urls) {
try { for (let attempt = 0; attempt <= CONFIG.API.RETRY_ATTEMPTS; attempt++) {
const response = await fetchWithTimeout(url, options); try {
return await response.json(); const response = await fetchWithTimeout(url, options);
} catch (error) { return await response.json();
errors.push({ url, error: error.message }); } catch (error) {
continue; errors.push({ url, attempt, error: error.message });
if (attempt < CONFIG.API.RETRY_ATTEMPTS) {
// Exponential back-off: 200ms, 400ms, …
await new Promise(r => setTimeout(r, 200 * 2 ** attempt));
}
}
} }
} }
@ -1013,9 +1018,21 @@ class StateManager {
} }
update(updates) { update(updates) {
const changed = [];
Object.entries(updates).forEach(([key, value]) => { Object.entries(updates).forEach(([key, value]) => {
this.set(key, value); const oldValue = this.#state[key];
this.#state[key] = value;
if (this.#listeners.has(key)) {
changed.push({ key, value, oldValue });
}
}); });
// Notify after all values are in place
changed.forEach(({ key, value, oldValue }) => {
this.#listeners.get(key)?.forEach(cb => cb(value, oldValue));
});
return this; return this;
} }
@ -1853,24 +1870,6 @@ class ComparisonManager {
this.#lifecycle.destroy(); this.#lifecycle.destroy();
} }
#handleCompare() {
const service1 = this.#elements.select1.value;
const service2 = this.#elements.select2.value;
console.log('Comparing services:', { service1, service2 });
if (!service1 || !service2) {
this.#showEmptyState();
return;
}
if (service1 === service2) {
this.#showSameProviderWarning();
return;
}
this.#renderComparison(service1, service2);
}
#renderComparison(service1, service2) { #renderComparison(service1, service2) {
const hosts = Object.keys(this.#data); const hosts = Object.keys(this.#data);
@ -2294,8 +2293,8 @@ class App {
// Fetch data // Fetch data
const dataPromises = [ const dataPromises = [
Utils.fetchWithFallback(CONFIG.API.FILE_HOSTS), DataService.fetchHosts('file-hosts'),
Utils.fetchWithFallback(CONFIG.API.ADULT_HOSTS) DataService.fetchHosts('adult-hosts')
]; ];
const results = await Promise.allSettled(dataPromises); const results = await Promise.allSettled(dataPromises);