Changeset View
Changeset View
Standalone View
Standalone View
web/database/database-module-provider.js
// @flow | // @flow | ||||
import { DATABASE_WORKER_PATH, SQLJS_FILE_PATH } from './utils/constants.js'; | import { DATABASE_WORKER_PATH, SQLJS_FILE_PATH } from './utils/constants.js'; | ||||
import { isSQLiteSupported } from './utils/db-utils.js'; | import { isDesktopSafari, isSQLiteSupported } from './utils/db-utils.js'; | ||||
import WorkerConnectionProxy from './utils/WorkerConnectionProxy.js'; | import WorkerConnectionProxy from './utils/WorkerConnectionProxy.js'; | ||||
import type { AppState } from '../redux/redux-setup.js'; | import type { AppState } from '../redux/redux-setup.js'; | ||||
import { | import { | ||||
workerRequestMessageTypes, | workerRequestMessageTypes, | ||||
type WorkerRequestMessage, | type WorkerRequestMessage, | ||||
type WorkerResponseMessage, | type WorkerResponseMessage, | ||||
} from '../types/worker-types.js'; | } from '../types/worker-types.js'; | ||||
declare var sqljsFilename: string; | declare var sqljsFilename: string; | ||||
declare var preloadedState: AppState; | declare var preloadedState: AppState; | ||||
const databaseStatuses = Object.freeze({ | const databaseStatuses = Object.freeze({ | ||||
notSupported: 'NOT_SUPPORTED', | notSupported: 'NOT_SUPPORTED', | ||||
initSuccess: 'INIT_SUCCESS', | initSuccess: 'INIT_SUCCESS', | ||||
initInProgress: 'INIT_IN_PROGRESS', | initInProgress: 'INIT_IN_PROGRESS', | ||||
initError: 'INIT_ERROR', | initError: 'INIT_ERROR', | ||||
}); | }); | ||||
type DatabaseStatus = $Values<typeof databaseStatuses>; | type DatabaseStatus = $Values<typeof databaseStatuses>; | ||||
const isSafari = isDesktopSafari(); | |||||
class DatabaseModule { | class DatabaseModule { | ||||
worker: SharedWorker; | worker: SharedWorker; | ||||
workerProxy: WorkerConnectionProxy; | workerProxy: WorkerConnectionProxy; | ||||
initPromise: Promise<void>; | initPromise: Promise<void>; | ||||
status: DatabaseStatus; | status: DatabaseStatus; | ||||
constructor() { | constructor() { | ||||
const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous | const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous | ||||
? undefined | ? undefined | ||||
: preloadedState.currentUserInfo?.id; | : preloadedState.currentUserInfo?.id; | ||||
const isSupported = isSQLiteSupported(currentLoggedInUserID); | const isSupported = isSQLiteSupported(currentLoggedInUserID); | ||||
if (!isSupported) { | if (!isSupported || isSafari) { | ||||
this.status = databaseStatuses.notSupported; | this.status = databaseStatuses.notSupported; | ||||
} else { | } else { | ||||
this.init(); | this.init(); | ||||
} | } | ||||
} | } | ||||
init(encryptionKey?: ?SubtleCrypto$JsonWebKey) { | init(encryptionKey?: ?SubtleCrypto$JsonWebKey) { | ||||
this.status = databaseStatuses.initInProgress; | this.status = databaseStatuses.initInProgress; | ||||
▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines |