| … | … | @@ -29,6 +29,9 @@ let swarm = null |
| 29 | 29 | let identityKeyPair = null |
| 30 | 30 | let connections = new Set() // Track active connections |
| 31 | 31 | |
| 32 | // Initialization lock to prevent concurrent initialization |
| 33 | let initializationPromise = null |
| 34 | |
| 32 | 35 | // WDK singleton instances (Phase 6.1) |
| 33 | 36 | let wdkInstance = null |
| 34 | 37 | let walletManagers = {} // { bitcoin, ethereum } |
| … | … | @@ -112,10 +115,34 @@ async function apply(nodes, view, base) { |
| 112 | 115 | * @param {Object} identity - keet-identity-key instance |
| 113 | 116 | */ |
| 114 | 117 | async function initializeStorage(identity) { |
| 118 | // Return existing instance if already initialized |
| 115 | 119 | if (store && autobase && txHistoryDb) { |
| 116 | 120 | return { store, autobase, txHistoryDb, swarm } |
| 117 | 121 | } |
| 118 | 122 | |
| 123 | // If initialization is in progress, wait for it |
| 124 | if (initializationPromise) { |
| 125 | console.log('⏳ Storage initialization in progress, waiting...') |
| 126 | return await initializationPromise |
| 127 | } |
| 128 | |
| 129 | // Start new initialization |
| 130 | console.log('🔧 Initializing storage (singleton)') |
| 131 | initializationPromise = _initializeStorage(identity) |
| 132 | |
| 133 | try { |
| 134 | const result = await initializationPromise |
| 135 | return result |
| 136 | } finally { |
| 137 | // Clear the promise after initialization completes (success or failure) |
| 138 | initializationPromise = null |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Internal storage initialization (wrapped by singleton lock) |
| 144 | */ |
| 145 | async function _initializeStorage(identity) { |
| 119 | 146 | // Store identity keypair for attestation |
| 120 | 147 | identityKeyPair = identity.identityKeyPair |
| 121 | 148 | |