Refactor handshake message handling using bach writing

This commit is contained in:
Sosthene 2025-07-02 11:36:15 +02:00
parent a027004bd0
commit 5192745a48

View File

@ -1280,7 +1280,21 @@ export default class Services {
setTimeout(async () => { setTimeout(async () => {
const newProcesses: OutPointProcessMap = handshakeMsg.processes_list; const newProcesses: OutPointProcessMap = handshakeMsg.processes_list;
if (newProcesses && Object.keys(newProcesses).length !== 0) { if (!newProcesses || Object.keys(newProcesses).length === 0) {
console.debug('Received empty processes list from', url);
return;
}
if (this.processesCache && Object.keys(this.processesCache).length === 0) {
// We restored db but cache is empty, meaning we're starting from scratch
try {
await this.batchSaveProcessesToDb(newProcesses);
} catch (e) {
console.error('Failed to save processes to db:', e);
}
} else {
// We need to update our processes with what relay provides
const toSave: Record<string, Process> = {};
for (const [processId, process] of Object.entries(newProcesses)) { for (const [processId, process] of Object.entries(newProcesses)) {
const existing = await this.getProcess(processId); const existing = await this.getProcess(processId);
if (existing) { if (existing) {
@ -1300,6 +1314,7 @@ export default class Services {
if (new_states.length != 0) { if (new_states.length != 0) {
// We request the new states // We request the new states
await this.requestDataFromPeers(processId, new_states, roles); await this.requestDataFromPeers(processId, new_states, roles);
toSave[processId] = process;
} }
// Just to be sure check if that's a pairing process // Just to be sure check if that's a pairing process
@ -1329,9 +1344,11 @@ export default class Services {
} else { } else {
// We add it to db // We add it to db
console.log(`Saving ${processId} to db`); console.log(`Saving ${processId} to db`);
await this.saveProcessToDb(processId, process as Process); toSave[processId] = process;
} }
} }
await this.batchSaveProcessesToDb(toSave);
} }
}, 500) }, 500)
} catch (e) { } catch (e) {