Changeset View
Changeset View
Standalone View
Standalone View
web/shared-worker/worker/process-operations.js
// @flow | // @flow | ||||
import type { ClientDBCommunityStoreOperation } from 'lib/ops/community-store-ops.js'; | import type { ClientDBCommunityStoreOperation } from 'lib/ops/community-store-ops.js'; | ||||
import type { ClientDBIntegrityStoreOperation } from 'lib/ops/integrity-store-ops.js'; | import type { ClientDBIntegrityStoreOperation } from 'lib/ops/integrity-store-ops.js'; | ||||
import type { ClientDBKeyserverStoreOperation } from 'lib/ops/keyserver-store-ops.js'; | import type { ClientDBKeyserverStoreOperation } from 'lib/ops/keyserver-store-ops.js'; | ||||
import type { ClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js'; | import type { ClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js'; | ||||
import type { ClientDBSyncedMetadataStoreOperation } from 'lib/ops/synced-metadata-store-ops.js'; | |||||
import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops.js'; | import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops.js'; | ||||
import type { | import type { | ||||
ClientDBDraftStoreOperation, | ClientDBDraftStoreOperation, | ||||
DraftStoreOperation, | DraftStoreOperation, | ||||
} from 'lib/types/draft-types.js'; | } from 'lib/types/draft-types.js'; | ||||
import type { | import type { | ||||
ClientDBStore, | ClientDBStore, | ||||
ClientDBStoreOperations, | ClientDBStoreOperations, | ||||
▲ Show 20 Lines • Show All 198 Lines • ▼ Show 20 Lines | try { | ||||
e, | e, | ||||
module, | module, | ||||
)}`, | )}`, | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
function processSyncedMetadataStoreOperations( | |||||
sqliteQueryExecutor: SQLiteQueryExecutor, | |||||
operations: $ReadOnlyArray<ClientDBSyncedMetadataStoreOperation>, | |||||
module: EmscriptenModule, | |||||
) { | |||||
for (const operation: ClientDBSyncedMetadataStoreOperation of operations) { | |||||
try { | |||||
if (operation.type === 'remove_all_synced_metadata') { | |||||
sqliteQueryExecutor.removeAllSyncedMetadata(); | |||||
} else if (operation.type === 'remove_synced_metadata') { | |||||
const { names } = operation.payload; | |||||
sqliteQueryExecutor.removeSyncedMetadata(names); | |||||
} else if (operation.type === 'replace_synced_metadata_entry') { | |||||
const { name, data } = operation.payload; | |||||
sqliteQueryExecutor.replaceSyncedMetadataEntry({ name, data }); | |||||
} else { | |||||
throw new Error('Unsupported synced metadata operation'); | |||||
} | |||||
} catch (e) { | |||||
throw new Error( | |||||
`Error while processing ${ | |||||
operation.type | |||||
} synced metadata operation: ${getProcessingStoreOpsExceptionMessage( | |||||
e, | |||||
module, | |||||
)}`, | |||||
); | |||||
} | |||||
} | |||||
} | |||||
function processDBStoreOperations( | function processDBStoreOperations( | ||||
sqliteQueryExecutor: SQLiteQueryExecutor, | sqliteQueryExecutor: SQLiteQueryExecutor, | ||||
storeOperations: ClientDBStoreOperations, | storeOperations: ClientDBStoreOperations, | ||||
module: EmscriptenModule, | module: EmscriptenModule, | ||||
) { | ) { | ||||
const { | const { | ||||
draftStoreOperations, | draftStoreOperations, | ||||
reportStoreOperations, | reportStoreOperations, | ||||
threadStoreOperations, | threadStoreOperations, | ||||
keyserverStoreOperations, | keyserverStoreOperations, | ||||
communityStoreOperations, | communityStoreOperations, | ||||
integrityStoreOperations, | integrityStoreOperations, | ||||
syncedMetadataStoreOperations, | |||||
} = storeOperations; | } = storeOperations; | ||||
try { | try { | ||||
sqliteQueryExecutor.beginTransaction(); | sqliteQueryExecutor.beginTransaction(); | ||||
if (draftStoreOperations && draftStoreOperations.length > 0) { | if (draftStoreOperations && draftStoreOperations.length > 0) { | ||||
processDraftStoreOperations( | processDraftStoreOperations( | ||||
sqliteQueryExecutor, | sqliteQueryExecutor, | ||||
draftStoreOperations, | draftStoreOperations, | ||||
Show All 30 Lines | try { | ||||
} | } | ||||
if (integrityStoreOperations && integrityStoreOperations.length > 0) { | if (integrityStoreOperations && integrityStoreOperations.length > 0) { | ||||
processIntegrityStoreOperations( | processIntegrityStoreOperations( | ||||
sqliteQueryExecutor, | sqliteQueryExecutor, | ||||
integrityStoreOperations, | integrityStoreOperations, | ||||
module, | module, | ||||
); | ); | ||||
} | } | ||||
if ( | |||||
syncedMetadataStoreOperations && | |||||
syncedMetadataStoreOperations.length > 0 | |||||
) { | |||||
processSyncedMetadataStoreOperations( | |||||
sqliteQueryExecutor, | |||||
syncedMetadataStoreOperations, | |||||
module, | |||||
); | |||||
} | |||||
sqliteQueryExecutor.commitTransaction(); | sqliteQueryExecutor.commitTransaction(); | ||||
} catch (e) { | } catch (e) { | ||||
sqliteQueryExecutor.rollbackTransaction(); | sqliteQueryExecutor.rollbackTransaction(); | ||||
console.log('Error while processing store ops: ', e); | console.log('Error while processing store ops: ', e); | ||||
throw e; | throw e; | ||||
} | } | ||||
} | } | ||||
function getClientStoreFromQueryExecutor( | function getClientStoreFromQueryExecutor( | ||||
sqliteQueryExecutor: SQLiteQueryExecutor, | sqliteQueryExecutor: SQLiteQueryExecutor, | ||||
): ClientDBStore { | ): ClientDBStore { | ||||
return { | return { | ||||
drafts: sqliteQueryExecutor.getAllDrafts(), | drafts: sqliteQueryExecutor.getAllDrafts(), | ||||
messages: [], | messages: [], | ||||
threads: sqliteQueryExecutor | threads: sqliteQueryExecutor | ||||
.getAllThreadsWeb() | .getAllThreadsWeb() | ||||
.map(t => webThreadToClientDBThreadInfo(t)), | .map(t => webThreadToClientDBThreadInfo(t)), | ||||
messageStoreThreads: [], | messageStoreThreads: [], | ||||
reports: sqliteQueryExecutor.getAllReports(), | reports: sqliteQueryExecutor.getAllReports(), | ||||
users: [], | users: [], | ||||
keyservers: sqliteQueryExecutor.getAllKeyservers(), | keyservers: sqliteQueryExecutor.getAllKeyservers(), | ||||
communities: sqliteQueryExecutor.getAllCommunities(), | communities: sqliteQueryExecutor.getAllCommunities(), | ||||
integrityThreadHashes: sqliteQueryExecutor.getAllIntegrityThreadHashes(), | integrityThreadHashes: sqliteQueryExecutor.getAllIntegrityThreadHashes(), | ||||
syncedMetadata: [], | syncedMetadata: sqliteQueryExecutor.getAllSyncedMetadata(), | ||||
}; | }; | ||||
} | } | ||||
export { | export { | ||||
processDBStoreOperations, | processDBStoreOperations, | ||||
getProcessingStoreOpsExceptionMessage, | getProcessingStoreOpsExceptionMessage, | ||||
getClientStoreFromQueryExecutor, | getClientStoreFromQueryExecutor, | ||||
}; | }; |