Add service worker message handlers
This commit is contained in:
parent
54dcd2b29d
commit
59fff148ac
@ -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) {
|
private async handleServiceWorkerMessage(message: any) {
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'TO_DOWNLOAD':
|
case 'TO_DOWNLOAD':
|
||||||
await this.handleDownloadList(message.data);
|
await this.handleDownloadList(message.data);
|
||||||
break;
|
break;
|
||||||
|
case 'DIFFS_TO_CREATE':
|
||||||
|
await this.handleDiffsToCreate(message.data);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
console.warn('Unknown message type received from service worker:', message);
|
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> {
|
private async handleDownloadList(downloadList: string[]): Promise<void> {
|
||||||
// Download the missing data
|
|
||||||
let requestedStateId: string[] = [];
|
let requestedStateId: string[] = [];
|
||||||
const service = await Services.getInstance();
|
const service = await Services.getInstance();
|
||||||
for (const hash of downloadList) {
|
for (const hash of downloadList) {
|
||||||
const diff = await service.getDiffByValue(hash);
|
const diff = await service.getDiffByValue(hash);
|
||||||
if (!diff) {
|
if (!diff) {
|
||||||
// This should never happen
|
|
||||||
console.warn(`Missing a diff for hash ${hash}`);
|
console.warn(`Missing a diff for hash ${hash}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -198,22 +259,15 @@ export class Database {
|
|||||||
try {
|
try {
|
||||||
const valueBytes = await service.fetchValueFromStorage(hash);
|
const valueBytes = await service.fetchValueFromStorage(hash);
|
||||||
if (valueBytes) {
|
if (valueBytes) {
|
||||||
// Save data to db
|
|
||||||
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
|
const blob = new Blob([valueBytes], { type: 'application/octet-stream' });
|
||||||
await service.saveBlobToDb(hash, blob);
|
await service.saveBlobToDb(hash, blob);
|
||||||
document.dispatchEvent(
|
document.dispatchEvent(
|
||||||
new CustomEvent('newDataReceived', {
|
new CustomEvent('newDataReceived', {
|
||||||
detail: {
|
detail: { processId, stateId, hash },
|
||||||
processId,
|
|
||||||
stateId,
|
|
||||||
hash,
|
|
||||||
},
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// We first request the data from managers
|
|
||||||
console.log('Request data from managers of the process');
|
console.log('Request data from managers of the process');
|
||||||
// get the diff from db
|
|
||||||
if (!requestedStateId.includes(stateId)) {
|
if (!requestedStateId.includes(stateId)) {
|
||||||
await service.requestDataFromPeers(processId, [stateId], [roles]);
|
await service.requestDataFromPeers(processId, [stateId], [roles]);
|
||||||
requestedStateId.push(stateId);
|
requestedStateId.push(stateId);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user