Page MenuHomePhabricator

D6993.diff
No OneTemporary

D6993.diff

diff --git a/web/database/utils/constants.js b/web/database/utils/constants.js
new file mode 100644
--- /dev/null
+++ b/web/database/utils/constants.js
@@ -0,0 +1,3 @@
+// @flow
+
+export const SQLITE_CONTENT = 'sqliteFileContent';
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
@@ -1,6 +1,7 @@
// @flow
import localforage from 'localforage';
+import initSqlJs from 'sql.js';
import {
type SharedWorkerMessageEvent,
@@ -10,6 +11,7 @@
workerResponseMessageTypes,
type WorkerRequestProxyMessage,
} from '../../types/worker-types.js';
+import { SQLITE_CONTENT } from '../utils/constants.js';
const localforageConfig: PartialConfig = {
driver: localforage.INDEXEDDB,
@@ -20,14 +22,46 @@
};
localforage.config(localforageConfig);
-function processAppRequest(
+let sqliteDb = null;
+
+async function initDatabase(sqljsFilePath: string, sqljsFilename: ?string) {
+ const content = await localforage.getItem(SQLITE_CONTENT);
+
+ const locateFile = defaultFilename => {
+ if (sqljsFilename) {
+ return `${sqljsFilePath}/${sqljsFilename}`;
+ }
+ return `${sqljsFilePath}/${defaultFilename}`;
+ };
+ const SQL = await initSqlJs({
+ locateFile,
+ });
+
+ if (content) {
+ sqliteDb = new SQL.Database(new Uint8Array(content));
+ } else {
+ sqliteDb = new SQL.Database();
+ }
+
+ const versionData = sqliteDb.exec('PRAGMA user_version;');
+ if (versionData.length && versionData[0].values.length) {
+ console.info(`Db version: ${versionData[0].values[0]}`);
+ } else {
+ throw new Error('Error while retrieving database version');
+ }
+}
+
+async function processAppRequest(
message: WorkerRequestMessage,
-): ?WorkerResponseMessage {
+): Promise<?WorkerResponseMessage> {
if (message.type === workerRequestMessageTypes.PING) {
return {
type: workerResponseMessageTypes.PONG,
text: 'PONG',
};
+ } else if (message.type === workerRequestMessageTypes.INIT) {
+ await initDatabase(message.sqljsFilePath, message.sqljsFilename);
+ return;
}
throw new Error('Request type not supported');
@@ -51,7 +85,7 @@
}
try {
- const result = processAppRequest(message);
+ const result = await processAppRequest(message);
port.postMessage({
id,
message: result,
diff --git a/web/types/worker-types.js b/web/types/worker-types.js
--- a/web/types/worker-types.js
+++ b/web/types/worker-types.js
@@ -3,6 +3,7 @@
// The types of messages sent from app to worker
export const workerRequestMessageTypes = Object.freeze({
PING: 0,
+ INIT: 1,
});
export type PingWorkerRequestMessage = {
@@ -10,7 +11,15 @@
+text: string,
};
-export type WorkerRequestMessage = PingWorkerRequestMessage;
+export type InitWorkerRequestMessage = {
+ +type: 1,
+ +sqljsFilePath: string,
+ +sqljsFilename: ?string,
+};
+
+export type WorkerRequestMessage =
+ | PingWorkerRequestMessage
+ | InitWorkerRequestMessage;
export type WorkerRequestProxyMessage = {
+id: number,

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 10:32 PM (21 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2591942
Default Alt Text
D6993.diff (3 KB)

Event Timeline