Save maps in SessionStorage

This commit is contained in:
Sosthene 2025-09-11 08:53:03 +02:00
parent c1f12b2cf7
commit 114c20dd26

View File

@ -8,66 +8,56 @@ export default abstract class AbstractService {
protected constructor() { }
protected static setItem(key: string, process: any): void {
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
const index: number = list.findIndex((item: any) => item.process.processData.uid === process.processData.uid);
if (index !== -1) {
list[index] = {
process: process,
timestamp: Date.now()
};
} else {
list.push({
process: process,
timestamp: Date.now()
});
}
sessionStorage.setItem(key, JSON.stringify(list));
protected static setItem(key: string, processId: string, process: any): void {
const cache: Record<string, {process: any, timestamp: number}> =
JSON.parse(sessionStorage.getItem(key) || '{}');
cache[processId] = {
process: process, // we overwrite existing process
timestamp: Date.now()
};
sessionStorage.setItem(key, JSON.stringify(cache));
}
protected static getItem(key: string, uid: string): any {
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
if (list.length === 0) {
return null;
}
const item: any = list.find((item: any) => item.process.processData.uid === uid);
protected static getItem(key: string, processId: string): any | null {
const cache: Record<string, {process: any, timestamp: number}> =
JSON.parse(sessionStorage.getItem(key) || '{}');
const item = cache[processId];
if (!item) {
return null;
}
const now: number = Date.now();
if ((now - item.timestamp) < this.CACHE_TTL) {
return item.process;
return { process: item.process, timestamp: item.timestamp };
}
return null;
}
protected static getItems(key: string): any[] {
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
protected static getItems(key: string): Record<string, any> {
const cache: Record<string, {process: any, timestamp: number}> =
JSON.parse(sessionStorage.getItem(key) || '{}');
const now: number = Date.now();
const items: any[] = [];
for (const item of list) {
const validItems: Record<string, any> = {};
for (const [processId, item] of Object.entries(cache)) {
if (now - item.timestamp < this.CACHE_TTL) {
items.push(item.process);
validItems[processId] = item.process;
}
}
return items;
return validItems;
}
protected static removeItem(key: string, uid: string): void {
const list: any[] = JSON.parse(sessionStorage.getItem(key) || '[]');
protected static removeItem(key: string, processId: string): void {
const cache: Record<string, {process: any, timestamp: number}> =
JSON.parse(sessionStorage.getItem(key) || '{}');
const index: number = list.findIndex((item: any) => item.process.processData.uid === uid);
if (index !== -1) {
list.splice(index, 1);
}
delete cache[processId];
sessionStorage.setItem(key, JSON.stringify(list));
sessionStorage.setItem(key, JSON.stringify(cache));
}
}