Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32167719
D7189.1765053693.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
D7189.1765053693.diff
View Options
diff --git a/web/database/database-module-provider.js b/web/database/database-module-provider.js
new file mode 100644
--- /dev/null
+++ b/web/database/database-module-provider.js
@@ -0,0 +1,105 @@
+// @flow
+
+import { DATABASE_WORKER_PATH, SQLJS_FILE_PATH } from './utils/constants.js';
+import { isSQLiteSupported } from './utils/db-utils.js';
+import WorkerConnectionProxy from './utils/WorkerConnectionProxy.js';
+import type { AppState } from '../redux/redux-setup.js';
+import {
+ workerRequestMessageTypes,
+ type WorkerRequestMessage,
+ type WorkerResponseMessage,
+} from '../types/worker-types.js';
+
+declare var sqljsFilename: string;
+declare var preloadedState: AppState;
+
+const databaseStatuses = Object.freeze({
+ notSupported: 'NOT_SUPPORTED',
+ initSuccess: 'INIT_SUCCESS',
+ initInProgress: 'INIT_IN_PROGRESS',
+ initError: 'INIT_ERROR',
+});
+
+type DatabaseStatus = $Values<typeof databaseStatuses>;
+
+class DatabaseModule {
+ worker: SharedWorker;
+ workerProxy: WorkerConnectionProxy;
+ initPromise: Promise<void>;
+ status: DatabaseStatus;
+
+ constructor() {
+ const currentLoggedInUserID = preloadedState.currentUserInfo?.anonymous
+ ? undefined
+ : preloadedState.currentUserInfo?.id;
+ const isSupported = isSQLiteSupported(currentLoggedInUserID);
+
+ if (!isSupported) {
+ this.status = databaseStatuses.notSupported;
+ } else {
+ this.init();
+ }
+ }
+
+ onError: (error: Error) => void = error => {
+ console.error(error);
+ };
+
+ init() {
+ this.status = databaseStatuses.initInProgress;
+ this.worker = new SharedWorker(DATABASE_WORKER_PATH);
+ this.worker.onerror = this.onError;
+ this.workerProxy = new WorkerConnectionProxy(
+ this.worker.port,
+ this.onError,
+ );
+
+ const origin = window.location.origin;
+
+ this.initPromise = (async () => {
+ try {
+ await this.workerProxy.scheduleOnWorker({
+ type: workerRequestMessageTypes.INIT,
+ sqljsFilePath: `${origin}${SQLJS_FILE_PATH}`,
+ sqljsFilename,
+ });
+ this.status = databaseStatuses.initSuccess;
+ console.info('Database initialization success');
+ } catch (error) {
+ this.status = databaseStatuses.initError;
+ console.error(`Database initialization failure`, error);
+ }
+ })();
+ }
+
+ userLoggedIn(currentLoggedInUserID: ?string) {
+ if (
+ this.status === databaseStatuses.notSupported &&
+ isSQLiteSupported(currentLoggedInUserID)
+ ) {
+ this.init();
+ }
+ }
+
+ async schedule(
+ payload: WorkerRequestMessage,
+ ): Promise<?WorkerResponseMessage> {
+ if (this.status === databaseStatuses.notSupported) {
+ throw new Error('Database not supported');
+ }
+
+ if (this.status === databaseStatuses.initInProgress) {
+ await this.initPromise;
+ }
+
+ if (this.status === databaseStatuses.initError) {
+ throw new Error('Database could not be initialized');
+ }
+
+ return this.workerProxy.scheduleOnWorker(payload);
+ }
+}
+
+const databaseModule: DatabaseModule = new DatabaseModule();
+
+export { databaseModule };
diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js
--- a/web/database/utils/constants.js
+++ b/web/database/utils/constants.js
@@ -7,6 +7,9 @@
export const DB_PERSIST_THROTTLE_WAIT_MS = 300;
+export const DATABASE_WORKER_PATH = '/worker/database';
+export const SQLJS_FILE_PATH = '/compiled/webworkers';
+
export const DB_SUPPORTED_OS: $ReadOnlyArray<string> = [
'Windows 10',
'Linux',
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Dec 6, 8:41 PM (22 h, 32 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5840709
Default Alt Text
D7189.1765053693.diff (3 KB)
Attached To
Mode
D7189: [web-db] add database module provider
Attached
Detach File
Event Timeline
Log In to Comment