Page MenuHomePhorge

D9108.1765069835.diff
No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None

D9108.1765069835.diff

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
@@ -1,5 +1,6 @@
// @flow
+import invariant from 'invariant';
import localforage from 'localforage';
import {
@@ -33,26 +34,41 @@
type DatabaseStatus = $Values<typeof databaseStatuses>;
class DatabaseModule {
- worker: SharedWorker;
- workerProxy: WorkerConnectionProxy;
- initPromise: Promise<void>;
- status: DatabaseStatus;
+ worker: ?SharedWorker;
+ workerProxy: ?WorkerConnectionProxy;
+ initPromise: ?Promise<void>;
+ status: DatabaseStatus = databaseStatuses.notSupported;
- constructor() {
- const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous
- ? undefined
- : preloadedState.currentUserInfo?.id;
- const isSupported = isSQLiteSupported(currentLoggedInUserID);
+ async init(currentLoggedInUserID: ?string): Promise<void> {
+ if (!currentLoggedInUserID) {
+ return;
+ }
- if (!isSupported || isDesktopSafari) {
+ if (!isSQLiteSupported(currentLoggedInUserID)) {
+ console.warn('Sqlite is not supported');
this.status = databaseStatuses.notSupported;
- } else {
- this.init();
+ return;
+ }
+
+ if (this.status === databaseStatuses.initInProgress) {
+ await this.initPromise;
+ return;
+ }
+
+ if (
+ this.status === databaseStatuses.initSuccess ||
+ this.status === databaseStatuses.initError
+ ) {
+ return;
}
- }
- init(encryptionKey?: ?SubtleCrypto$JsonWebKey) {
this.status = databaseStatuses.initInProgress;
+
+ let encryptionKey = null;
+ if (isDesktopSafari) {
+ encryptionKey = await getSafariEncryptionKey();
+ }
+
this.worker = new SharedWorker(DATABASE_WORKER_PATH);
this.worker.onerror = console.error;
this.workerProxy = new WorkerConnectionProxy(
@@ -64,6 +80,7 @@
this.initPromise = (async () => {
try {
+ invariant(this.workerProxy, 'Worker proxy should exist');
await this.workerProxy.scheduleOnWorker({
type: workerRequestMessageTypes.INIT,
databaseModuleFilePath: `${origin}${DATABASE_MODULE_FILE_PATH}`,
@@ -77,28 +94,13 @@
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<void> {
this.status = databaseStatuses.notSupported;
+ invariant(this.workerProxy, 'Worker proxy should exist');
await this.workerProxy.scheduleOnWorker({
type: workerRequestMessageTypes.CLEAR_SENSITIVE_DATA,
});
@@ -126,6 +128,7 @@
throw new Error('Database could not be initialized');
}
+ invariant(this.workerProxy, 'Worker proxy should exist');
return this.workerProxy.scheduleOnWorker(payload);
}
}
@@ -146,6 +149,10 @@
async function getDatabaseModule(): Promise<DatabaseModule> {
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
@@ -50,7 +50,7 @@
const databaseModule = await getDatabaseModule();
if (currentLoggedInUserID) {
- await databaseModule.initDBForLoggedInUser(currentLoggedInUserID);
+ await databaseModule.init(currentLoggedInUserID);
}
if (!rehydrateConcluded) {

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 7, 1:10 AM (11 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5840760
Default Alt Text
D9108.1765069835.diff (4 KB)

Event Timeline