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 @@ -25,6 +25,11 @@ updateDraft, } from '../queries/draft-queries.js'; import { getMetadata, setMetadata } from '../queries/metadata-queries.js'; +import { + getPersistStorageItem, + removePersistStorageItem, + setPersistStorageItem, +} from '../queries/storage-engine-queries.js'; import { CURRENT_USER_ID_KEY, SQLITE_CONTENT, @@ -102,48 +107,70 @@ async function processAppRequest( message: WorkerRequestMessage, ): Promise { + // non-database operations if (message.type === workerRequestMessageTypes.PING) { return { type: workerResponseMessageTypes.PONG, text: 'PONG', }; - } else if (message.type === workerRequestMessageTypes.INIT) { - await initDatabase(message.sqljsFilePath, message.sqljsFilename); - return; } else if ( message.type === workerRequestMessageTypes.GENERATE_DATABASE_ENCRYPTION_KEY ) { const cryptoKey = await generateDatabaseCryptoKey(); await localforage.setItem(SQLITE_ENCRYPTION_KEY, cryptoKey); return; + } + + // database operations + if (message.type === workerRequestMessageTypes.INIT) { + await initDatabase(message.sqljsFilePath, message.sqljsFilename); + return; + } + + if (!sqliteDb) { + throw new Error('Database not initialized'); + } + + // read-only operations + if (message.type === workerRequestMessageTypes.GET_CLIENT_STORE) { + return { + type: workerResponseMessageTypes.CLIENT_STORE, + store: getClientStore(), + }; + } else if (message.type === workerRequestMessageTypes.GET_CURRENT_USER_ID) { + return { + type: workerResponseMessageTypes.GET_CURRENT_USER_ID, + userID: getMetadata(sqliteDb, CURRENT_USER_ID_KEY), + }; } else if ( - message.type === workerRequestMessageTypes.PROCESS_STORE_OPERATIONS + message.type === workerRequestMessageTypes.GET_PERSIST_STORAGE_ITEM ) { + return { + type: workerResponseMessageTypes.GET_PERSIST_STORAGE_ITEM, + item: getPersistStorageItem(sqliteDb, message.key), + }; + } + + // write operations + if (message.type === workerRequestMessageTypes.PROCESS_STORE_OPERATIONS) { const { draftStoreOperations } = message.storeOperations; if (draftStoreOperations) { processDraftStoreOperations(draftStoreOperations); } return; - } else if (message.type === workerRequestMessageTypes.GET_CLIENT_STORE) { - return { - type: workerResponseMessageTypes.CLIENT_STORE, - store: getClientStore(), - }; } else if (message.type === workerRequestMessageTypes.SET_CURRENT_USER_ID) { - if (!sqliteDb) { - throw new Error('Database not initialized'); - } setMetadata(sqliteDb, CURRENT_USER_ID_KEY, message.userID); return; - } else if (message.type === workerRequestMessageTypes.GET_CURRENT_USER_ID) { - if (!sqliteDb) { - throw new Error('Database not initialized'); - } - const userID = getMetadata(sqliteDb, CURRENT_USER_ID_KEY); - return { - type: workerResponseMessageTypes.GET_CURRENT_USER_ID, - userID, - }; + } else if ( + message.type === workerRequestMessageTypes.SET_PERSIST_STORAGE_ITEM + ) { + setPersistStorageItem(sqliteDb, message.key, message.item); + return; + } else if ( + message.type === workerRequestMessageTypes.REMOVE_PERSIST_STORAGE_ITEM + ) { + removePersistStorageItem(sqliteDb, message.key); + return; } throw new Error('Request type not supported'); 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 @@ -14,6 +14,9 @@ GET_CLIENT_STORE: 4, SET_CURRENT_USER_ID: 5, GET_CURRENT_USER_ID: 6, + GET_PERSIST_STORAGE_ITEM: 7, + SET_PERSIST_STORAGE_ITEM: 8, + REMOVE_PERSIST_STORAGE_ITEM: 9, }); export type PingWorkerRequestMessage = { @@ -49,6 +52,22 @@ +type: 6, }; +export type GetPersistStorageItemRequestMessage = { + +type: 7, + +key: string, +}; + +export type SetPersistStorageItemRequestMessage = { + +type: 8, + +key: string, + +item: string, +}; + +export type RemovePersistStorageItemRequestMessage = { + +type: 9, + +key: string, +}; + export type WorkerRequestMessage = | PingWorkerRequestMessage | InitWorkerRequestMessage @@ -56,7 +75,10 @@ | ProcessStoreOperationsRequestMessage | GetClientStoreRequestMessage | SetCurrentUserIDRequestMessage - | GetCurrentUserIDRequestMessage; + | GetCurrentUserIDRequestMessage + | GetPersistStorageItemRequestMessage + | SetPersistStorageItemRequestMessage + | RemovePersistStorageItemRequestMessage; export type WorkerRequestProxyMessage = { +id: number, @@ -68,6 +90,7 @@ PONG: 0, CLIENT_STORE: 1, GET_CURRENT_USER_ID: 2, + GET_PERSIST_STORAGE_ITEM: 3, }); export type PongWorkerResponseMessage = { @@ -85,10 +108,16 @@ +userID: ?string, }; +export type GetPersistStorageItemResponseMessage = { + +type: 3, + +item: string, +}; + export type WorkerResponseMessage = | PongWorkerResponseMessage | ClientStoreResponseMessage - | GetCurrentUserIDResponseMessage; + | GetCurrentUserIDResponseMessage + | GetPersistStorageItemResponseMessage; export type WorkerResponseProxyMessage = { +id?: number,