Refactor worker
This commit is contained in:
parent
85a2296d56
commit
aa22b78121
@ -1,6 +1,3 @@
|
||||
let processesToScan = new Set();
|
||||
let toDownload = new Set();
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(self.skipWaiting()); // Activate worker immediately
|
||||
});
|
||||
@ -12,82 +9,24 @@ self.addEventListener('activate', (event) => {
|
||||
// Event listener for messages from clients
|
||||
self.addEventListener('message', async (event) => {
|
||||
const data = event.data;
|
||||
if (data.type === 'START') {
|
||||
const fetchNotifications = async () => {
|
||||
const itemsWithFlag = await getAllDiffsNeedValidation();
|
||||
|
||||
// Process items with the specific flag
|
||||
itemsWithFlag?.forEach((item) => {
|
||||
console.log(item); // Do something with each flagged item
|
||||
});
|
||||
|
||||
event.ports[0].postMessage({
|
||||
type: 'NOTIFICATIONS',
|
||||
data: itemsWithFlag,
|
||||
});
|
||||
};
|
||||
const scanMissingData = async () => {
|
||||
console.log('Scanning for missing data...');
|
||||
const myProcesses = await getProcesses(processesToScan);
|
||||
|
||||
// Iterate on each process
|
||||
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 [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(processId, state.state_id, hash, field)
|
||||
} else {
|
||||
// We remove it if we have it in the set
|
||||
if (toDownload.delete(hash)) {
|
||||
console.log(`Removing ${hash} from the set`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (toDownload.size != 0) {
|
||||
event.ports[0].postMessage({
|
||||
type: 'TO_DOWNLOAD',
|
||||
data: Array.from(toDownload),
|
||||
});
|
||||
}
|
||||
}
|
||||
fetchNotifications();
|
||||
setInterval(fetchNotifications, 2 * 60 * 1000);
|
||||
scanMissingData();
|
||||
setInterval(scanMissingData, 10 * 1000);
|
||||
}
|
||||
console.log(data);
|
||||
|
||||
if (data.type === 'UPDATE_PROCESSES') {
|
||||
if (data.type === 'SCAN') {
|
||||
try {
|
||||
const { myProcessesId } = data.payload;
|
||||
console.log(myProcessesId);
|
||||
const myProcessesId = data.payload;
|
||||
if (myProcessesId && myProcessesId.length != 0) {
|
||||
for (const processId of myProcessesId) {
|
||||
processesToScan.add(processId);
|
||||
const toDownload = await scanMissingData(myProcessesId);
|
||||
if (toDownload.length != 0) {
|
||||
console.log('Sending TO_DOWNLOAD message');
|
||||
event.source.postMessage({ type: 'TO_DOWNLOAD', data: toDownload});
|
||||
}
|
||||
console.log(processesToScan);
|
||||
} else {
|
||||
event.ports[0].postMessage({ status: 'error', message: 'Empty lists' });
|
||||
event.source.postMessage({ status: 'error', message: 'Empty lists' });
|
||||
}
|
||||
} catch (error) {
|
||||
event.ports[0].postMessage({ status: 'error', message: error.message });
|
||||
event.source.postMessage({ status: 'error', message: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
if (data.type === 'ADD_OBJECT') {
|
||||
} else if (data.type === 'ADD_OBJECT') {
|
||||
try {
|
||||
const { storeName, object, key } = data.payload;
|
||||
const db = await openDatabase();
|
||||
@ -107,6 +46,42 @@ self.addEventListener('message', async (event) => {
|
||||
}
|
||||
});
|
||||
|
||||
async function scanMissingData(processesToScan) {
|
||||
console.log('Scanning for missing data...');
|
||||
const myProcesses = await getProcesses(processesToScan);
|
||||
|
||||
let toDownload = new Set();
|
||||
// Iterate on each process
|
||||
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 [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(processId, state.state_id, hash, field);
|
||||
} else {
|
||||
// We remove it if we have it in the set
|
||||
if (toDownload.delete(hash)) {
|
||||
console.log(`Removing ${hash} from the set`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(toDownload);
|
||||
return Array.from(toDownload);
|
||||
}
|
||||
|
||||
async function openDatabase() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open('4nk', 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user