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 stateId: string | null = null;
private sdkClient: any;
private processesCache: Record<string, Process> = {};
private myProcesses: Set<string> = 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<Process | null> {
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<Record<string, Process>> {
const db = await Database.getInstance();
const processes: Record<string, Process> = 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<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>) {
const db = await Database.getInstance();
const storeName = 'processes';