From a2a7022c08ee447d8f3149078f95a2a8dadf10a5 Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Fri, 14 Feb 2025 16:24:55 +0100 Subject: [PATCH] Fix worker --- src/service-workers/database.worker.js | 6 +++-- src/services/database.service.ts | 35 +++++++++++++------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/service-workers/database.worker.js b/src/service-workers/database.worker.js index daf65e2..903ed32 100755 --- a/src/service-workers/database.worker.js +++ b/src/service-workers/database.worker.js @@ -34,16 +34,18 @@ self.addEventListener('message', async (event) => { if (myProcesses && myProcesses.length != 0) { for (const process of myProcesses) { // Iterate on states + const firstState = process.states[0]; + const processId = firstState.commited_in; for (const state of process.states) { if (!state.pcd_commitment) continue; // iterate on pcd_commitment - for (const hash of Object.values(state.pcd_commitment)) { + for (const [field, hash] of Object.entries(state.pcd_commitment)) { // Check if we have the data in db const existingData = await getBlob(hash); if (!existingData) { toDownload.add(hash); // We also add an entry in diff, in case it doesn't already exist - await addDiff(process, state, hash) + await addDiff(processId, state.state_id, hash, field) } else { // We remove it if we have it in the set if (toDownload.delete(hash)) { diff --git a/src/services/database.service.ts b/src/services/database.service.ts index 845802d..e99370f 100755 --- a/src/services/database.service.ts +++ b/src/services/database.service.ts @@ -117,23 +117,24 @@ export class Database { try { // Get existing service worker registrations const registrations = await navigator.serviceWorker.getRegistrations(); - if (registrations.length > 0) { - console.log('Existing Service Worker(s) detected. Unregistering...'); + if (registrations.length === 0) { + // No existing workers: register a new one. + this.serviceWorkerRegistration = await navigator.serviceWorker.register('/src/service-workers/database.worker.js', { type: 'module' }); + console.log('Service Worker registered with scope:', registration.scope); + } else if (registrations.length === 1) { + // One existing worker: update it (restart it) without unregistering. + this.serviceWorkerRegistration = registrations[0]; + await this.serviceWorkerRegistration.update(); + console.log('Service worker updated'); + } else { + // More than one existing worker: unregister them all and register a new one. + console.log('Multiple Service Worker(s) detected. Unregistering all...'); await Promise.all(registrations.map(reg => reg.unregister())); console.log('All previous Service Workers unregistered.'); + this.serviceWorkerRegistration = await navigator.serviceWorker.register('/src/service-workers/database.worker.js', { type: 'module' }); + console.log('Service Worker registered with scope:', registration.scope); } - // Now check the messageChannel to avoid unnecessary reinitialization - if (this.messageChannel) { - console.log('Persistent update channel already initialized.'); - return; - } - - // Register the new service worker - const registration = await navigator.serviceWorker.register('/src/service-workers/database.worker.js', { type: 'module' }); - console.log('Service Worker registered with scope:', registration.scope); - - this.serviceWorkerRegistration = registration; await this.checkForUpdates(); // Set up the message channels @@ -143,7 +144,7 @@ export class Database { this.messageChannelForGet.port1.onmessage = this.handleGetObjectResponse; // Ensure the new service worker is activated before sending messages - const activeWorker = registration.active || (await this.waitForServiceWorkerActivation(registration)); + const activeWorker = this.serviceWorkerRegistration.active || (await this.waitForServiceWorkerActivation(this.serviceWorkerRegistration)); activeWorker?.postMessage( { type: 'START' }, @@ -198,10 +199,10 @@ export class Database { let requestedStateId = []; for (const hash of data.data) { try { - const value = await service.fetchValueFromStorage(hash); - if (value) { + const valueBytes = await service.fetchValueFromStorage(hash); + if (valueBytes) { // Save data to db - const blob = new Blob([value], {type: "text/plain"}); + const blob = new Blob([valueBytes], {type: "application/octet-stream"}); await service.saveBlobToDb(hash, blob); } else { // We first request the data from managers