From 939c640e8f7ed83c8cdc9426ee49117edfc3b06d Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Tue, 11 Feb 2025 23:23:10 +0100 Subject: [PATCH] Add getBlob() and addDiff() + fixes to scanMissingData --- src/service-workers/database.worker.js | 81 +++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/src/service-workers/database.worker.js b/src/service-workers/database.worker.js index 2f869ef..918cc7d 100755 --- a/src/service-workers/database.worker.js +++ b/src/service-workers/database.worker.js @@ -1,5 +1,5 @@ let processesToScan = new Set(); -let toDownload = []; +let toDownload = new Set(); self.addEventListener('install', (event) => { event.waitUntil(self.skipWaiting()); // Activate worker immediately @@ -30,7 +30,6 @@ self.addEventListener('message', async (event) => { console.log('Scanning for missing data...'); const myProcesses = await getProcesses(processesToScan); - let toDownload = []; // Iterate on each process if (myProcesses && myProcesses.length != 0) { console.log(myProcesses); @@ -42,22 +41,33 @@ self.addEventListener('message', async (event) => { console.log(state); // iterate on pcd_commitment for (const hash of Object.values(state.pcd_commitment)) { - console.log(hash); - toDownload.push(hash); + // 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) + } else { + // We remove it if we have it in the set + console.log(`Removing ${hash} from the set`); + toDownload.delete(hash); + } } } } } - event.ports[0].postMessage({ - type: 'TO_DOWNLOAD', - data: toDownload, - }); + if (toDownload.size != 0) { + event.ports[0].postMessage({ + type: 'TO_DOWNLOAD', + data: Array.from(toDownload), + }); + } } fetchNotifications(); setInterval(fetchNotifications, 2 * 60 * 1000); scanMissingData(); - setInterval(scanMissingData, 2 * 1000); + setInterval(scanMissingData, 10 * 1000); } if (data.type === 'UPDATE_PROCESSES') { @@ -222,3 +232,56 @@ async function getAllDiffsNeedValidation() { }; }); } + +async function getBlob(hash) { + const db = await openDatabase(); + const storeName = 'data'; + const tx = db.transaction(storeName, 'readonly'); + const store = tx.objectStore(storeName); + const result = await new Promise((resolve, reject) => { + const getRequest = store.get(hash); + getRequest.onsuccess = () => resolve(getRequest.result); + getRequest.onerror = () => reject(getRequest.error); + }); + return result; +} + +async function addDiff(processId, stateId, hash) { + const db = await openDatabase(); + const storeName = 'diffs'; + const tx = db.transaction(storeName, 'readwrite'); + const store = tx.objectStore(storeName); + + // Check if the diff already exists + const existingDiff = await new Promise((resolve, reject) => { + const getRequest = store.get(hash); + getRequest.onsuccess = () => resolve(getRequest.result); + getRequest.onerror = () => reject(getRequest.error); + }); + + if (!existingDiff) { + console.log('Inserting a diff'); + const newDiff = { + process_id: processId, + state_id: stateId, + value_commitment: hash, + field: '', + description: null, + previous_value: null, + new_value: null, + notify_user: false, + need_validation: false, + validation_status: 'None' + }; + + const insertResult = await new Promise((resolve, reject) => { + const putRequest = store.put(newDiff); + putRequest.onsuccess = () => resolve(putRequest.result); + putRequest.onerror = () => reject(putRequest.error); + }); + + return insertResult; + } + + return existingDiff; +}