diff --git a/src/services/service.ts b/src/services/service.ts index 8303ef6..6613d3a 100755 --- a/src/services/service.ts +++ b/src/services/service.ts @@ -23,6 +23,7 @@ export default class Services { private processId: string | null = null; private stateId: string | null = null; private sdkClient: any; + private processesCache: Record = {}; private myProcesses: Set = new Set(); private notifications: any[] | null = null; private subscriptions: { element: Element; event: string; eventHandler: string }[] = []; @@ -912,12 +913,16 @@ export default class Services { public async saveProcessToDb(processId: string, process: Process) { const db = await Database.getInstance(); + const storeName = 'processes'; try { await db.addObject({ - storeName: 'processes', + storeName, object: process, key: processId, }); + + // Update the process in the cache + this.processesCache[processId] = process; } catch (e) { console.error(`Failed to save process ${processId}: ${e}`); } @@ -983,39 +988,29 @@ export default class Services { } public async getProcess(processId: string): Promise { - const db = await Database.getInstance(); - return await db.getObject('processes', processId); + if (this.processesCache[processId]) { + return this.processesCache[processId]; + } else { + const db = await Database.getInstance(); + const process = await db.getObject('processes', processId); + return process; + } } public async getProcesses(): Promise> { - const db = await Database.getInstance(); - - const processes: Record = await db.dumpStore('processes'); - return processes; + if (Object.keys(this.processesCache).length > 0) { + return this.processesCache; + } else { + try { + const db = await Database.getInstance(); + this.processesCache = await db.dumpStore('processes'); + return this.processesCache; + } catch (e) { + throw e; + } + } } - // TODO rewrite that it's a mess and we don't use it now - // public async getChildrenOfProcess(processId: string): Promise { - // const processes = await this.getProcesses(); - - // const res = []; - // for (const [hash, process] of Object.entries(processes)) { - // const firstState = process.states[0]; - // const pcdCommitment = firstState['pcd_commitment']; - // try { - // const parentIdHash = pcdCommitment['parent_id']; - // const diff = await this.getDiffByValue(parentIdHash); - // if (diff && diff['new_value'] === processId) { - // res.push(JSON.stringify(process)); - // } - // } catch (e) { - // continue; - // } - // } - - // return res; - // } - public async restoreProcessesFromBackUp(processes: Record) { const db = await Database.getInstance(); const storeName = 'processes';