diff --git a/web/database/database-module-provider.js b/web/database/database-module-provider.js --- a/web/database/database-module-provider.js +++ b/web/database/database-module-provider.js @@ -146,11 +146,13 @@ let databaseModule: ?DatabaseModule = null; async function getDatabaseModule(): Promise { - if (!databaseModule) { - databaseModule = new DatabaseModule(); - await databaseModule.init({ clearDatabase: false }); + if (databaseModule) { + return databaseModule; } - return databaseModule; + const newModule = new DatabaseModule(); + databaseModule = newModule; + await newModule.init({ clearDatabase: false }); + return newModule; } // Start initializing the database immediately getDatabaseModule(); diff --git a/web/database/worker/db-worker.js b/web/database/worker/db-worker.js --- a/web/database/worker/db-worker.js +++ b/web/database/worker/db-worker.js @@ -53,13 +53,14 @@ commQueryExecutorFilename: ?string, encryptionKeyJWK?: ?SubtleCrypto$JsonWebKey, ) { - dbModule = getDatabaseModule( + const newModule = getDatabaseModule( commQueryExecutorFilename, databaseModuleFilePath, ); + dbModule = newModule; try { - const result = dbModule.CommQueryExecutor.testDBOperation(); + const result = newModule.CommQueryExecutor.testDBOperation(); console.log(result); } catch (e) { console.error(e); @@ -90,7 +91,7 @@ } if (dbContent) { - importDatabaseContent(dbContent, dbModule, COMM_SQLITE_DATABASE_PATH); + importDatabaseContent(dbContent, newModule, COMM_SQLITE_DATABASE_PATH); console.info( 'Database exists and is properly encrypted, using persisted data', @@ -98,14 +99,15 @@ } else { console.info('Creating fresh database'); } - sqliteQueryExecutor = new dbModule.SQLiteQueryExecutor( + sqliteQueryExecutor = new newModule.SQLiteQueryExecutor( COMM_SQLITE_DATABASE_PATH, ); } async function persist() { persistInProgress = true; - if (!sqliteQueryExecutor || !dbModule) { + const module = dbModule; + if (!sqliteQueryExecutor || !module) { persistInProgress = false; throw new Error('Database not initialized'); } @@ -116,7 +118,7 @@ while (persistNeeded) { persistNeeded = false; - const dbData = exportDatabaseContent(dbModule, COMM_SQLITE_DATABASE_PATH); + const dbData = exportDatabaseContent(module, COMM_SQLITE_DATABASE_PATH); if (!encryptionKey) { persistInProgress = false; throw new Error('Encryption key is missing');