Update Scan to use the new communication method (request data from database service)

This commit is contained in:
omaroughriss 2025-11-27 16:40:02 +01:00
parent 29d2688278
commit 92a9c6e455

View File

@ -70,33 +70,58 @@ async function requestFromMainThread(client, action, payload) {
// SCAN LOGIC
// ============================================
async function scanMissingData(processesToScan) {
console.log('Scanning for missing data...');
const myProcesses = await getProcesses(processesToScan);
async function scanMissingData(processesToScan, client) {
console.log('[Service Worker] Scanning for missing data...');
const myProcesses = await requestFromMainThread(client, 'GET_MULTIPLE_OBJECTS', {
storeName: 'processes',
keys: processesToScan
});
let toDownload = new Set();
// Iterate on each process
let diffsToCreate = [];
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.state_id === EMPTY32BYTES) continue;
// iterate on pcd_commitment
for (const [field, hash] of Object.entries(state.pcd_commitment)) {
// Skip public fields
if (state.public_data[field] !== undefined || field === 'roles') continue;
// Check if we have the data in db
const existingData = await getBlob(hash);
const existingData = await requestFromMainThread(client, 'GET_OBJECT', {
storeName: 'data',
key: hash
});
if (!existingData) {
toDownload.add(hash);
// We also add an entry in diff, in case it doesn't already exist
await addDiff(processId, state.state_id, hash, state.roles, field);
const existingDiff = await requestFromMainThread(client, 'GET_OBJECT', {
storeName: 'diffs',
key: hash
});
if (!existingDiff) {
diffsToCreate.push({
process_id: processId,
state_id: state.state_id,
value_commitment: hash,
roles: state.roles,
field: field,
description: null,
previous_value: null,
new_value: null,
notify_user: false,
need_validation: false,
validation_status: 'None'
});
}
} else {
// We remove it if we have it in the set
if (toDownload.delete(hash)) {
console.log(`Removing ${hash} from the set`);
console.log(`[Service Worker] Removing ${hash} from the set`);
}
}
}