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 @@ -38,20 +38,30 @@ initPromise: Promise; status: DatabaseStatus; - constructor() { - const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous - ? undefined - : preloadedState.currentUserInfo?.id; - const isSupported = isSQLiteSupported(currentLoggedInUserID); + async init(currentLoggedInUserID: ?string): Promise { + if (!isSQLiteSupported(currentLoggedInUserID)) { + console.warn('Sqlite is not supported'); + this.status = databaseStatuses.initError; + return; + } + + if (this.status === databaseStatuses.initInProgress) { + await this.initPromise; + return; + } - if (!isSupported || isDesktopSafari) { - this.status = databaseStatuses.notSupported; - } else { - this.init(); + if ( + status === databaseStatuses.initSuccess || + status === databaseStatuses.initError + ) { + return; + } + + let encryptionKey = null; + if (isDesktopSafari) { + encryptionKey = await getSafariEncryptionKey(); } - } - init(encryptionKey?: ?SubtleCrypto$JsonWebKey) { this.status = databaseStatuses.initInProgress; this.worker = new SharedWorker(DATABASE_WORKER_PATH); this.worker.onerror = console.error; @@ -77,24 +87,8 @@ console.error(`Database initialization failure`, error); } })(); - } - - async initDBForLoggedInUser(currentLoggedInUserID: ?string) { - if (this.status === databaseStatuses.initSuccess) { - return; - } - - if ( - this.status === databaseStatuses.notSupported && - isSQLiteSupported(currentLoggedInUserID) - ) { - let encryptionKey = null; - if (isDesktopSafari) { - encryptionKey = await getSafariEncryptionKey(); - } - this.init(encryptionKey); - } + await this.initPromise; } async clearSensitiveData(): Promise { @@ -146,6 +140,10 @@ async function getDatabaseModule(): Promise { if (!databaseModule) { databaseModule = new DatabaseModule(); + const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous + ? undefined + : preloadedState.currentUserInfo?.id; + await databaseModule.init(currentLoggedInUserID); } return databaseModule; } diff --git a/web/database/sqlite-data-handler.js b/web/database/sqlite-data-handler.js --- a/web/database/sqlite-data-handler.js +++ b/web/database/sqlite-data-handler.js @@ -1,6 +1,5 @@ // @flow -import localforage from 'localforage'; import * as React from 'react'; import { useDispatch } from 'react-redux'; @@ -51,7 +50,7 @@ const databaseModule = await getDatabaseModule(); if (currentLoggedInUserID) { - await databaseModule.initDBForLoggedInUser(currentLoggedInUserID); + await databaseModule.init(currentLoggedInUserID); } if (!rehydrateConcluded) {