Fix worker

This commit is contained in:
NicolasCantu 2025-02-14 16:24:55 +01:00
parent 682ab03861
commit a2a7022c08
2 changed files with 22 additions and 19 deletions

View File

@ -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)) {

View File

@ -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