use a processCache for optimization

This commit is contained in:
Sosthene 2025-07-01 18:06:44 +02:00
parent 19b2ab994e
commit 58fed7a53b

View File

@ -23,6 +23,7 @@ export default class Services {
private processId: string | null = null; private processId: string | null = null;
private stateId: string | null = null; private stateId: string | null = null;
private sdkClient: any; private sdkClient: any;
private processesCache: Record<string, Process> = {};
private myProcesses: Set<string> = new Set(); private myProcesses: Set<string> = new Set();
private notifications: any[] | null = null; private notifications: any[] | null = null;
private subscriptions: { element: Element; event: string; eventHandler: string }[] = []; private subscriptions: { element: Element; event: string; eventHandler: string }[] = [];
@ -912,12 +913,16 @@ export default class Services {
public async saveProcessToDb(processId: string, process: Process) { public async saveProcessToDb(processId: string, process: Process) {
const db = await Database.getInstance(); const db = await Database.getInstance();
const storeName = 'processes';
try { try {
await db.addObject({ await db.addObject({
storeName: 'processes', storeName,
object: process, object: process,
key: processId, key: processId,
}); });
// Update the process in the cache
this.processesCache[processId] = process;
} catch (e) { } catch (e) {
console.error(`Failed to save process ${processId}: ${e}`); console.error(`Failed to save process ${processId}: ${e}`);
} }
@ -983,39 +988,29 @@ export default class Services {
} }
public async getProcess(processId: string): Promise<Process | null> { public async getProcess(processId: string): Promise<Process | null> {
const db = await Database.getInstance(); if (this.processesCache[processId]) {
return await db.getObject('processes', processId); return this.processesCache[processId];
} else {
const db = await Database.getInstance();
const process = await db.getObject('processes', processId);
return process;
}
} }
public async getProcesses(): Promise<Record<string, Process>> { public async getProcesses(): Promise<Record<string, Process>> {
const db = await Database.getInstance(); if (Object.keys(this.processesCache).length > 0) {
return this.processesCache;
const processes: Record<string, Process> = await db.dumpStore('processes'); } else {
return processes; 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<string[]> {
// 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<string, Process>) { public async restoreProcessesFromBackUp(processes: Record<string, Process>) {
const db = await Database.getInstance(); const db = await Database.getInstance();
const storeName = 'processes'; const storeName = 'processes';