Add service worker message handlers

This commit is contained in:
omaroughriss 2025-11-27 16:49:15 +01:00
parent 54dcd2b29d
commit 59fff148ac

View File

@ -171,24 +171,85 @@ export class Database {
}
}
// ============================================
// SERVICE WORKER MESSAGE HANDLERS
// ============================================
private async handleDatabaseRequest(request: any): Promise<void> {
const { id, action, payload } = request;
try {
let result;
switch (action) {
case 'GET_OBJECT':
result = await this.getObject(payload.storeName, payload.key);
break;
case 'GET_MULTIPLE_OBJECTS':
result = await this.sendMessageToWorker('GET_MULTIPLE_OBJECTS', payload);
break;
case 'GET_ALL_OBJECTS':
result = await this.sendMessageToWorker('GET_ALL_OBJECTS', payload);
break;
case 'GET_ALL_OBJECTS_WITH_FILTER':
result = await this.sendMessageToWorker('GET_ALL_OBJECTS_WITH_FILTER', payload);
break;
default:
throw new Error(`Unknown database action: ${action}`);
}
if (this.serviceWorkerRegistration?.active) {
this.serviceWorkerRegistration.active.postMessage({
type: 'DB_RESPONSE',
id,
result
});
}
} catch (error: any) {
console.error('[Database] Error handling database request:', error);
if (this.serviceWorkerRegistration?.active) {
this.serviceWorkerRegistration.active.postMessage({
type: 'DB_ERROR',
id,
error: error.message || String(error)
});
}
}
}
private async handleServiceWorkerMessage(message: any) {
switch (message.type) {
case 'TO_DOWNLOAD':
await this.handleDownloadList(message.data);
break;
case 'DIFFS_TO_CREATE':
await this.handleDiffsToCreate(message.data);
break;
default:
console.warn('Unknown message type received from service worker:', message);
}
}
private async handleDiffsToCreate(diffs: any[]): Promise<void> {
console.log(`[Database] Creating ${diffs.length} diffs from Service Worker scan`);
try {
await this.saveDiffs(diffs);
console.log('[Database] Diffs created successfully');
} catch (error) {
console.error('[Database] Error creating diffs:', error);
}
}
private async handleDownloadList(downloadList: string[]): Promise<void> {
// Download the missing data
let requestedStateId: string[] = [];
const service = await Services.getInstance();
for (const hash of downloadList) {
const diff = await service.getDiffByValue(hash);
if (!diff) {
// This should never happen
console.warn(`Missing a diff for hash ${hash}`);
continue;
}
@ -198,22 +259,15 @@ export class Database {
try {
const valueBytes = await service.fetchValueFromStorage(hash);
if (valueBytes) {
// Save data to db
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
await service.saveBlobToDb(hash, blob);
document.dispatchEvent(
new CustomEvent('newDataReceived', {
detail: {
processId,
stateId,
hash,
},
detail: { processId, stateId, hash },
}),
);
} else {
// We first request the data from managers
console.log('Request data from managers of the process');
// get the diff from db
if (!requestedStateId.includes(stateId)) {
await service.requestDataFromPeers(processId, [stateId], [roles]);
requestedStateId.push(stateId);