fix: Résolution des blocages après l'enregistrement du Service Worker
- Ajout de timeouts sur checkForUpdates() (10s) et waitForServiceWorkerActivation() (15s) - Gestion d'erreur améliorée pour éviter les blocages infinis - checkForUpdates() avec timeout de 5s pour éviter les blocages - waitForServiceWorkerActivation() retourne null au lieu de bloquer - Gestion d'erreur dans l'intervalle de scan du service worker - Continuation de l'initialisation même en cas d'échec partiel - Logs d'avertissement pour diagnostiquer les problèmes
This commit is contained in:
parent
17517f861a
commit
937b071100
@ -140,7 +140,16 @@ export class Database {
|
||||
this.showServiceWorkerSpinner('Initializing database service...');
|
||||
}
|
||||
|
||||
await this.checkForUpdates();
|
||||
// Check for updates with timeout
|
||||
try {
|
||||
await Promise.race([
|
||||
this.checkForUpdates(),
|
||||
new Promise((_, reject) => setTimeout(() => reject(new Error('Update timeout')), 10000))
|
||||
]);
|
||||
} catch (error) {
|
||||
console.warn('Service worker update failed or timed out:', error);
|
||||
// Continue anyway - don't block the initialization
|
||||
}
|
||||
|
||||
// Hide spinner once service worker is ready
|
||||
this.hideServiceWorkerSpinner();
|
||||
@ -153,11 +162,18 @@ export class Database {
|
||||
|
||||
// Set up a periodic check to ensure the service worker is active and to send a SCAN message.
|
||||
this.serviceWorkerCheckIntervalId = window.setInterval(async () => {
|
||||
const activeWorker = this.serviceWorkerRegistration?.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration!));
|
||||
const service = await Services.getInstance();
|
||||
const payload = await service.getMyProcesses();
|
||||
if (payload && payload.length != 0) {
|
||||
activeWorker?.postMessage({ type: 'SCAN', payload });
|
||||
try {
|
||||
const activeWorker = this.serviceWorkerRegistration?.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration!));
|
||||
if (activeWorker) {
|
||||
const service = await Services.getInstance();
|
||||
const payload = await service.getMyProcesses();
|
||||
if (payload && payload.length != 0) {
|
||||
activeWorker.postMessage({ type: 'SCAN', payload });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('Service worker scan failed:', error);
|
||||
// Continue the interval even if one scan fails
|
||||
}
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
@ -167,18 +183,28 @@ export class Database {
|
||||
|
||||
// Helper function to wait for service worker activation
|
||||
private async waitForServiceWorkerActivation(registration: ServiceWorkerRegistration): Promise<ServiceWorker | null> {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (registration.active) {
|
||||
resolve(registration.active);
|
||||
} else {
|
||||
const listener = () => {
|
||||
if (registration.active) {
|
||||
navigator.serviceWorker.removeEventListener('controllerchange', listener);
|
||||
resolve(registration.active);
|
||||
}
|
||||
};
|
||||
navigator.serviceWorker.addEventListener('controllerchange', listener);
|
||||
return;
|
||||
}
|
||||
|
||||
// Set a timeout to prevent infinite waiting
|
||||
const timeout = setTimeout(() => {
|
||||
navigator.serviceWorker.removeEventListener('controllerchange', listener);
|
||||
console.warn('Service worker activation timeout');
|
||||
resolve(null); // Return null instead of rejecting to allow continuation
|
||||
}, 15000); // 15 second timeout
|
||||
|
||||
const listener = () => {
|
||||
if (registration.active) {
|
||||
clearTimeout(timeout);
|
||||
navigator.serviceWorker.removeEventListener('controllerchange', listener);
|
||||
resolve(registration.active);
|
||||
}
|
||||
};
|
||||
|
||||
navigator.serviceWorker.addEventListener('controllerchange', listener);
|
||||
});
|
||||
}
|
||||
|
||||
@ -186,7 +212,10 @@ export class Database {
|
||||
if (this.serviceWorkerRegistration) {
|
||||
// Check for updates to the service worker
|
||||
try {
|
||||
await this.serviceWorkerRegistration.update();
|
||||
await Promise.race([
|
||||
this.serviceWorkerRegistration.update(),
|
||||
new Promise((_, reject) => setTimeout(() => reject(new Error('Update timeout')), 5000))
|
||||
]);
|
||||
|
||||
// If there's a new worker waiting, activate it immediately
|
||||
if (this.serviceWorkerRegistration.waiting) {
|
||||
@ -194,6 +223,7 @@ export class Database {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking for service worker updates:', error);
|
||||
throw error; // Re-throw to be caught by the calling function
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user