From 5192745a48b8b104743f8c16008fba10b43ec939 Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 2 Jul 2025 11:36:15 +0200 Subject: [PATCH] Refactor handshake message handling using bach writing --- src/services/service.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/services/service.ts b/src/services/service.ts index 2815d4f..de45e4b 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -1280,7 +1280,21 @@ export default class Services { setTimeout(async () => { 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 = {}; for (const [processId, process] of Object.entries(newProcesses)) { const existing = await this.getProcess(processId); if (existing) { @@ -1300,6 +1314,7 @@ export default class Services { if (new_states.length != 0) { // We request the new states await this.requestDataFromPeers(processId, new_states, roles); + toSave[processId] = process; } // Just to be sure check if that's a pairing process @@ -1329,9 +1344,11 @@ export default class Services { } else { // We add it to db console.log(`Saving ${processId} to db`); - await this.saveProcessToDb(processId, process as Process); + toSave[processId] = process; } } + + await this.batchSaveProcessesToDb(toSave); } }, 500) } catch (e) {