diff --git a/src/pages/birthday-setup/birthday-setup.ts b/src/pages/birthday-setup/birthday-setup.ts index 0874bc6..9247308 100644 --- a/src/pages/birthday-setup/birthday-setup.ts +++ b/src/pages/birthday-setup/birthday-setup.ts @@ -28,9 +28,20 @@ document.addEventListener('DOMContentLoaded', async () => { } console.log('🔄 Getting existing services instance...'); - // Utiliser l'instance existante des services - const services = await Services.getInstance(); - console.log('✅ Services instance obtained successfully'); + // Utiliser l'instance existante des services avec gestion des erreurs de mémoire + let services; + try { + services = await Services.getInstance(); + console.log('✅ Services instance obtained successfully'); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); + if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) { + console.error('🚫 Memory error detected'); + updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error'); + throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.'); + } + throw error; + } // Vérifier les prérequis en base de données updateStatus('🔍 Vérification des prérequis...', 'loading'); diff --git a/src/pages/block-sync/block-sync.ts b/src/pages/block-sync/block-sync.ts index 5bdae8f..a2631cf 100644 --- a/src/pages/block-sync/block-sync.ts +++ b/src/pages/block-sync/block-sync.ts @@ -1,6 +1,6 @@ document.addEventListener("DOMContentLoaded", async () => { console.log("🔄 Block sync page loaded"); - + const status = document.getElementById("status") as HTMLElement; const progressBar = document.getElementById("progressBar") as HTMLElement; const continueBtn = document.getElementById("continueBtn") as HTMLButtonElement; @@ -95,7 +95,16 @@ document.addEventListener("DOMContentLoaded", async () => { console.log('✅ Services initialized successfully'); break; } catch (error) { - console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, (error as Error).message); + const errorMessage = error instanceof Error ? error.message : String(error); + console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, errorMessage); + + // Si c'est une erreur de mémoire, arrêter immédiatement + if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) { + console.error('🚫 Memory error detected - stopping retry attempts'); + updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error'); + throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.'); + } + attempts++; if (attempts >= maxAttempts) { throw new Error(`Services failed to initialize after ${maxAttempts} attempts.`); @@ -158,7 +167,7 @@ document.addEventListener("DOMContentLoaded", async () => { if (currentBlockHeight === -1 || currentBlockHeight === 0) { console.log('⚠️ Block height not available, connecting to relays...'); await services.connectAllRelays(); - + // Attendre que le handshake arrive et que chain_tip soit défini await new Promise((resolve, reject) => { const timeout = setTimeout(() => { @@ -212,30 +221,30 @@ document.addEventListener("DOMContentLoaded", async () => { updateProgress(60); console.log(`🔄 Starting real block scan from ${lastScan} to ${currentBlockHeight}...`); - + // Utiliser updateDeviceBlockHeight qui gère la synchronisation si nécessaire // Cette méthode vérifie si last_scan < currentBlockHeight et synchronise si nécessaire try { await services.updateDeviceBlockHeight(); console.log('✅ Block scan completed successfully'); - + // Vérifier que la mise à jour a été sauvegardée const finalWallet = await services.getDeviceFromDatabase(); if (finalWallet?.sp_wallet?.last_scan) { const finalLastScan = finalWallet.sp_wallet.last_scan; console.log('✅ Wallet updated with last_scan:', finalLastScan); - + // Finalisation updateStatus('✅ Synchronisation terminée avec succès!', 'success'); updateProgress(100); updateSyncItem('blocksScanned', finalLastScan.toString(), 'completed'); updateSyncItem('blocksToScan', blocksToScan.toString(), 'completed'); - + // Activer le bouton continuer if (continueBtn) { continueBtn.disabled = false; } - + console.log('🎉 Block sync completed successfully'); } else { throw new Error('Failed to verify wallet update - last_scan not found'); @@ -248,9 +257,9 @@ document.addEventListener("DOMContentLoaded", async () => { } catch (error) { console.error('❌ Error during block sync:', error); const errorMessage = (error as Error).message; - + updateStatus(`❌ Erreur: ${errorMessage}`, 'error'); - + // Si l'erreur est liée aux prérequis, rediriger vers la page appropriée if (errorMessage.includes('PBKDF2') || errorMessage.includes('security')) { console.log('⚠️ Security error detected, redirecting to security-setup...'); diff --git a/src/pages/wallet-setup/wallet-setup.ts b/src/pages/wallet-setup/wallet-setup.ts index 93bc616..7d396af 100644 --- a/src/pages/wallet-setup/wallet-setup.ts +++ b/src/pages/wallet-setup/wallet-setup.ts @@ -78,24 +78,31 @@ document.addEventListener('DOMContentLoaded', async () => { services = await Services.getInstance(); console.log('✅ Services initialized successfully'); break; - } catch (error) { - console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, error instanceof Error ? error.message : String(error)); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : String(error); + console.log(`⏳ Services not ready yet (attempt ${attempts + 1}/${maxAttempts}):`, errorMessage); - // Diagnostic plus détaillé - if (attempts === 5) { - console.log('🔍 Diagnostic: Checking memory usage...'); - if ((performance as any).memory) { - const memory = (performance as any).memory; - console.log(`📊 Memory usage: ${Math.round(memory.usedJSHeapSize / 1024 / 1024)}MB / ${Math.round(memory.totalJSHeapSize / 1024 / 1024)}MB`); - } - } - - attempts++; - if (attempts >= maxAttempts) { - throw new Error(`Services failed to initialize after ${maxAttempts} attempts. This may be due to high memory usage or WebAssembly initialization issues.`); - } - await new Promise(resolve => setTimeout(resolve, delayMs)); + // Si c'est une erreur de mémoire, arrêter immédiatement + if (errorMessage.includes('Out of memory') || errorMessage.includes('insufficient memory')) { + console.error('🚫 Memory error detected - stopping retry attempts'); + updateStatus('❌ Erreur: Mémoire insuffisante. Veuillez actualiser la page.', 'error'); + throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.'); } + + // Diagnostic plus détaillé + if (attempts === 5) { + console.log('🔍 Diagnostic: Checking memory usage...'); + if ((performance as any).memory) { + const memory = (performance as any).memory; + console.log(`📊 Memory usage: ${Math.round(memory.usedJSHeapSize / 1024 / 1024)}MB / ${Math.round(memory.totalJSHeapSize / 1024 / 1024)}MB`); + } + } + + attempts++; + if (attempts >= maxAttempts) { + throw new Error(`Services failed to initialize after ${maxAttempts} attempts. This may be due to high memory usage or WebAssembly initialization issues.`); + } + await new Promise(resolve => setTimeout(resolve, delayMs)); } } catch (error) { console.error('❌ Services not available:', error); diff --git a/src/services/service.ts b/src/services/service.ts index 6fa6d6a..2925734 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -331,6 +331,16 @@ export default class Services { } catch (error) { console.error('❌ Service initialization failed:', error); + // Réinitialiser initializing pour permettre une nouvelle tentative après un délai + Services.initializing = null; + + // Si c'est une erreur de mémoire, ne pas réessayer immédiatement + const errorMessage = (error as Error).message || String(error); + if (errorMessage.includes('Out of memory') || errorMessage.includes('memory')) { + console.error('🚫 Memory error detected - cannot retry immediately'); + throw new Error('WebAssembly initialization failed due to insufficient memory. Please refresh the page.'); + } + throw error; }